import {ElLoading, ElMessage, ElMessageBox, ElNotification, type MessageBoxData} from 'element-plus'
import type {LoadingInstance} from "element-plus/es/components/loading/src/loading";

export default class MessageUtils {

    // 顶部消息框 ---- 成功
    static successMessage(msg: string) : void {
        ElMessage({
            message: msg,
            type: 'success',
        });
    }

    // 顶部消息框 ---- 警告
    static warningMessage(msg: string) : void {
        ElMessage({
            message: msg,
            type: 'warning',
        });
    }


    // 通知
    static showNotification(title: string, message: string, type: 'success'|'warning'|'info'|'error', duration: number) {
        switch (type) {
            case 'success':
                this.showSuccessNotification(title, message, duration);
                break;
            case 'warning':
                this.showWarningNotification(title, message, duration);
                break;
            case 'info':
                this.showInfoNotification(title, message, duration);
                break;
            case 'error':
                this.showErrorNotification(title, message, duration);
                break;
            default:
                this.showInfoNotification(title, message, duration);
        }
    }

    static showSuccessNotification(title: string, message: string, duration: number) {
        ElNotification({
            title: title,
            message: message,
            type: 'success',
            duration: duration
        });
    }

    static showWarningNotification(title: string, message: string, duration: number) {
        ElNotification({
            title: title,
            message: message,
            type: 'warning',
            duration: duration
        });
    }

    static showInfoNotification(title: string, message: string, duration: number) {
        ElNotification({
            title: title,
            message: message,
            type: 'info',
            duration: duration
        });
    }

    static showErrorNotification(title: string, message: string, duration: number) {
        ElNotification({
            title: title,
            message: message,
            type: 'error',
            duration: duration
        });
    }

    // 顶部消息框 ---- 错误
    static errMessage(msg: string) : void {
        ElMessage({
            message: msg,
            grouping: true,
            type: 'error',
        });
    }

    // 顶部消息框 ---- 普通
    static infoMessage(msg: string) : void {
        ElMessage(msg);
    }

    // 消息弹窗 ---- 普通
    static infoMessageBox(msg: string, title: string) : void {
        ElMessageBox.alert(msg, title).then(r => {}).catch(c => {});
    }

    // 成功的消息弹窗 ---- 成功
    static successMessageBox(msg: string,  title: string) : void {
        ElMessageBox.alert(msg, title, {
                type: 'success'
            }).then(r => {}).catch(c => {});
    }

    // 警告的消息弹窗 ---- 警告
    static warningMessageBox(msg: string,  title: string = '警告') : void {
        ElMessageBox.alert(msg, title, {
            type: 'warning'
        }).then(r => {}).catch(c => {});
    }

    // 错误的消息弹窗 ---- 错误
    static errMessageBox(msg: string,  title: string = '发生异常') : void {
        ElMessageBox.alert(msg, title, {
            type: 'error'
        }).then(r => {}).catch(c => {});
    }

    // 确认的消息弹窗
    static confirmMessageBox(msg: string, title: string, confirmButtonText: string = '确认', cancelButtonText: string = '取消') : Promise<void | MessageBoxData> {
        return ElMessageBox.confirm(msg, title, {
                confirmButtonText: confirmButtonText,
                cancelButtonText: cancelButtonText,
                type: 'warning',
            }
        );
    }

    // 加载框
    static loading(msg: string = "请稍后") : LoadingInstance {
        return ElLoading.service({
            lock: true,
            text: msg,
            background: 'rgba(0, 0, 0, 0.7)',
        })
    }

}