import {Component, inject, OnInit} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {Observable} from 'rxjs';
import {Button} from 'primeng/button';
import {ToggleSwitch} from 'primeng/toggleswitch';
import {MenuItem, MessageService} from 'primeng/api';
import {SplitButton} from 'primeng/splitbutton';

import {AppSettingsService} from '../../../shared/service/app-settings.service';
import {BookMetadataManageService} from '../../book/service/book-metadata-manage.service';
import {AppSettingKey, AppSettings, CoverCroppingSettings} from '../../../shared/model/app-settings.model';
import {filter, take} from 'rxjs/operators';
import {InputText} from 'primeng/inputtext';
import {Slider} from 'primeng/slider';
import {TranslocoDirective, TranslocoPipe, TranslocoService} from '@jsverse/transloco';

export const SUPPORT_ANIMATION_KEY = 'booklore-support-animation';

@Component({
  selector: 'app-global-preferences',
  standalone: true,
  imports: [
    Button,
    ToggleSwitch,
    FormsModule,
    InputText,
    Slider,
    SplitButton,
    TranslocoDirective,
    TranslocoPipe
  ],
  templateUrl: './global-preferences.component.html',
  styleUrl: './global-preferences.component.scss'
})
export class GlobalPreferencesComponent implements OnInit {

  toggles = {
    autoBookSearch: false,
    similarBookRecommendation: false
  };

  supportButtonAnimation = localStorage.getItem(SUPPORT_ANIMATION_KEY) !== 'false';

  coverCroppingSettings: CoverCroppingSettings = {
    verticalCroppingEnabled: false,
    horizontalCroppingEnabled: false,
    aspectRatioThreshold: 2.5,
    smartCroppingEnabled: false
  };

  private appSettingsService = inject(AppSettingsService);
  private bookMetadataManageService = inject(BookMetadataManageService);
  private messageService = inject(MessageService);
  private t = inject(TranslocoService);

  appSettings$: Observable<AppSettings | null> = this.appSettingsService.appSettings$;
  maxFileUploadSizeInMb?: number;
  regenerateCoverMenuItems: MenuItem[] = [];

  ngOnInit(): void {
    this.regenerateCoverMenuItems = [
      {
        label: this.t.translate('settingsApp.covers.regenerateMissingBtn'),
        icon: 'pi pi-images',
        command: () => this.regenerateCovers(true)
      }
    ];

    this.appSettings$.pipe(
      filter(settings => !!settings),
      take(1)
    ).subscribe(settings => {
      if (settings?.maxFileUploadSizeInMb) {
        this.maxFileUploadSizeInMb = settings.maxFileUploadSizeInMb;
      }
      if (settings?.coverCroppingSettings) {
        this.coverCroppingSettings = {...settings.coverCroppingSettings};
      }
      this.toggles.autoBookSearch = settings.autoBookSearch ?? false;
      this.toggles.similarBookRecommendation = settings.similarBookRecommendation ?? false;
    });
  }

  onToggleChange(settingKey: keyof typeof this.toggles, checked: boolean): void {
    this.toggles[settingKey] = checked;
    const toggleKeyMap: Record<string, AppSettingKey> = {
      autoBookSearch: AppSettingKey.AUTO_BOOK_SEARCH,
      similarBookRecommendation: AppSettingKey.SIMILAR_BOOK_RECOMMENDATION
    };
    const keyToSend = toggleKeyMap[settingKey];
    if (keyToSend) {
      this.saveSetting(keyToSend, checked);
    } else {
      console.warn(`Unknown toggle key: ${settingKey}`);
    }
  }

  onSupportAnimationChange(checked: boolean): void {
    this.supportButtonAnimation = checked;
    localStorage.setItem(SUPPORT_ANIMATION_KEY, String(checked));
    window.dispatchEvent(new StorageEvent('storage', {key: SUPPORT_ANIMATION_KEY, newValue: String(checked)}));
  }

  onCoverCroppingChange(): void {
    this.saveSetting(AppSettingKey.COVER_CROPPING_SETTINGS, this.coverCroppingSettings);
  }

  saveFileSize() {
    if (!this.maxFileUploadSizeInMb || this.maxFileUploadSizeInMb <= 0) {
      this.showMessage('error', this.t.translate('settingsApp.fileManagement.invalidInput'), this.t.translate('settingsApp.fileManagement.invalidInputDetail'));
      return;
    }
    this.saveSetting(AppSettingKey.MAX_FILE_UPLOAD_SIZE_IN_MB, this.maxFileUploadSizeInMb);
  }

  regenerateCovers(missingOnly = false): void {
    this.bookMetadataManageService.regenerateCovers(missingOnly).subscribe({
      next: () =>
        this.showMessage('success', this.t.translate('settingsApp.covers.regenerateStarted'), this.t.translate('settingsApp.covers.regenerateStartedDetail')),
      error: () =>
        this.showMessage('error', this.t.translate('common.error'), this.t.translate('settingsApp.covers.regenerateError'))
    });
  }

  private saveSetting(key: string, value: unknown): void {
    this.appSettingsService.saveSettings([{key, newValue: value}]).subscribe({
      next: () =>
        this.showMessage('success', this.t.translate('settingsApp.settingsSaved'), this.t.translate('settingsApp.settingsSavedDetail')),
      error: () =>
        this.showMessage('error', this.t.translate('common.error'), this.t.translate('settingsApp.settingsError'))
    });
  }

  private showMessage(severity: 'success' | 'error', summary: string, detail: string): void {
    this.messageService.add({severity, summary, detail});
  }
}
