import { Component, EventEmitter, Input, Output, SimpleChanges } from '@angular/core';
import dayjs from 'dayjs';
import { CommonNzModule } from './../../../../../../../common.nz.module';

interface ProgressNode {
  title: string;
  date: string;
  done: boolean;
  toNext: boolean;
  actionText: string;
}

function findTheMostRecentlyUpdatedRound(rounds: Hazard.InspectionPlanRound[]) {
  let latestRound: Hazard.InspectionPlanRound | null = null;
  for (const round of rounds) {
    if (round.handles.length > 0) {
      if (!latestRound) {
        latestRound = round;
      } else {
        if (dayjs(round.updatedDate).isAfter(dayjs(latestRound.updatedDate))) {
          latestRound = round;
        }
      }
    }
  }
  return latestRound;
}

@Component({
  selector: 'plan-detail-progress',
  standalone: true,
  imports: [CommonNzModule],
  templateUrl: './progress.component.html',
  styleUrl: './progress.component.less',
})
export class PlanDetailProgressComponent {
  @Input({ required: true }) data!: Hazard.InspectionPlanDetail;
  @Output() onConfirm = new EventEmitter<void>();

  ngOnChanges(changes: SimpleChanges) {
    if (changes['data']) {
      this.updateProgressList();
    }
  }

  list: ProgressNode[] = [
    { title: '计划创建', date: '', done: true, toNext: false, actionText: '' },
    { title: '任务制定', date: '', done: false, toNext: false, actionText: '' },
    { title: '制定完成', date: '', done: false, toNext: false, actionText: '' }, // action: 确认下发
    { title: '计划执行中', date: '', done: false, toNext: false, actionText: '' },
    { title: '计划结束', date: '', done: false, toNext: false, actionText: '' },
  ];

  updateProgressList() {
    const [create, make, confirm, execute, end] = this.list;
    this.updateCreate(create);
    this.updateMake(make);
    this.updateConfirm(confirm);
    this.updateExecute(execute);
    this.updateEnd(end);
  }

  updateCreate(node: ProgressNode) {
    // 计划创建
    node.date = dayjs(this.data.createdDate).format('YYYY/MM/DD HH:mm');
    node.toNext = true;
    node.done = true;
  }

  updateMake(node: ProgressNode) {
    // 任务制定
    switch (this.data.status) {
      // 未执行,制定中，已制定，已发布，执行中（有一条填报数据），已结束（所有数据都填报完成）
      case '未执行':
        node.done = false;
        node.toNext = false;
        break;
      case '制定中': // 已选择部门, 未制定任务
        node.done = true;
        node.toNext = false;
        break;
      default:
        node.done = true;
        node.toNext = true;
        break;
    }
  }

  updateConfirm(node: ProgressNode) {
    // 制定完成
    switch (this.data.status) {
      case '未执行':
      case '制定中':
        node.done = false;
        node.toNext = false;
        break;
      case '已制定':
        node.done = true;
        node.toNext = false;
        node.actionText = '确认下发';
        break;
      case '已发布':
        node.done = true;
        node.toNext = true;
        node.actionText = '';
        break;
      default:
        node.done = true;
        node.toNext = true;
        node.actionText = '';
        break;
    }
  }

  updateExecute(node: ProgressNode) {
    // 计划执行中
    switch (this.data.status) {
      case '未执行':
      case '制定中':
      case '已制定':
      case '已发布':
        node.done = false;
        node.toNext = false;
        break;
      case '执行中':
        node.done = true;
        node.toNext = true;
        break;
      default:
        node.done = true;
        node.toNext = true;
        break;
    }
  }

  updateEnd(node: ProgressNode) {
    switch (this.data.status) {
      case '已结束':
        node.done = true;
        break;
      default:
        node.done = false;
        break;
    }
  }

  previousNodeCanPrecessNext(index: number) {
    return index !== 0 && this.list[index - 1].toNext;
  }

  confirmToPublish() {
    this.onConfirm.emit();
  }

  onActionClick(node: ProgressNode) {
    if (node.actionText === '确认下发') {
      this.onConfirm.emit();
    }
  }
}
