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.DialogSimpleTowLinesBinding

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

    private val viewBinding: DialogSimpleTowLinesBinding by lazy {
        DialogSimpleTowLinesBinding.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.messageLine1.text = message1
        viewBinding.messageLine1.setTextColor(Color.parseColor(messageTextColor))
        viewBinding.messageLine1.setTypeface(null, messageTextStyle)

        if (message2.isEmpty()) {
            viewBinding.messageLine2.visibility = View.GONE
        } else {
            viewBinding.messageLine2.visibility = View.VISIBLE
            viewBinding.messageLine2.text = message2
            viewBinding.messageLine2.setTextColor(Color.parseColor(messageTextColor))
            viewBinding.messageLine2.setTypeface(null, messageTextStyle)
        }

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

}