import { Component, EventEmitter, Input, Output } from '@angular/core';
import { Subscription } from 'rxjs';
import { scaleInOut } from '../../../../common.animation';
import { CommonNzModule } from './../../../../common.nz.module';
import { BasicDataService } from './../../../../services/basic.service';
import { DatavSelectComponent } from './../../../../shared/datav-select/datav-select.component';

@Component({
  selector: 'left-panel-top',
  standalone: true,
  imports: [CommonNzModule, DatavSelectComponent],
  templateUrl: './left-panel-top.component.html',
  styleUrl: './left-panel-top.component.less',
  animations: [scaleInOut],
})
export class LeftPanelTopComponent {
  @Input() currentLine: number | null = null;
  @Input() continueDays: number = 0;
  @Output() currentLineChange = new EventEmitter<number>();
  @Output() accidentButtonClick = new EventEmitter<void>();
  subscription?: Subscription;
  constructor(private basic: BasicDataService) {
    this.subscription = basic.$fetched.subscribe(() => {
      this.setOptions();
      this.setLine();
    });
  }
  options: Option<number>[] = [];

  ngAfterViewInit() {
    this.setOptions();
    this.setLine();
  }
  setOptions() {
    this.options = this.basic.lines
      .filter(line => line.value !== '所有线路')
      .map(line => ({
        label: line.value,
        value: line.id,
      }));
  }
  setLine() {
    if (this.options.length === 0) return;
    const planeLine = this.options.find(option => option.label === '机场联络线');
    let currentLine;
    if (planeLine) {
      currentLine = planeLine.value;
    } else {
      currentLine = this.options[0].value || null;
    }
    currentLine && this.currentLineChange.emit(currentLine);
  }
  onChange(value: number) {
    this.currentLineChange.emit(value);
  }

  onAccidentButtonClick() {
    this.accidentButtonClick.emit();
  }

  ngOnDestroy(): void {
    this.subscription?.unsubscribe();
  }
}
