import {
  Component, OnInit, AfterContentInit, Output, EventEmitter,
  Input, OnDestroy, OnChanges, SimpleChanges, ViewChild
} from '@angular/core';
import { Store, select, StateObservable } from '@ngrx/store';
import { RPCClientError } from '@loafer/ng-rpc/protocol';
import {
  DiscoveryStartInfo,
  DiscoverZone,
  Zone,
  DiscoverPort,
  DiscoverService
} from '@overflow/commons-typescript/model/discovery';
import * as CIDR from 'ip-cidr';
import * as DiscoveredStore from '../../store/setting';
import { SettingSelector, DiscoverSelector } from '../../store';
import * as DiscoverStore from '../../store/discover';
import * as RegistStore from '../../store/regist';
import { Host } from '@overflow/commons-typescript/model/discovery';
import { Port } from '@overflow/commons-typescript/model/discovery';
import { Service } from '@overflow/commons-typescript/model/discovery';
import * as ProbeDetailStore from 'packages/probe/store';
import { Probe } from '@overflow/commons-typescript/model/probe';
import { TreeNode } from 'primeng/primeng';
import { ListSelector as ProbeListSelector } from 'packages/probe/store';
import * as ProbeListStore from 'packages/probe/store/list';
import { AuthSelector } from 'packages/member/store';
import { Domain } from '@overflow/commons-typescript/model/domain';
import { Subscription } from 'rxjs/Subscription';
import { ResultComponent } from './result/result.component';
import { ProbeSelectorComponent } from './probe-selector/probe-selector.component';
import { FilterComponent } from './filter/filter.component';

import { trigger, state, transition, style, animate } from '@angular/core';

@Component({
  selector: 'of-discovery-setting',
  templateUrl: './setting.component.html',
  animations: [
    trigger(
      'discoveryFilter', [
        state('summary', style({
          height: '70px',
          opacity: 0.9,
        })),
        state('full', style({
          height: '500px',
          opacity: 1,
        })),
        transition('* => *', animate('200ms ease-in')),
      ]),
    trigger(
      'result', [
        state('show', style({
          height: '300px',
          opacity: 1,
        })),
        state('hidden', style({
          height: '0px',
          opacity: 0,
        })),
        transition('* => *', animate('200ms ease-in')),
      ]
    )
  ]
})
export class SettingComponent implements OnInit, AfterContentInit, OnDestroy, OnChanges {

  @Input() visible: boolean;
  @Input() probe: Probe;
  @Output() close = new EventEmitter();
  private requestStart = false;
  private started = false;
  private filterData: DiscoverZone;

  private selectedProbe: Probe;
  private height: number;

  @ViewChild('probeSelectorComponent') probeSelectorComponent: ProbeSelectorComponent;
  @ViewChild('filterComponent') filterComponent: FilterComponent;
  @ViewChild('resultComponent') resultComponent: ResultComponent;

  constructor(
    private discoverStore: Store<DiscoverStore.State>,
  ) {
    this.height = window.innerHeight * 0.9;
  }

  ngOnInit() {
    this.selectedProbe = this.probe;
  }

  ngAfterContentInit() {
  }

  ngOnChanges(changes: SimpleChanges): void {
    if (changes['visible']) {
      const change = changes['visible'];
      if (!change.previousValue && change.currentValue) {
        this.initAll();
      } else if (change.previousValue && !change.currentValue) {
        this.destroyAll();
      }
    }
  }

  ngOnDestroy() {
    this.selectedProbe = null;
    this.started = false;
    this.close.emit();
    this.requestStart = false;
  }

  initAll() {
    this.ngOnInit();
    if (this.probeSelectorComponent) {
      this.probeSelectorComponent.ngOnInit();
    }
    if (this.filterComponent) {
      this.filterComponent.ngOnInit();
    }
    if (this.resultComponent) {
      this.resultComponent.ngOnInit();
    }
  }

  destroyAll() {
    this.ngOnDestroy();
    if (this.probeSelectorComponent) {
      this.probeSelectorComponent.ngOnDestroy();
    }
    if (this.filterComponent) {
      this.filterComponent.ngOnDestroy();
    }
    if (this.resultComponent) {
      this.resultComponent.ngOnDestroy();
    }
  }

  onProbeSelect(probe: Probe) {
    this.selectedProbe = probe;
  }

  onDiscoveryStart(discoverZone: DiscoverZone) {
    this.filterData = discoverZone;
    this.discoverStore.dispatch(new DiscoverStore.DiscoverZone(
      { probeID: this.selectedProbe.probeKey, discoverZone: discoverZone }));

    setTimeout(() => {
      this.started = true;
      // this.requestStart = false;
    });
  }

  onCancel() {
    this.destroyAll();
  }

  onSave() {
    this.resultComponent.save();
  }

  onStop() {
    this.started = false;
  }

  onSummaryClick() {
    if (this.started) {
      return;
    }
    this.requestStart = false;
  }

}
