import { Component, Input, OnInit, ViewChild, EventEmitter, Output } from '@angular/core';
import { WeekSchedulerFormComponent } from '../week-scheduler-form/week-scheduler-form.component';
import * as moment_ from 'moment';
import { CalendarExceptionGridComponent } from '../calendar-exception-grid/calendar-exception-grid.component';
const moment = moment_;
@Component({
  selector: 'byte-calendar-config-layout',
  templateUrl: './calendar-config-layout.component.html',
  styleUrls: ['./calendar-config-layout.component.scss']
})
export class CalendarConfigLayoutComponent implements OnInit {
  @Input() templateDefinition;
  @Input() weekConfig: Array<any> = [];
  @Input() mode: string;
  @Input() calendarId: string;
  @Output() dirtyChange: EventEmitter<any> = new EventEmitter<any>();

  constructor() { }

  @ViewChild('weekScheduler', { static: false }) weekSchedulerComponent: WeekSchedulerFormComponent;
  @ViewChild('calendarException', { static: false }) calendarExceptionGridComponent: CalendarExceptionGridComponent;
  ngOnInit() {
  }

  reloadweekSchedulerForm(mode: string) {
    if (this.weekSchedulerComponent) {
      return this.weekSchedulerComponent.reLoadFields(mode);
    }
  }

  getWeekFormValidator(): boolean {
    let formValidator = false;
    if (this.weekSchedulerComponent.monValidator && this.weekSchedulerComponent.tueValidator
      && this.weekSchedulerComponent.wedValidator && this.weekSchedulerComponent.thuValidator
      && this.weekSchedulerComponent.friValidator && this.weekSchedulerComponent.satValidator
      && this.weekSchedulerComponent.sunValidator) {
      formValidator = true;
    }
    return formValidator;
  }

  getWeeklyConfig() {
    const weekSchedulerList = [];
    const sunConf = 'sunConf';
    if (this.weekSchedulerComponent) {
      if (this.weekSchedulerComponent.form.value[sunConf]) {
        const sunFrom = moment(this.weekSchedulerComponent.form.value['sunFrom']);
        const sunTo = moment(this.weekSchedulerComponent.form.value['sunTo']);
        weekSchedulerList.push({
          'id': this.weekSchedulerComponent.getValueId(0),
          'dayOfWeek': 1,
          'from': sunFrom.format('HH:mm:ssZ'),
          'to': sunTo.format('HH:mm:ssZ'),
          'calendarID': this.calendarId
            
          
        });
      }
      if (!this.weekSchedulerComponent.form.value['sunConf']) {
        const sunFrom = moment(this.weekSchedulerComponent.form.value['sunFrom']);
        const sunTo = moment(this.weekSchedulerComponent.form.value['sunTo']);
        weekSchedulerList.push({
          'id': this.weekSchedulerComponent.getValueId(0),
          'dayOfWeek': 1,
          'from': null,
          'to': null,
          'calendarID': this.calendarId
        });
      }
      if (this.weekSchedulerComponent.form.value['monConf']) {
        const monFrom = moment(this.weekSchedulerComponent.form.value['monFrom']);
        const monTo = moment(this.weekSchedulerComponent.form.value['monTo']);
        weekSchedulerList.push({
          'id': this.weekSchedulerComponent.getValueId(1),
          'dayOfWeek': 2,
          'from': monFrom.format('HH:mm:ssZ'),
          'to': monTo.format('HH:mm:ssZ'),
          'calendarID': this.calendarId
        });
      }
      if (!this.weekSchedulerComponent.form.value['monConf']) {
        const monFrom = moment(this.weekSchedulerComponent.form.value['monFrom']);
        const monTo = moment(this.weekSchedulerComponent.form.value['monTo']);
        weekSchedulerList.push({
          'id': this.weekSchedulerComponent.getValueId(1),
          'dayOfWeek': 2,
          'from': null,
          'to': null,
          'calendarID': this.calendarId
        });
      }
      if (this.weekSchedulerComponent.form.value['tueConf']) {
        const tueFrom = moment(this.weekSchedulerComponent.form.value['tueFrom']);
        const tueTo = moment(this.weekSchedulerComponent.form.value['tueTo']);
        weekSchedulerList.push({
          'id': this.weekSchedulerComponent.getValueId(2),
          'dayOfWeek': 3,
          'from': tueFrom.format('HH:mm:ssZ'),
          'to': tueTo.format('HH:mm:ssZ'),
          'calendarID': this.calendarId
        });
      }
      if (!this.weekSchedulerComponent.form.value['tueConf']) {
        const tueFrom = moment(this.weekSchedulerComponent.form.value['tueFrom']);
        const tueTo = moment(this.weekSchedulerComponent.form.value['tueTo']);
        weekSchedulerList.push({
          'id': this.weekSchedulerComponent.getValueId(2),
          'dayOfWeek': 3,
          'from': null,
          'to': null,
          'calendarID': this.calendarId
        });
      }
      if (this.weekSchedulerComponent.form.value['wedConf']) {
        const wedFrom = moment(this.weekSchedulerComponent.form.value['wedFrom']);
        const wedTo = moment(this.weekSchedulerComponent.form.value['wedTo']);
        weekSchedulerList.push({
          'id': this.weekSchedulerComponent.getValueId(3),
          'dayOfWeek': 4,
          'from': wedFrom.format('HH:mm:ssZ'),
          'to': wedTo.format('HH:mm:ssZ'),
          'calendarID': this.calendarId
        });
      }
      if (!this.weekSchedulerComponent.form.value['wedConf']) {
        const wedFrom = moment(this.weekSchedulerComponent.form.value['wedFrom']);
        const wedTo = moment(this.weekSchedulerComponent.form.value['wedTo']);
        weekSchedulerList.push({
          'id': this.weekSchedulerComponent.getValueId(3),
          'dayOfWeek': 4,
          'from': null,
          'to': null,
          'calendarID': this.calendarId
        });
      }
      if (this.weekSchedulerComponent.form.value['thuConf']) {
        const thuFrom = moment(this.weekSchedulerComponent.form.value['thuFrom']);
        const thuTo = moment(this.weekSchedulerComponent.form.value['thuTo']);
        weekSchedulerList.push({
          'id': this.weekSchedulerComponent.getValueId(4),
          'dayOfWeek': 5,
          'from': thuFrom.format('HH:mm:ssZ'),
          'to': thuTo.format('HH:mm:ssZ'),
          'calendarID': this.calendarId
        });
      }
      if (!this.weekSchedulerComponent.form.value['thuConf']) {
        const thuFrom = moment(this.weekSchedulerComponent.form.value['thuFrom']);
        const thuTo = moment(this.weekSchedulerComponent.form.value['thuTo']);
        weekSchedulerList.push({
          'id': this.weekSchedulerComponent.getValueId(4),
          'dayOfWeek': 5,
          'from': null,
          'to': null,
          'calendarID': this.calendarId
        });
      }
      if (this.weekSchedulerComponent.form.value['friConf']) {
        const friFrom = moment(this.weekSchedulerComponent.form.value['friFrom']);
        const friTo = moment(this.weekSchedulerComponent.form.value['friTo']);
        weekSchedulerList.push({
          'id': this.weekSchedulerComponent.getValueId(5),
          'dayOfWeek': 6,
          'from': friFrom.format('HH:mm:ssZ'),
          'to': friTo.format('HH:mm:ssZ'),
          'calendarID': this.calendarId
        });
      }
      if (!this.weekSchedulerComponent.form.value['friConf']) {
        const friFrom = moment(this.weekSchedulerComponent.form.value['friFrom']);
        const friTo = moment(this.weekSchedulerComponent.form.value['friTo']);
        weekSchedulerList.push({
          'id': this.weekSchedulerComponent.getValueId(5),
          'dayOfWeek': 6,
          'from': null,
          'to': null,
          'calendarID': this.calendarId
        });
      }
      if (this.weekSchedulerComponent.form.value['satConf']) {
        const satFrom = moment(this.weekSchedulerComponent.form.value['satFrom']);
        const satTo = moment(this.weekSchedulerComponent.form.value['satTo']);
        weekSchedulerList.push({
          'id': this.weekSchedulerComponent.getValueId(6),
          'dayOfWeek': 7,
          'from': satFrom.format('HH:mm:ssZ'),
          'to': satTo.format('HH:mm:ssZ'),
          'calendarID': this.calendarId
        });
      }
      if (!this.weekSchedulerComponent.form.value['satConf']) {
        const satFrom = moment(this.weekSchedulerComponent.form.value['satFrom']);
        const satTo = moment(this.weekSchedulerComponent.form.value['satTo']);
        weekSchedulerList.push({
          'id': this.weekSchedulerComponent.getValueId(6),
          'dayOfWeek': 7,
          'from': null,
          'to': null,
          'calendarID': this.calendarId
        });
      }
    }
    return weekSchedulerList;
  }
  loadWeek() {
    return this.weekSchedulerComponent.ngOnInit();
  }
  onTabChange(event) {
    if (event === 1) {
      // this.loadingHistory = true;
      // this.requestNipService.getHistoryData(this.record['codigo']).subscribe(response => {
      //   this.dataSource = new MatTableDataSource<HistoryModel>(response);
      //   this.loadingHistory = false;
      // }, error => {
      //   this.dataSource = new MatTableDataSource<HistoryModel>([]);
      //   this.loadingHistory = false;
      // });
    }
  }

  getCalendarException() {
    //console.log(this.calendarExceptionGridComponent.getCalendarExceptionList());
    return this.calendarExceptionGridComponent.getCalendarExceptionList();
  }

  dirtyChangeForm() {
    this.dirtyChange.emit(true);
  }
}
