import { DateAdapter } from '@isa-ui/core/date';
import { EventEmitter, Input, Output, Directive } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

interface SetPropertyOptions {
  emit: boolean;
}

@Directive()
export abstract class Datepicker<TDate> {
  @Input()
  get selected() {
    return this.selectedSubject.value;
  }
  set selected(val: TDate) {
    this.setSelected(val, { emit: false });
  }
  @Output()
  selectedChange = new EventEmitter<TDate>();

  @Output()
  save = new EventEmitter<TDate>();

  @Input()
  get displayed() {
    return this.displayedSubject.value;
  }
  set displayed(val: TDate) {
    this.setDisplayed(val, { emit: false });
  }
  @Output()
  displayedChange = new EventEmitter<TDate>();

  @Input()
  get min() {
    return this.displayedSubject.value;
  }
  set min(val: TDate) {
    this.setMin(val);
  }
  @Input()
  get max() {
    return this.displayedSubject.value;
  }
  set max(val: TDate) {
    this.setMax(val);
  }

  @Input()
  get disabledDaysOfWeek() {
    return this.disableDayOfWeekSubject.value;
  }
  set disabledDaysOfWeek(value: number[]) {
    // 0 = Sunday, 1 = Monday, ... 6 = Saturday
    this.disableDayOfWeekSubject.next(value);
  }

  protected selectedSubject = new BehaviorSubject<TDate>(undefined);
  protected displayedSubject = new BehaviorSubject<TDate>(this.dateAdapter.today());
  protected minSubject = new BehaviorSubject<TDate>(undefined);
  protected maxSubject = new BehaviorSubject<TDate>(undefined);
  protected disableDayOfWeekSubject = new BehaviorSubject<number[]>(undefined);

  get selected$() {
    return this.selectedSubject.asObservable();
  }
  get displayed$() {
    return this.displayedSubject.asObservable();
  }
  get min$() {
    return this.minSubject.asObservable();
  }
  get max$() {
    return this.maxSubject.asObservable();
  }
  get disabledDaysOfWeek$() {
    return this.disableDayOfWeekSubject.asObservable();
  }

  constructor(private dateAdapter: DateAdapter<TDate>) {}

  nextMonth() {
    this.setDisplayed(this.dateAdapter.addCalendarMonths(this.displayed, 1));
  }

  previousMonth() {
    this.setDisplayed(this.dateAdapter.addCalendarMonths(this.displayed, -1));
  }

  setSelected(date: TDate, { emit: emitChanged }: SetPropertyOptions = { emit: true }) {
    if (!this.dateAdapter.isValid(date)) {
      this.selectedSubject.next(undefined);
      return;
    }

    if (this.dateAdapter.compareDate(this.selected, date) !== 0) {
      this.selectedSubject.next(date);
      if (emitChanged) {
        this.selectedChange.emit(date);
      }
    }
  }

  setDisplayed(date: TDate, { emit: emitChanged }: SetPropertyOptions = { emit: true }) {
    if (!this.dateAdapter.isValid(date)) {
      this.displayedSubject.next(undefined);
      return;
    }

    if (this.dateAdapter.compareDate(this.displayed, date) !== 0) {
      this.displayedSubject.next(date);
      if (emitChanged) {
        this.displayedChange.emit(date);
      }
    }
  }

  setMin(date: TDate) {
    if (!!!date && !this.dateAdapter.isValid(date)) {
      throw new Error('Date is not Valid.');
    }

    this.minSubject.next(date);
  }

  setMax(date: TDate) {
    if (!!!date && !this.dateAdapter.isValid(date)) {
      throw new Error('Date is not Valid.');
    }

    this.maxSubject.next(date);
  }

  onDestroy() {
    this.selectedSubject.complete();
    this.displayedSubject.complete();
    this.selectedChange.complete();
    this.displayedChange.complete();
    this.maxSubject.complete();
    this.minSubject.complete();
  }
}
