import {Component, inject, OnInit} from '@angular/core'
import {SettingService} from '../services/setting.service'
import {ToastController} from '@ionic/angular'

@Component({
  selector: 'app-setting',
  templateUrl: './setting.page.html',
  styleUrls: ['./setting.page.scss'],
})
export class SettingPage implements OnInit {

  showNotifications: boolean = false

  settingService = inject(SettingService)
  #toastController = inject(ToastController)

  constructor() {
  }

  ngOnInit() {
    this.showNotifications = this.settingService.notificationsAreEnabled()
  }

  async toggleShowNotification(): Promise<void> {
    const old = !this.showNotifications
    this.showNotifications = await this.settingService.toggleShowNotifications()
    if (!old && !this.showNotifications) {
      await this.presentNoPermissionToast()
    }
  }

  async changeHandlerMinutes(minutes: string | undefined | null | number): Promise<void> {
    if (minutes) {
      await this.settingService.setTimeBeforeDeadline(Number(minutes))
    }
  }

  /**
   * Show a toast that informs the user that an image has been deleted.
   */
  async presentNoPermissionToast() {
    const toast = await this.#toastController.create({
      message: `Can't enable notifications. Please give permission or allow the application to ask for permission.`,
      position: 'bottom',
      duration: 2000,
      buttons: [
        {
          side: 'end',
          text: 'Close',
          handler: () => this.#toastController.dismiss(),
        },
      ],
    })
    await toast.present()
  }
}
