import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';
import { DateAdapter } from '../../components/calendar/providers';
import { TaskType, Task, CalendarItem } from '../../defs';
import { Filter } from '../../../filter';
import { TaskCalendarFacade } from 'apps/sales/src/app/store/branch';
import { Router } from '@angular/router';
import { ModalService } from 'apps/sales/src/app/core/overlay/modal-service';
import { EditTaskComponent } from '../../modals/edit-task';

@Component({
  selector: 'app-tasks-view-component',
  templateUrl: './tasks-view.component.html',
  styleUrls: ['./tasks-view.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TasksViewComponent implements OnInit {
  currentDate = this.dateAdapter.getDate(new Date());
  currentWeek = this.dateAdapter.getCalendarWeek(this.currentDate.year, this.currentDate.month, this.currentDate.date);
  currentHour = new Date().getHours();

  type: TaskType;

  filters: Filter[] = [];

  tasks: Task[];
  calendarItems: CalendarItem[];

  constructor(private dateAdapter: DateAdapter, public store: TaskCalendarFacade, private router: Router, private modal: ModalService) {}

  ngOnInit() {
    this.store.getTasks();
  }

  private setDateOnNavigationFromCalendar(date: { year: number; month: number; date: number }) {
    this.currentDate = date;
    this.currentWeek = this.dateAdapter.getCalendarWeek(date.year, date.month, date.date);
  }

  private update() {
    const week = this.dateAdapter.getDatesForCalendarWeek(this.currentDate.year, this.currentWeek);
  }

  handleDateChange(d) {}

  openDetails(task: Task) {
    const ref = this.modal.open(EditTaskComponent, { data: task.id });

    // close ref on navigation
  }

  navigateToCalendar() {
    // TODO breadcrumbs to store, current route
    this.router.navigate(['task-calendar', 'calendar']);
  }
}
