import { Component, Input, OnInit, Output, EventEmitter, ViewChildren, QueryList } from '@angular/core';
import { Zone, Host } from '@overflow/model/discovery';
import { AutoHeightDirective } from '@overflow/commons/ui/directive/auto-height.directive';

const IPCIDR = require('ip-cidr');

@Component({
  selector: 'app-zone-detail',
  templateUrl: './zone-detail.component.html',
  styleUrls: ['./zone-detail.component.scss'],
})
export class ZoneDetailComponent implements OnInit {

  @Input() zone: Zone;
  @Output() otherHostSelect = new EventEmitter<Host>();

  @ViewChildren(AutoHeightDirective) autoHeightDirectives: QueryList<AutoHeightDirective>;

  ipRange: string;

  constructor(
  ) {
  }

  ngOnInit(): void {
    if (!this.zone) {
      return;
    }

    const cidr = new IPCIDR(this.zone.network);
    this.ipRange = cidr.start() + ' - ' + cidr.end();
  }

  hostSelected(host: Host) {
    this.otherHostSelect.emit(host);
  }

  onTabViewChange($event) {
    const index = String($event.index);
    this.autoHeightDirectives.map(item => {
      if (item.key === index) {
        item.forceCalculate();
      }
    });
  }

}
