import { Component, Inject, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { WorkflowService } from '../../services/samwf/workflow.service';
import { ModulosInterface } from '../../interfaces/samwf/modulos-interface';
import {
  MatDialog,
  MatDialogRef,
  MAT_DIALOG_DATA,
} from '@angular/material/dialog';

@Component({
  selector: 'app-workflow-form',
  templateUrl: './workflow-form.component.html',
  styleUrls: ['./workflow-form.component.css'],
})
export class WorkflowFormComponent implements OnInit {
  formGroup: FormGroup;
  modules: any[] = [];

  constructor(
    public dialogRef: MatDialogRef<any>,
    @Inject(MAT_DIALOG_DATA) public data: any,
    private _samwfService: WorkflowService
  ) {
    this.modules = data.modules;
    this.formGroup = this.createFormGroup();
  }

  private createFormGroup() {
    if (this.data.action == 'Registrar') {
      return new FormGroup({
        name: new FormControl('', Validators.required),
        description: new FormControl('', Validators.required),
        diagram: new FormControl([]),
        tasks: new FormControl([
          {
            id: 1,
            name: 'Iniciar workflow',
            description: 'Iniciar workflow',

            sequence: 1,

            nextSequence: null,

            changeSequence: null,

            workflow: null,

            employeesNotify: [
              {
                id: 1,
              },

              {
                id: 2,
              },
            ],

            employeesValidate: [
              {
                id: 1,
              },

              {
                id: 2,
              },
            ],

            validationRules: [],
            status: 'Pending',
            isAvailable: 1,
            isValidated: 0,
            notification: 0,
            isConditional: 0,

            id_workflow: null,

            id_module: null,
            created_at: '2022-03-03 09:24:43',
            updated_at: '2022-03-03 09:24:43',
          },
          {
            id: 2,
            name: 'Finalizar workflow',
            description: 'Finalizar workflow',

            sequence: 1000,

            nextSequence: null,

            changeSequence: null,

            workflow: null,

            employeesNotify: [
              {
                id: 1,
              },

              {
                id: 2,
              },
            ],

            employeesValidate: [
              {
                id: 1,
              },

              {
                id: 2,
              },
            ],

            validationRules: [],
            status: 'Pending',
            isAvailable: 1,
            isValidated: 0,
            notification: 0,
            isConditional: 0,

            id_workflow: null,

            id_module: null,
            created_at: '2022-03-03 09:24:43',
            updated_at: '2022-03-03 09:24:43',
          },
        ]),
        id_module: new FormControl(''),
        module: new FormControl('', Validators.required),
        isAutomatic: new FormControl(false, Validators.required),
        isAvailable: new FormControl(true),
      });
    } else {
      return new FormGroup({
        name: new FormControl(this.data.item.name, Validators.required),
        description: new FormControl(
          this.data.item.description,
          Validators.required
        ),
        diagram: new FormControl(this.data.item.diagram),
        tasks: new FormControl(this.data.item.tasks),
        module: new FormControl(
          this.modules[
            this.modules.findIndex(
              (object) => object.id == this.data.item.id_module
            )
          ].id,
          Validators.required
        ),
        id_module: new FormControl(
          this.modules[
            this.modules.findIndex(
              (object) => object.id == this.data.item.id_module
            )
          ].id
        ),
        isAutomatic: new FormControl(
          this.data.item.isAutomatic,
          Validators.required
        ),
        isAvailable: new FormControl(this.data.item.isAvailable),
        isCompleted: new FormControl(this.data.item.isCompleted),
        isStopped: new FormControl(this.data.item.isStopped),
      });
    }
  }

  ngOnInit(): void {}

  get name() {
    return this.formGroup.get('name');
  }

  get description() {
    return this.formGroup.get('description');
  }

  get diagram() {
    return this.formGroup.get('diagram');
  }

  get tasks() {
    return this.formGroup.get('tasks');
  }

  get module() {
    return this.formGroup.get('module');
  }

  get isAutomatic() {
    return this.formGroup.get('isAutomatic');
  }

  get isAvailable() {
    return this.formGroup.get('isAvailable');
  }
}
