package com.dlc.chemical.ui.dialog

import android.app.Dialog
import android.content.Context
import android.content.res.Resources
import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.dlc.chemical.R
import com.dlc.chemical.databinding.DialogSimpleBinding

/**
 * info
 *
 * @author ReiChin_
 */
class SimpleDialog(
    context: Context,
    private val titleText: String,
    private val message: String,
    private val messageTextColor: String = "#FF2296FB",
    private val messageTextStyle: Int = Typeface.NORMAL,
    private val showCancelBtn: Boolean = true,
    private val extraTips: String? = null,
    private val dialogListener: (() -> Unit)? = null
) :
    Dialog(context, R.style.AlertDialogStyle) {

    private val viewBinding: DialogSimpleBinding by lazy {
        DialogSimpleBinding.inflate(LayoutInflater.from(context))
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(viewBinding.root)
        initWindowConfig()
        initViews()
    }

    private fun initWindowConfig() {
        window?.apply {
            val dm = Resources.getSystem().displayMetrics
            attributes.width = (dm.widthPixels * 0.35f).toInt()
            attributes.height = WindowManager.LayoutParams.WRAP_CONTENT
        }
    }

    private fun initViews() {
        viewBinding.title.text = titleText
        viewBinding.message.text = message
        viewBinding.message.setTextColor(Color.parseColor(messageTextColor))
        viewBinding.message.setTypeface(null, messageTextStyle)
        if (extraTips.isNullOrEmpty()) {
            viewBinding.extraTips.visibility = View.GONE
        } else {
            viewBinding.extraTips.visibility = View.VISIBLE
            viewBinding.extraTips.text = extraTips
        }

        // 取消
        viewBinding.cancel.visibility = if (showCancelBtn) View.VISIBLE else View.GONE
        viewBinding.cancel.setOnClickListener { cancel() }
        // 确认
        viewBinding.confirm.setOnClickListener {
            dialogListener?.invoke()
            dismiss()
        }
    }

}