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.LinearLayout
import android.widget.TextView
import com.cr.cruav.R
import com.cr.data.CrUtil
import com.cr.widget.CrEditTextWidget

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/6/7 15:42
 * 描述：输入对话框
 */
class DialogInput : Dialog, View.OnClickListener {
    /**
     * 对外接口
     */
    interface DialogInputListener {
        /**
         * 完成
         * @param valueOne String 值1
         * @param valueTwo String 值2
         * @param self DialogInput 对话框本身
         */
        fun completion(valueOne:String,valueTwo:String,self:DialogInput)

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

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

    // define: 2023/3/31 定义控件
    var btnCompletion: Button? = null  // define: 2023/6/7 完成按钮
    var btnClose: Button? = null   // define: 2023/6/7 取消按钮
    var lblTitle: TextView? = null  // define: 2023/6/7 对话框标题
    var lblOne: TextView? = null  // define: 2023/6/7 第一输入框标题
    var lblTwo: TextView? = null  // define: 2023/6/7 第二输入框标题
    var panelOne: LinearLayout? = null  // define: 2023/6/7 第一输入框容器
    var panelTwo: LinearLayout? = null   // define: 2023/6/7 第二输入框容器
    var txtOne: CrEditTextWidget? = null  // define: 2023/6/7 第一输入框
    var txtTwo: CrEditTextWidget? = null  // define: 2023/6/7 第二输入框
    var self:DialogInput?=null  // define: 2023/6/7 自身引用

    /**
     * 初始化
     * @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 标题
     * @constructor
     */
    constructor(context: Context, title: String) : super(context, R.style.Dialog) {
        init()
        lblTitle?.text = title
    }

    /**
     * 初始化
     * @param context Context 上下文
     * @param title String 主题
     * @param listener DialogNormalListener 消息监听
     * @constructor
     */
    constructor(
        context: Context,
        title: String,
        listener: DialogInputListener
    ) : super(context, R.style.Dialog) {
        init()
        lblTitle?.text = title
        this.listener = listener
    }

    /**
     * 初始化
     */
    private fun init() {
        self = this
        setContentView(R.layout.dig_input)
        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)
        lblOne = findViewById(R.id.dig_lbl_one)
        lblTwo = findViewById(R.id.dig_lbl_two)
        panelOne = findViewById(R.id.dig_panel_one)
        panelTwo = findViewById(R.id.dig_panel_two)
        txtOne = findViewById(R.id.dig_txt_one)
        txtTwo = findViewById(R.id.dig_txt_two)
        // todo: 2023/3/31 默认不显示取消按钮
        btnClose?.visibility = View.GONE
    }

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

    /**
     * 设置标签显示内容
     * @param strOne String 标签一内容
     * @param strTow String 标签二内容
     */
    fun setLabels(strOne: String,strTow:String?) {
        if(strOne == null){
            panelOne?.visibility = View.GONE
        }else{
            panelOne?.visibility = View.VISIBLE
            lblOne?.text = strOne
        }
        if(strTow == null){
            panelTwo?.visibility = View.GONE
        }else{
            panelTwo?.visibility = View.VISIBLE
            lblTwo?.text = strTow
        }
    }

    /**
     * 设置输入文本框的提示信息
     * @param hintOne String 输入框一的提示信息
     * @param hintTwo String 输入框二的提示信息
     */
    fun setHints(hintOne:String,hintTwo:String){
        txtOne?.setContextHint(hintOne)
        txtTwo?.setContextHint(hintTwo)
    }

    /**
     * 设置输入框图标
     * @param fontOne String 输入框一图标
     * @param fontTwo String 输入框二图标
     */
    fun setFonts(fontOne:String,fontTwo:String){
        txtOne?.setFontIcon(fontOne)
        txtTwo?.setFontIcon(fontTwo)
    }

    /**
     * 设置按钮文字
     * @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 onText String
     * @param twoText String
     */
    fun setContent(onText:String,twoText:String){
        txtOne?.setContent(onText)
        txtTwo?.setContent(twoText)
    }

    /**
     * 设置
     * @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 DialogInputListener
     */
    fun setListener(listener: DialogInputListener) {
        this.listener = listener
    }

    /**
     * 覆写点击事件
     * @param p0 View
     */
    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.dig_btn_ok -> {
                if(panelOne?.visibility == View.VISIBLE && (txtOne!!.getContent() == null || txtOne!!.getContent() == "")){
                    CrUtil.showToast("请输入内容！")
                    return
                }
                if(panelTwo?.visibility == View.VISIBLE && (txtTwo!!.getContent() == null || txtTwo!!.getContent() == "")){
                    CrUtil.showToast("请输入内容！")
                    return
                }
                dismiss()
                listener?.completion(txtOne!!.getContent(),txtTwo!!.getContent(),self!!)
            }
            R.id.dig_btn_cancel -> {
                dismiss()
                listener?.close()
            }
        }
    }
}