package com.cheng.blzb.ui.dialog

import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.example.base.extensions.gone
import com.example.base.extensions.onClick
import com.example.base.extensions.visible
import com.example.base.utils.ScreenUtils
import com.cheng.blzb.R
import com.cheng.blzb.common.Constants
import com.cheng.blzb.databinding.DialogSimpleTipBinding
import com.cheng.blzb.manager.DialogEnum

class SimpleTipDialog : DialogFragment() {

    private var mOnBackListener: ((DialogEnum) -> Unit)? = null     //回调事件

    private var content: CharSequence? = null
    private var leftText: String? = null
    private var rightText: String? = null

    lateinit var binding: DialogSimpleTipBinding

    override fun onStart() {
        super.onStart()
        val window = dialog?.window
        val windowParams = window?.attributes
        windowParams?.dimAmount = 0.7f
        windowParams?.width = (ScreenUtils.getWindowSize().x * 0.8).toInt()
        dialog?.window?.attributes = windowParams
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val view = layoutInflater.inflate(R.layout.dialog_simple_tip, null)

        binding = DialogSimpleTipBinding.bind(view)

        binding.tvContent.typeface = Constants.douyinsansB

        content = arguments?.getCharSequence("content")
        leftText = arguments?.getString("leftText")
        rightText = arguments?.getString("rightText")

        if (!TextUtils.isEmpty(content)) {
            binding.tvContent.text = content
        }
        if (!TextUtils.isEmpty(leftText)) {
            binding.tvCancel.text = leftText
            binding.tvCancel.visible()
        } else {
            binding.tvCancel.gone()
        }
        if (!TextUtils.isEmpty(rightText)) {
            binding.tvOk.text = rightText
        }

        binding.tvCancel.onClick {
            mOnBackListener?.invoke(DialogEnum.CLICK_CANCEL)
            dismiss()
        }
        binding.tvOk.onClick {
            mOnBackListener?.invoke(DialogEnum.CLICK_OK)
            dismiss()
        }

        val dialog = Dialog(requireContext())
        dialog.setContentView(view)
        return dialog
    }

    fun setOnSelectListener(listener: ((DialogEnum) -> Unit)) {
        mOnBackListener = listener
    }

    companion object {
        fun newInstance(
            content: CharSequence? = null,
            leftText: String? = null,
            rightText: String? = null,
        ): SimpleTipDialog {
            val arg = Bundle()
            val fragment = SimpleTipDialog()
            arg.putCharSequence("content", content)
            arg.putString("leftText", leftText)
            arg.putString("rightText", rightText)
            fragment.arguments = arg
            return fragment
        }
    }
}