import { Injectable } from '@angular/core';
import { AlertController, ToastController } from '@ionic/angular';
import { AlertOptions, ToastOptions } from '@ionic/core';

@Injectable({ providedIn: 'root' })
export class AppNotification {

    /**
     * Creates an instance of Notification.
     * @param {AlertController} alertCtrl
     * @param {ToastController} toastCtrl
     * @memberof Notification
     */
    constructor(private alertCtrl: AlertController, private toastCtrl: ToastController) { }

    /**
     * 对话框
     *
     * @param {string} title
     * @param {string} [message]
     * @memberof Notification
     */
    public async alert(title: string, message?: string, config: AlertOptions = {}): Promise<void> {
        const alert = await this.alertCtrl.create({
            header: title,
            message,
            buttons: ['确认'],
            ...config
        });
        alert.present();
    }

    /**
     * 对话框
     *
     * @param {string} title
     * @param {string} [message]
     * @returns {Promise<boolean>}
     * @memberof Notification
     */
    public async confirm(title: string, message?: string, config: AlertOptions = {}): Promise<boolean> {
        const alert = await this.alertCtrl.create({
            header: title,
            message,
            buttons: [
                {
                    text: '取消',
                    role: 'cancel',
                    cssClass: 'secondary'
                },
                {
                    text: '确认',
                    role: 'OK'
                }
            ],
            ...config
        });
        alert.present();
        const res = await alert.onWillDismiss();
        return Object.is(res.role, 'OK');
    }

    /**
     * 提示信息
     *
     * @param {string} message
     * @param {string} [type='light']
     * @param {('top' | 'bottom' | 'middle')} [position='top']
     * @returns {Promise<void>}
     * @memberof Notification
     */
    public async info(message: string, type: string = 'light', position: 'top' | 'bottom' | 'middle' = 'top', config: ToastOptions = {}): Promise<void> {
        const toast = await this.toastCtrl.create({
            message,
            position,
            duration: 3000,
            color: type,
            ...config
        });
        toast.present();
    }

    /**
     * 提示信息
     *
     * @param {string} message
     * @param {('top' | 'bottom' | 'middle')} [position='top']
     * @returns {Promise<void>}
     * @memberof Notification
     */
    public warning(message: string, position?: 'top' | 'bottom' | 'middle', config?: ToastOptions): Promise<void> {
        return this.info(message, 'warning', position, config);
    }

    /**
     * 成功提示
     *
     * @param {string} message
     * @param {('top' | 'bottom' | 'middle')} [position='top']
     * @returns {Promise<void>}
     * @memberof Notification
     */
    public success(message: string, position?: 'top' | 'bottom' | 'middle', config?: ToastOptions): Promise<void> {
        return this.info(message, 'success', position, config);
    }

    /**
     * 错误提示
     *
     * @param {string} message
     * @param {('top' | 'bottom' | 'middle')} [position='top']
     * @returns {Promise<void>}
     * @memberof Notification
     */
    public error(message: string, position?: 'top' | 'bottom' | 'middle', config?: ToastOptions): Promise<void> {
        return this.info(message, 'danger', position, config);
    }

    /**
     * Ionic 对话框
     *
     * @returns {AlertController}
     * @memberof Notification
     */
    public getAlertCtrl(): AlertController {
        return this.alertCtrl;
    }

    /**
     * Ionic 提示框
     *
     * @returns {ToastController}
     * @memberof Notification
     */
    public getToastCtrl(): ToastController {
        return this.toastCtrl;
    }

}