import { Component, OnInit, ViewChild, Input, AfterContentChecked, AfterViewInit } from '@angular/core';
import { 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 { 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: 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;

  // dirty acumulado
  generalDirty = false;

  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);

  }

  updateDirtyStatus(event: boolean) {
    this.generalDirty = this.generalDirty || event;
  }

}
