import { AbstractDialog } from '@/shared/abstractClasses/abstract-dialog';
import { UikitLabelComponent } from '@/uikit';
import { CommonModule } from '@angular/common';
import { Component, computed, EventEmitter, Input, Output, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { ButtonDirective, ButtonSeverity } from 'primeng/button';
import { SharedLightBottomsheetComponent } from '../dialog/light-bottomsheet.component';
import { FormFooterActionsComponent } from '../formFooterActions/form-footer-actions.component';

type SeasonKey = 0 | 1 | 2 | 3;

interface SeasonItem {
  key: SeasonKey;
  label: string;
  severity: ButtonSeverity;
}

@Component({
  selector: 'season-picker-dialog',
  templateUrl: './season-picker-dialog.component.html',
  imports: [
    CommonModule,
    FormsModule,
    SharedLightBottomsheetComponent,
    FormFooterActionsComponent,
    ButtonDirective,
    UikitLabelComponent,
  ],
})
export class SeasonPickerDialogComponent extends AbstractDialog {
  @Input() years: { year: number; value: number }[] = [];
  @Input() selectedYear!: number;
  @Input() selectedSeason!: SeasonKey;
  @Input() seasons: SeasonItem[] = [];

  @Output() submit = new EventEmitter<{ year: number; season: number }>();

  selectedYearDraft = signal(this.selectedYear);
  selectedSeasonDraft = signal(this.selectedSeason);

  isSeasonDisabled = computed(() => this.selectedYear >= this.selectedYearDraft());

  onSubmit(): void {
    this.submit.emit({
      year: this.selectedYearDraft(),
      season: this.selectedSeasonDraft(),
    });
    this.close();
  }

  ngOnChanges() {
    this.selectedYearDraft.set(this.selectedYear);
    this.selectedSeasonDraft.set(this.selectedSeason);
  }
}
