/*
 * Copyright (C) 2022 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, OnDestroy, OnInit} from '@angular/core';
import {SCSearchFilter, SCThingType} from '@openstapps/core';
import {ModalController} from '@ionic/angular';
import {DataRoutingService} from '../../../data/data-routing.service';
import {DataDetailComponent} from '../../../data/detail/data-detail.component';
import {Subscription} from 'rxjs';

/**
 * TODO
 */
@Component({
  selector: 'modal-event-creator',
  templateUrl: 'modal-event-creator.html',
  styleUrls: ['modal-event-creator.scss'],
})
export class ModalEventCreatorComponent implements OnInit, OnDestroy {
  subscriptions: Subscription[] = [];

  constructor(readonly modalController: ModalController, readonly dataRoutingService: DataRoutingService) {}

  ngOnInit() {
    this.subscriptions.push(
      this.dataRoutingService.itemSelectListener().subscribe(async item => {
        const modal = await this.modalController.create({
          component: DataDetailComponent,
          componentProps: {
            isModal: true,
            inputItem: item,
          },
          canDismiss: true,
        });
        return modal.present();
      }),
    );
  }

  ngOnDestroy() {
    for (const subscription of this.subscriptions) subscription.unsubscribe();
  }

  /**
   * Forced filter
   */
  filter: SCSearchFilter = {
    arguments: {
      field: 'type',
      value: SCThingType.AcademicEvent,
    },
    type: 'value',
  };
}
