/* eslint-disable @typescript-eslint/member-ordering */
import {
  Component,
  inject,
  signal
} from '@angular/core';
import { CommonModule } from '@angular/common';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { lucidePower } from '@ng-icons/lucide';

import type { DesktopSettingsSnapshot } from '../../../../core/platform/electron/electron-api.models';
import { loadGeneralSettingsFromStorage, saveGeneralSettingsToStorage } from '../../../../infrastructure/persistence';
import { ElectronBridgeService } from '../../../../core/platform/electron/electron-bridge.service';
import { PlatformService } from '../../../../core/platform';

@Component({
  selector: 'app-general-settings',
  standalone: true,
  imports: [CommonModule, NgIcon],
  viewProviders: [
    provideIcons({
      lucidePower
    })
  ],
  templateUrl: './general-settings.component.html'
})
export class GeneralSettingsComponent {
  private platform = inject(PlatformService);
  private electronBridge = inject(ElectronBridgeService);

  readonly isElectron = this.platform.isElectron;
  reopenLastViewedChat = signal(true);
  autoStart = signal(false);
  closeToTray = signal(true);
  savingAutoStart = signal(false);
  savingCloseToTray = signal(false);

  constructor() {
    this.loadGeneralSettings();

    if (this.isElectron) {
      void this.loadDesktopSettings();
    }
  }

  onReopenLastViewedChatChange(event: Event): void {
    const input = event.target as HTMLInputElement;
    const settings = saveGeneralSettingsToStorage({
      reopenLastViewedChat: !!input.checked
    });

    this.reopenLastViewedChat.set(settings.reopenLastViewedChat);
  }

  async onAutoStartChange(event: Event): Promise<void> {
    const input = event.target as HTMLInputElement;
    const enabled = !!input.checked;
    const api = this.electronBridge.getApi();

    if (!this.isElectron || !api) {
      input.checked = this.autoStart();
      return;
    }

    this.savingAutoStart.set(true);

    try {
      const snapshot = await api.setDesktopSettings({ autoStart: enabled });

      this.applyDesktopSettings(snapshot);
    } catch {
      input.checked = this.autoStart();
    } finally {
      this.savingAutoStart.set(false);
    }
  }

  async onCloseToTrayChange(event: Event): Promise<void> {
    const input = event.target as HTMLInputElement;
    const enabled = !!input.checked;
    const api = this.electronBridge.getApi();

    if (!this.isElectron || !api) {
      input.checked = this.closeToTray();
      return;
    }

    this.savingCloseToTray.set(true);

    try {
      const snapshot = await api.setDesktopSettings({ closeToTray: enabled });

      this.applyDesktopSettings(snapshot);
    } catch {
      input.checked = this.closeToTray();
    } finally {
      this.savingCloseToTray.set(false);
    }
  }

  private async loadDesktopSettings(): Promise<void> {
    const api = this.electronBridge.getApi();

    if (!api) {
      return;
    }

    try {
      const snapshot = await api.getDesktopSettings();

      this.applyDesktopSettings(snapshot);
    } catch {}
  }

  private loadGeneralSettings(): void {
    const settings = loadGeneralSettingsFromStorage();

    this.reopenLastViewedChat.set(settings.reopenLastViewedChat);
  }

  private applyDesktopSettings(snapshot: DesktopSettingsSnapshot): void {
    this.autoStart.set(snapshot.autoStart);
    this.closeToTray.set(snapshot.closeToTray);
  }
}
