import {
  AfterContentInit,
  Component,
  EventEmitter,
  Input,
  Output,
  OnInit,
} from '@angular/core';

@Component({
  selector: 'of-ip-input',
  templateUrl: './ip-input.component.html',
})
export class IpInputComponent implements OnInit, AfterContentInit {

  first: string;
  second: string;
  third: string;
  fourth: string;

  @Input() hostIp: string;
  @Input() title: string;
  @Input() fixed: number;
  @Output() inputIp = new EventEmitter();

  constructor(
  ) {
  }

  ngOnInit() {
    if (this.hostIp !== '' && this.hostIp !== null && this.hostIp !== undefined) {
      const tempIp = this.hostIp.split('.');

      this.first = tempIp[0];
      this.second = tempIp[1];
      this.third = tempIp[2];
      this.fourth = tempIp[3];
    }
  }

  ngAfterContentInit() {
  }

  onIpInput(event) {
    if (
      (this.first !== '' && this.first !== undefined) &&
      (this.second !== '' && this.second !== undefined) &&
      (this.third !== '' && this.third !== undefined) &&
      (this.fourth !== '' && this.fourth !== undefined) ) {
      event.value = this.first + '.' + this.second + '.' + this.third + '.' + this.fourth;
      this.inputIp.emit(event);
    } else {
      return;
    }
  }

}