import { Component, Input, OnInit, Output, ViewChild, ViewContainerRef, EventEmitter } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { InitCommonsService, NotificationService, NotificationType } from '@xdf/commons';
import { ColumnTemplate, ConfirmationDialogService, CrudDirective, DynaDataService, GridTemplate } from '@xdf/gallery';
import { AuthorizationService } from '@xdf/security';
import { CalendarExceptionDialogService } from '../../calendar-form/services/calendar-exception-dialog.service';
import { CalendarExceptionService } from '../../services/calendar-exception.service';
import { CalendarExceptionTableComponent } from './views/calendar-exception-table/calendar-exception-table.component';
const CALENDAR_EXCEPTION = {
  'H': 'calendar-exception.type.h',
  'N': 'calendar-exception.type.n',
  'W': 'calendar-exception.type.w'
};
const FREQUENCY = {
  'U': 'calendar-exception.frequency.u',
  'W': 'calendar-exception.frequency.w',
  'Y': 'calendar-exception.frequency.y',
  'R': 'calendar-exception.frequency.r'
};

@Component({
  selector: 'byte-calendar-exception-grid',
  templateUrl: './calendar-exception-grid.component.html',
  styleUrls: ['./calendar-exception-grid.component.scss']
})
export class CalendarExceptionGridComponent extends CrudDirective implements OnInit{
  @Input() templateDefinition;
  @Input() calendarID: string;
  @Input() mode = '';
  @Output() dirtyChange: EventEmitter<any> = new EventEmitter<any>();

  @ViewChild('calendarException', { static: false }) calendarExceptionTableComponent: CalendarExceptionTableComponent;
  columnTemplateArray: ColumnTemplate[];
  template: GridTemplate;

  title: string;
  description: string;
  calendarExceptionList;
  calendarExceptionGrid = [];
  sortColumn;
  sortDirection;

  innerContent: any;

  constructor(
    protected route: ActivatedRoute,
    protected router: Router,
    protected translate: TranslateService,
    protected confirmationDialogService: ConfirmationDialogService,
    protected vcRef: ViewContainerRef,
    protected notificationService: NotificationService,
    protected authorizationService: AuthorizationService,
    protected init: InitCommonsService,
    public dynaDataService: CalendarExceptionService,
    private calendarExceptionDialogService: CalendarExceptionDialogService,
  ) {

    super(route, router, dynaDataService, confirmationDialogService, vcRef, notificationService, authorizationService, init);
  }

  ngOnInit() {
    const calendarExceptionDataKey = 'calendarExceptionData';
    this.calendarExceptionList = this.route.snapshot.data[calendarExceptionDataKey];
    this.calendarExceptionGrid=this.calendarExceptionList;
    this.title = this.templateDefinition.title;
    this.description = this.templateDefinition.titleDesc;
    this.template = new GridTemplate(this.templateDefinition);
    this.innerContent = this.route.snapshot.data.innerTemplate;
    if (this.innerContent === 'none') {
      this.template.expandRow = false;
      this.innerContent = null;
    }
    if (this.template.expandRow && !this.innerContent) {
      const titleError = this.translate.instant('title.error');
      const error = this.translate.instant('inner.content.notfound.error');
      this.notificationService.showMessage(error, titleError, NotificationType.warning);
      this.template.expandRow = false;
    }

    this.sortColumn = this.template.sortColumnDefault;
    this.sortDirection = this.template.sortColumnDirection;

    this.columnTemplateArray = this.template.columnTemplateArray;
  }

  protected loadSettings() {
    this.title = this.templateDefinition.title;
    this.template = new GridTemplate(this.templateDefinition);

    this.innerContent = this.route.snapshot.data.innerTemplate;
    if (this.innerContent === 'none') {
      this.template.expandRow = false;
      this.innerContent = null;
    }
    if (this.template.expandRow && !this.innerContent) {
      const titleError = this.translate.instant('title.error');
      const error = this.translate.instant('inner.content.notfound.error');
      this.notificationService.showMessage(error, titleError, NotificationType.warning);
      this.template.expandRow = false;
    }

    this.sortColumn = this.template.sortColumnDefault;
    this.sortDirection = this.template.sortColumnDirection;

    this.columnTemplateArray = this.template.columnTemplateArray;
  }

  getCalendarExceptionList() {
    if (this.calendarExceptionTableComponent) {
      return this.calendarExceptionTableComponent.calendarExceptionGrid;
    }
    return this.calendarExceptionGrid;
  }

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

  onNewRecord() {
    this.calendarExceptionDialogService.loadComponent(this.vcRef, 'calendar_exception_new', undefined, 'new').subscribe(result => {
      const record = 'record';
      if (result && result['success']) {
        result[record].calendarException = this.translate.instant(CALENDAR_EXCEPTION[result[record].calendarExceptionType]);
        result[record].frequency = this.translate.instant(FREQUENCY[result[record].frequencyType]);
        //this.calendarExceptionList.push(result[record]);
        this.calendarExceptionGrid.push(result[record]);
        this.dirtyChange.emit(true);
      }
    });
  }

}
