import { Component, OnInit, ViewChild, Input, AfterContentChecked, AfterViewInit, ChangeDetectionStrategy, AfterContentInit, ViewContainerRef } from '@angular/core';
import { FormGroup, Validators, FormBuilder } from '@angular/forms';
import { CaGeneralInformationComponent } from '../components/ca-general-information/ca-general-information.component';
import { MAT_STEPPER_GLOBAL_OPTIONS } from '@angular/cdk/stepper';
import { ActivatedRoute, Router } from '@angular/router';
//import { EsConnectionsComponent } from '../components/es-connections/es-connections.component';
//import { EsEntityComponent } from '../components/es-entity/es-entity.component';
//import { EsStepsComponent } from '../components/es-steps/es-steps.component';
//import { EsSummaryComponent } from '../components/es-summary/es-summary.component';
import { NavigationService } from '@xdf/layouts';
import { IDirty } from '@xdf/gallery';
//import { ExternalSystemService } from '../../service/external-system.service';
import { first } from 'rxjs/operators';
import { CaFrequentQuestionsComponent } from '../components/ca-frequent-questions/ca-frequent-questions.component';


@Component({
  selector: 'byte-agent',
  templateUrl: './agent.component.html',
  styleUrls: ['./agent.component.scss'],
  providers: [{
    provide: MAT_STEPPER_GLOBAL_OPTIONS, useValue: { displayDefaultIndicatorType: false }
  }]

})
export class AgentComponent implements OnInit, IDirty {

  @ViewChild(CaGeneralInformationComponent, { static: true })
  generalInformationComponent: CaGeneralInformationComponent;

  @ViewChild(CaFrequentQuestionsComponent, { static: true })
  frequentQuestionsComponent: CaFrequentQuestionsComponent;

  externalSystemDetail: any;

  isLinear = true;
  isNew = true;

  resourceAuth: any;


  constructor(
    private _vcRef: ViewContainerRef,
    private _activatedRoute: ActivatedRoute,
    private _backService: NavigationService,
    private _router: Router
  ) {

    this._backService.backAnnounced$.subscribe(data => {
      this._router.navigate(['/config/agent']);
    });
  }

  isDirty(): boolean {
    let dirty = false;

    dirty = this.generalInformationComponent.isDirty()
      || this.frequentQuestionsComponent.isDirty()
      ;

    /* if (dirty) {
      dirty = this.summaryComponent.isDirty()
    }*/

    return dirty;
  }

  getRef(): ViewContainerRef {
    return this._vcRef;
  }

  ngOnInit() {

    const authList = this._activatedRoute.snapshot.paramMap['authorization'];

    this.resourceAuth = new Object();

    if (authList) {
      authList.forEach(option => {
        this.resourceAuth[option] = true;
      });
    }

    console.log(this.resourceAuth);

    this.isNew = this._activatedRoute.snapshot.data.mode === 'create';

    if (this.isNew) {
      this.externalSystemDetail = {
        connections: []
      };
    } else {
      this.externalSystemDetail = this._activatedRoute.snapshot.data.externalSystemDetail;
    }

    let dataForWizard = this._activatedRoute.snapshot.data.dataForWizard;

    this.frequentQuestionsComponent.setDataForWizard(dataForWizard);
    //this.stepsComponent.setDataForWizard(dataForWizard);
    //this.entitiesComponent.setDataForWizard(dataForWizard);

    this.generalInformationComponent.buildForm(this.externalSystemDetail);
    this.frequentQuestionsComponent.buildForm(this.externalSystemDetail);
    //this.stepsComponent.buildForm(this.externalSystemDetail, this.resourceAuth);
    //this.entitiesComponent.buildForm(this.externalSystemDetail);
    //this.summaryComponent.buildForm(this.externalSystemDetail);

  }

  selectionChange($event) {
    //this.stepsComponent.selectConnection();
    //this.entitiesComponent.selectConnection();
  }

  onDeleteConnection($event) {
    //this.stepsComponent.deleteConnection($event);
    //this.entitiesComponent.deleteConnection($event);
  }


}
