package com.atmob.keyboard_android.ui.popup

import android.content.Context
import android.view.View
import android.view.Window
import android.widget.TextView
import com.atmob.keyboard_android.R
import com.atmob.keyboard_android.ext.click
import com.atmob.keyboard_android.ui.popup.base.BasePopupDialog

/**
 * 确认弹窗
 */
class ConfirmDialog(private val context: Context, hostWindow: Window) : BasePopupDialog(context, hostWindow) {
    private lateinit var vCloseBtn: View
    private lateinit var vTitle: TextView
    private lateinit var vContent: TextView
    private lateinit var vRejectBtn: TextView
    private lateinit var vAllowBtn: TextView

    /**
     * 数据模型
     */
    private var mDataModel: DataModel? = null

    /**
     * 操作回调
     */
    private var mOnActionCallback: OnActionCallback? = null

    override fun onInflaterViewId(): Int {
        return R.layout.dialog_confirm
    }

    override fun onInflaterViewAfter(view: View) {
        super.onInflaterViewAfter(view)
        vCloseBtn = view.findViewById(R.id.close_btn)
        vTitle = view.findViewById(R.id.title)
        vContent = view.findViewById(R.id.content)
        vRejectBtn = view.findViewById(R.id.reject_btn)
        vAllowBtn = view.findViewById(R.id.allow_btn)
    }

    override fun onBindView(view: View) {
        super.onBindView(view)
        vCloseBtn.click {
            // 关闭
            dismiss()
        }
        vRejectBtn.click {
            // 拒绝
            mOnActionCallback?.onReject()
            dismiss()
        }
        vAllowBtn.click {
            // 允许
            mOnActionCallback?.onAllow()
            dismiss()
        }
    }

    /**
     * 渲染
     */
    private fun render() {
        mDataModel?.run {
            vTitle.text = title
            vContent.apply {
                text = content
                visibility = if (content.isBlank()) {
                    View.GONE
                } else {
                    View.VISIBLE
                }
            }
            // 拒绝
            vRejectBtn.text = if (rejectBtnText.isBlank()) {
                context.resources.getString(R.string.reject)
            } else {
                rejectBtnText
            }
            // 同意
            vAllowBtn.text = if (allowBtnText.isBlank()) {
                context.resources.getString(R.string.allow)
            } else {
                allowBtnText
            }
        }
    }

    class DataModel(
        // 标题
        val title: String,
        // 内容
        val content: String = "",
        // 拒绝按钮的文字
        val rejectBtnText: String = "",
        // 同意按钮的文字
        val allowBtnText: String = ""
    )

    interface OnActionCallback {
        /**
         * 拒绝
         */
        fun onReject()

        /**
         * 同意
         */
        fun onAllow()
    }

    /**
     * 绑定数据
     */
    fun bindDataModel(dataModel: DataModel): ConfirmDialog {
        this.mDataModel = dataModel
        render()
        return this
    }

    /**
     * 绑定操作回调
     */
    fun bindOnActionCallback(callback: OnActionCallback): ConfirmDialog {
        this.mOnActionCallback = callback
        return this
    }
}