import { Component, EventEmitter, Input, Output, SimpleChanges } from '@angular/core';
import { CommonNzModule } from '../../../../../common.nz.module';
import { KnowledgeService } from '../../../../../services/knowledge.service';
import { SettingService } from '../../../../../services/setting.service';
import { ProblemAssessmentStatus } from '../problem-assessment.utils';
import { AssessmentFormComponent } from './form/form.component';

export interface ProblemAssessmentDetailConfig {
  visible: boolean;
  data: ProblemAssessment.ProblemAssessmentDto;
}

interface InfoValues {
  name: string;
  creator: string;
  reportingTime: string;
  involved: string;
  risk: string;
  description: string;
}
interface InfoItem {
  label: string;
  key: keyof InfoValues;
}

@Component({
  selector: 'problem-assessment-detail',
  standalone: true,
  imports: [CommonNzModule, AssessmentFormComponent],
  templateUrl: './detail.component.html',
  styleUrl: './detail.component.less',
})
export class ProblemAssessmentDetailComponent {
  @Input() data!: ProblemAssessment.ProblemAssessmentDto;
  @Output() onClose = new EventEmitter<void>();

  unassessed = ProblemAssessmentStatus.Unassessed;
  assessing = ProblemAssessmentStatus.Assessing;
  assessed = ProblemAssessmentStatus.Assessed;

  formVisible = false;

  constructor(
    private knowledge: KnowledgeService,
    private setting: SettingService
  ) {}

  values: InfoValues = {
    name: '',
    creator: '',
    reportingTime: '',
    involved: '',
    risk: '',
    description: '',
  };
  images: string[] = Array.from({ length: 12 }).map(() => `https://loremflickr.com/260/150?q=${Math.random()}`);
  infoItems: InfoItem[] = [
    { label: '问题名称', key: 'name' },
    { label: '提交人', key: 'creator' },
    { label: '提交时间', key: 'reportingTime' },
    { label: '场景/设备', key: 'involved' },
    // { label: '风险项', key: 'risk' },
  ];

  ngOnChanges(changes: SimpleChanges) {
    if (changes['data']) {
      this.formVisible = false;
      this.clearValues();
      if (this.data) {
        this.fillOutValues();
      }
    }
  }
  clearValues() {
    Object.keys(this.values).forEach(key => {
      this.values[key as keyof InfoValues] = '';
    });
  }
  fillOutValues() {
    if (!this.data) return;
    this.values = {
      name: this.data.name,
      creator: this.data.creator,
      reportingTime: this.data.reportingTime,
      involved: this.involvedText,
      risk: this.riskText,
      description: this.data.description,
    };
  }
  handleClose() {
    this.onClose.emit();
  }
  startToAssess() {
    this.setting.getUsersOfCurrentCompany();
    this.knowledge.fetchRiskItems();
    this.formVisible = true;
  }
  get riskText() {
    const type = this.data?.type;
    const category = this.data?.category;
    if (!type || !category) return '';
    return `${this.knowledge.getTypeName(type)}   - ${this.knowledge.getCategoryName(category, type)}`;
  }

  get involvedText() {
    const { involvedEquipments, involvedOperations, involvedPositions } = this.data;
    if (!involvedEquipments && !involvedOperations && !involvedPositions) return '';
    return [involvedEquipments, involvedOperations, involvedPositions].map(arr => arr.join(',')).join(' / ');
  }
}
