package com.cr.common

import android.os.Handler
import android.os.Looper
import com.cr.cruav.CrApplication
import com.cr.cruav.R
import com.cr.dialog.DialogNormal
import com.cr.models.CompletionModel
import com.cr.models.ICompletion

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/9/20 14:27
 * 描述：对话框管理器
 */
class CrDialogManager {
    // define: 2023/9/20 主线程
    private var mainHandler = Handler(Looper.getMainLooper())

    /**
     * 单例必备
     */
    object InstanceHelper{
        var self = CrDialogManager()
    }

    companion object{
        /**
         * 获取单例
         * @return CrDialogManager
         */
        fun getInstance() = InstanceHelper.self
    }

    /**
     * 显示警告消息
     * @param warning String 消息内容
     */
    fun showWarning(warning: String) {
        mainHandler.post {
            val dialog = DialogNormal(CrApplication.getContext(), "警告", warning)
            dialog.setHeadImage(R.drawable.ico_waning)
            dialog.show()
        }
    }

    /**
     * 显示错误消息
     * @param error String 错误消息
     */
    fun showError(error: String) {
        mainHandler.post{
            val dialog = DialogNormal(CrApplication.getContext(), "错误", error)
            dialog.setHeadImage(R.drawable.ico_error)
            dialog.show()
        }
    }

    /**
     * 显示提示消息
     * @param information String 提示信息
     */
    fun showInformation(information: String) {
        mainHandler.post {
            val dialog = DialogNormal(CrApplication.getContext(), "提示", information)
            dialog.setHeadImage(R.drawable.ico_information)
            dialog.show()
        }
    }

    /**
     * 确认对话框
     * @param message String 显示的消息内容
     * @param actionButtonContent Array<String> 按钮文字数组 大小必须是2
     * @param callback ICompletion<String> 回调 可以传null
     */
     fun showConfirm(message:String,actionButtonContent:Array<String>,callback: ICompletion<String>?){
        if(actionButtonContent.size != 2) return
        mainHandler.post {
            var dialog = DialogNormal(CrApplication.getContext(),"确认",message)
            dialog.setHeadImage(R.drawable.ico_information)
            dialog.setButtonsText(actionButtonContent[0],actionButtonContent[1])
            dialog.setListener(object:DialogNormal.DialogNormalListener{
                // todo: 2023/8/15 确认操作
                override fun completion() {
                    callback?.let {
                        it.onCompletion(CompletionModel(true,""))
                    }
                }

                // todo: 2023/8/15 关闭操作
                override fun close() {
                    callback?.let {
                        it.onCompletion(CompletionModel(false,""))
                    }
                }

            })
            dialog.show()
        }
    }
}