import {Component, Input, OnInit} from '@angular/core';
import {BaseFilterCellComponent, FilterService} from '@progress/kendo-angular-grid';
import {CompositeFilterDescriptor, FilterDescriptor} from '@progress/kendo-data-query';
import {debounce} from 'ts-debounce';

@Component({
  selector: 'app-date-filter',
  templateUrl: './date-filter.component.html',
  styleUrls: ['./date-filter.component.scss']
})
export class DateFilterComponent extends BaseFilterCellComponent  implements OnInit {
  @Input() filter: CompositeFilterDescriptor;
  @Input() data: Date;
  @Input() fieldName = '';
  @Input() start = 0;
  @Input() end = 0;

  public date: Date;
  @Input() public range = 0;
  public rangeData = [];

  public debouncedDoFilter = debounce(this.doFilter, 500);
  constructor(filterService: FilterService) { super(filterService); }

  ngOnInit(): void {
    this.rangeData = this.rangeBetween(this.start, this.end);
    if ( this.range < this.start){
      this.range = this.start;
    }
    if ( this.range > this.end ){
      this.range = this.end;
    }
    this.date = null;
  }

  private rangeBetween(start: number, end: number): number[] {
    let ret = [];
    if (start > end) {
      ret = new Array(start - end + 1);
      for (let i = 0; i < ret.length; i++, start--) {
        ret[i] = start;
      }
      return ret;
    } else {
      ret = new Array(end - start + 1);
      for (let j = 0; j < ret.length; j++, start++){
        ret[j] = start;
      }
      return ret;
    }
  }

  public onDateChange(d: Date): void {
    console.log(this.date);
    this.debouncedDoFilter().then();
  }
  public onRangeChange(r: number): void {
    this.debouncedDoFilter().then();
  }

  private doFilter(): void {
    if ( this.date === null || this.date.getFullYear() < 1900){
      this.applyFilter(this.removeFilter(this.fieldName));
      return;
    }

    if ( this.range === 0 ){
      this.buildSingleFilter();
    }else{
      this.buildRangeFilter();
    }
  }

  private buildSingleFilter(): void {
    if ( this.date < new Date('1900-01-01') ){
      this.applyFilter(this.removeFilter(this.fieldName));
      return;
    }else{
      this.applyFilter(this.updateFilter({
        field: this.fieldName,
        operator: 'eq',
        value: this.date
      }));
    }
  }

  private buildRangeFilter(): void {
    const fs: FilterDescriptor[] = [];
    const start: Date = new Date(this.date); start.setDate(this.date.getDate() - this.range);
    const end: Date = new Date(this.date); end.setDate(this.date.getDate() + this.range);
    fs.push({
      field: this.fieldName,
      operator: 'gte',
      value: start.getTime() / 1000
    });

    fs.push({
      field: this.fieldName,
      operator: 'lte',
      value: end.getTime() / 1000
    });
    this.removeFilter(this.fieldName);
    const root: CompositeFilterDescriptor = this.filter || { logic: 'and',
      filters: fs,
    };
    if (fs.length) {
      root.filters.push(...fs);
    }
    this.filterService.filter(root);
  }

}
