import {Component, inject} from '@angular/core'
import {App} from '@capacitor/app'
import {ToastController} from '@ionic/angular'

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss'],
})
export class AppComponent {

  #backButtonPresses = 0
  #toastController = inject(ToastController)


  constructor() {
    App.addListener('backButton', data => {
      this.#backButtonPresses++

      if (this.#backButtonPresses === 1) {
        this.#presentToast().then()
      } else {
        App.exitApp().then()
      }
    })
  }

  async #presentToast(): Promise<void> {
    const toast = await this.#toastController.create({
      message: 'Press the back button again to close the app.',
      duration: 2000,
    })
    await toast.present()

    await toast.onDidDismiss()
    this.#backButtonPresses--
  }
}
