import { Component } from '@angular/core';
import { ModalController } from '@ionic/angular/standalone';
import { IonButton } from '@ionic/angular/standalone';
import { FlowExecutor } from '../lib/flow.executor';
import { MockProgressTask, MockRetryTask, MockSuccessTask } from './mock-tasks/mock-tasks';
import { FlowDisplayComponent } from '../lib/flow-display/flow-display.component';
import { TriageResultComponent } from './consult-tasks/triage-result/triage-result.component';
import { TriageTask } from './consult-tasks/consult-tasks';
import { SymptomInputModalComponent } from './consult-tasks/symptom-input/symptom-input.modal';

@Component({
  selector: 'app-page-flow-test',
  templateUrl: './page-flow-test.component.html',
  styleUrls: ['./page-flow-test.component.scss'],
  imports:[IonButton,FlowDisplayComponent,
    TriageResultComponent,
    SymptomInputModalComponent
  ],
  standalone: true,
})
export class PageFlowTestComponent {

  private executor = new FlowExecutor(3, true);

  constructor(
    private modalCtrl: ModalController
  ) {
    // 创建工作流
    const workflow = {
      title: '示例工作流',
      desc: '演示工作流执行过程',
      taskList: [
        new MockSuccessTask(),
        new MockRetryTask(),
        new MockProgressTask()
      ]
    };

    this.executor.setWorkflow(workflow);
  }

  async startWorkflow() {
    const modal = await this.modalCtrl.create({
      component: FlowDisplayComponent,
      componentProps: { executor: this.executor }
    });

    await modal.present();
    await this.executor.start();
  }

  // 问诊任务
  async startTriage() {
    this.executor.setWorkflow({
      title: '医疗分诊流程',
      taskList: [new TriageTask({modalCtrl:this.modalCtrl})]
    });

    this.executor.taskSuccess$.subscribe(task => {
      if (task instanceof TriageTask) {
        this.showTriageResult(task);
      }
    });

    await this.executor.start();
  }

  private async showTriageResult(task: TriageTask) {
    const modal = await this.modalCtrl.create({
      component: TriageResultComponent,
      componentProps: {
        symptom: task.getData('symptomDescription'),
        department: task.getData('department'),
        reason: task.getData('triageReason')
      }
    });

    await modal.present();
  }
}
