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 { NavigationService } from '@xdf/layouts';
import { IDirty } from '@xdf/gallery';
import { CaFrequentQuestionsComponent } from '../components/ca-frequent-questions/ca-frequent-questions.component';
import { MatStepper } from '@angular/material';
import { CaDeploymentChannelsComponent } from '../components/ca-deployment-channels/ca-deployment-channels.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;

  @ViewChild(CaDeploymentChannelsComponent, { static: true })
  deploymentChannelsComponent: CaDeploymentChannelsComponent;

  @ViewChild('stepper', { static: true }) stepper: MatStepper;

  agentDetail: 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(['/configuration/agent']);
    });
  }

  isDirty(): boolean {
    let dirty = false;

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

    if (dirty) {
      dirty = this.deploymentChannelsComponent.isDirty()
    }

    return dirty;
  }

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

  ngOnInit() {

    this.stepper.selectedIndex = 0;

    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.agentDetail = {
      };
    } else {
      this.agentDetail = this._activatedRoute.snapshot.data.agentDetail;
    }


    this.generalInformationComponent.buildForm(this.agentDetail);
    this.frequentQuestionsComponent.buildForm(this.agentDetail);
    this.deploymentChannelsComponent.buildForm(this.agentDetail);

  }

}
