package com.cr.dialog

import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import com.cr.cruav.R

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/3/31 15:42
 * 描述：普通对话框
 */
class DialogNormal : Dialog, View.OnClickListener {
    /**
     * 对外接口
     */
    interface DialogNormalListener {
        /**
         * 完成
         */
        fun completion()

        /**
         * 关闭
         */
        fun close()
    }

    // define: 2023/3/31 监听接口
    private var listener: DialogNormalListener? = null

    // define: 2023/3/31 定义控件
    var btnCompletion: Button? = null
    var btnClose: Button? = null
    var lblTitle: TextView? = null
    var lblMessage: TextView? = null

    /**
     * 初始化
     * @param context Context
     * @constructor
     */
    constructor(context: Context) : this(context, 0)

    /**
     * 初始化
     * @param context Context 上下文
     * @param themeResId Int 主题Id
     * @constructor
     */
    constructor(context: Context, themeResId: Int) : super(context, R.style.Dialog) {
        init()
    }

    /**
     * 初始化
     * @param context Context 上下文
     * @param title String 标题
     * @param message String 消息内容
     * @constructor
     */
    constructor(context: Context, title:String, message:String): super(context, R.style.Dialog){
        init()
        lblTitle?.text = title
        lblMessage?.text = message
    }

    /**
     * 初始化
     * @param context Context 上下文
     * @param title String 主题
     * @param message String 消息内容
     * @param listener DialogNormalListener 消息监听
     * @constructor
     */
    constructor(context: Context, title:String, message:String,listener: DialogNormalListener): super(context, R.style.Dialog) {
        init()
        lblTitle?.text = title
        lblMessage?.text = message
        this.listener = listener
    }
    /**
     * 初始化
     */
    private fun init(){
        setContentView(R.layout.dig_normal)
        window?.setGravity(Gravity.CENTER)
        window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        // todo: 2023/3/31 禁止点击对话框外部关闭
        setCanceledOnTouchOutside(false)
        // todo: 2023/3/31 初始化控件
        btnCompletion = findViewById(R.id.dig_btn_ok)
        btnClose = findViewById(R.id.dig_btn_cancel)
        btnCompletion?.setOnClickListener(this)
        btnClose?.setOnClickListener(this)
        lblTitle = findViewById(R.id.dig_title)
        lblMessage = findViewById(R.id.dig_message)
        // todo: 2023/3/31 默认不显示取消按钮
        btnClose?.visibility = View.GONE
    }

    /**
     * 设置标题
     * @param title String 标题内容
     */
    fun setTitle(title:String){
        lblTitle?.text = title
    }

    /**
     * 设置消息
     * @param message String 消息内容
     */
    fun setMessage(message:String){
        lblMessage?.text = message
    }

    /**
     * 设置按钮文字
     * @param okText String 确定按钮文字
     * @param closeText String 关闭按钮文字
     */
    fun setButtonsText(okText:String,closeText:String){
        if (okText != null){
            btnCompletion?.text = okText
            btnCompletion?.visibility = View.VISIBLE
        }else{
            btnCompletion?.visibility = View.GONE
        }
        if (closeText != null){
            btnClose?.text = closeText
            btnClose?.visibility = View.VISIBLE
        }else{
            btnClose?.visibility = View.GONE
        }
    }

    /**
     * 设置
     * @param okVisible Boolean 确定按钮是否显示
     * @param closeVisible Boolean 取消按钮是否显示
     */
    fun setButtonsVisible(okVisible:Boolean,closeVisible:Boolean){
        btnCompletion?.visibility = if(okVisible) View.VISIBLE else View.GONE
        btnClose?.visibility = if(closeVisible) View.VISIBLE else View.GONE
    }

    /**
     * 设置监听
     * @param listener DialogNormalListener
     */
    fun setListener(listener: DialogNormalListener){
        this.listener = listener
    }
    /**
     * 覆写点击事件
     * @param p0 View
     */
    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.dig_btn_ok -> {
                dismiss()
                listener?.completion()
            }
            R.id.dig_btn_cancel -> {
                dismiss()
                listener?.close()
            }
        }
    }
}