package com.abbidot.tracker.dialog

import android.content.Context
import android.text.TextUtils
import android.view.View
import androidx.core.content.ContextCompat
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.tracker.base.BaseDialog
import com.abbidot.tracker.databinding.DialogCommonLayoutBinding

/**
 *Created  by .yzq on 2022/1/7/007.
 * @link
 * @description:通用弹窗
 */
class CommonDialog1(
    context: Context,
    title: String = "",
    content: String = "",
    showCancelBtn: Boolean = true,
    contentTextSize: Float = 0f,
    contentTextStyle: Int = 0,
    contentTextColor: Int = 0,
    cancelable: Boolean = true,
    canceledOnTouchOutside: Boolean = false,
    okClickListener: OnDialogOkListener? = null,
    cancelListener: OnDialogCancelListener? = null
) : BaseDialog<DialogCommonLayoutBinding>(DialogCommonLayoutBinding::inflate, context) {

    private val mTitle = title
    private val mContent = content
    private val mContentTextSize = contentTextSize
    private val mContentTextColor = contentTextColor
    private val mContentTextStyle = contentTextStyle
    private val isShowCancelBtn = showCancelBtn
    private val isCancelable = cancelable
    private val mCanceledOnTouchOutside = canceledOnTouchOutside
    private val mOkClickListener = okClickListener
    private val mCancelListener = cancelListener

    override fun initView() {
        setCanceledOnTouchOutside(mCanceledOnTouchOutside)
        setCancelable(isCancelable)
        mViewBinding.apply {
            if (TextUtils.isEmpty(mTitle)) {
                tvCommonDialogTitle.visibility = View.GONE
            } else {
                tvCommonDialogTitle.visibility = View.VISIBLE
                tvCommonDialogTitle.text = mTitle
            }
            tvCommonDialogContent.let {
                it.text = mContent
                if (mContentTextColor != 0) it.setTextColor(
                    ContextCompat.getColor(
                        context, mContentTextColor
                    )
                )
                if (mContentTextSize > 0) it.textSize = AppUtils.spToPx(mContentTextSize)
                if (mContentTextStyle > 0) it.setTypeface(null, mContentTextStyle)
            }
            tbnCommonDialogNo.visibility = if (isShowCancelBtn) View.VISIBLE
            else View.GONE
            setOnClickListenerViews(tbnCommonDialogOk, tbnCommonDialogNo)
        }
    }

    override fun onClick(v: View) {
        dismiss()
        mViewBinding.apply {
            when (v) {
                tbnCommonDialogOk -> mOkClickListener?.onOkClick(this@CommonDialog1)
                tbnCommonDialogNo -> mCancelListener?.onCancelClick(this@CommonDialog1)
            }
        }
    }
}