/*
 * Copyright (C) 2021 StApps
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */
import {Component} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {Platform} from '@ionic/angular';
import moment from 'moment';
import {DateFormatPipe} from 'ngx-moment';
import {
  materialFade,
  materialManualFade,
  materialSharedAxisX,
} from '../../../animation/material-motion';
import {ScheduleProvider} from '../../calendar/schedule.provider';
import {CalendarViewComponent} from './calendar-view.component';
import {SwiperComponent} from 'swiper/angular';

/**
 * Component that displays the schedule
 */
@Component({
  selector: 'stapps-schedule-view',
  templateUrl: 'schedule-view.html',
  styleUrls: ['calendar-view.scss', 'schedule-view.scss'],
  animations: [materialFade, materialSharedAxisX, materialManualFade],
})
export class ScheduleViewComponent extends CalendarViewComponent {
  /**
   * Route Fragment
   */
  // @Override
  routeFragment = 'schedule/recurring';

  weekDates = Array.from({length: 7}).map(
    (_, i) => moment(new Date(2022, 0, 2 + i)), // 02/01/2022 is a Sunday
  );

  constructor(
    scheduleProvider: ScheduleProvider,
    activatedRoute: ActivatedRoute,
    datePipe: DateFormatPipe,
    platform: Platform,
  ) {
    super(scheduleProvider, activatedRoute, datePipe, platform);
  }

  syncSwiper(self: SwiperComponent, other: SwiperComponent) {
    other.swiperRef.slideTo(self.swiperRef.activeIndex);
  }

  /**
   * Determine displayed dates according to display size
   */
  // @Override
  determineDisplayDates() {
    // let's boldly assume that we at least display one day

    const out = [moment(moment.now()).startOf(this.layout.startOf)];
    for (let i = 1; i < this.layout.days; i++) {
      out.push(out[0].clone().add(i, 'day'));
    }

    this.displayDates = [out];

    // void this.mainSlides.slideTo(this.mode === 'schedule' ? 0 : 1, 0, false);
  }

  /**
   * Load events
   */
  // @Override
  async loadEvents(): Promise<number> {
    const dateSeries = await this.scheduleProvider.getDateSeries(
      this.uuids,
      ['P1W', 'P2W', 'P3W', 'P4W'],
      moment(moment.now()).startOf('week').toISOString(),
    );

    this.testSchedule = {};

    for (const series of dateSeries.dates) {
      const weekDays = Object.keys(
        // eslint-disable-next-line unicorn/no-array-reduce
        series.dates.reduce((accumulator, date) => {
          accumulator[moment(date).isoWeekday()] = true;
          return accumulator;
        }, {} as Record<number, true>),
      );

      for (const day of weekDays) {
        // fall back to default
        (this.testSchedule[day] ?? (this.testSchedule[day] = {}))[series.uid] =
          {
            dateSeries: series,
            time: {
              start: moment(series.dates[0]).hours(),
              duration: series.duration,
            },
          };
      }
    }

    return this.todaySlideIndex;
  }
}
