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.onClick
import com.example.base.utils.ScreenUtils
import com.cheng.blzb.R
import com.cheng.blzb.common.Constants
import com.cheng.blzb.databinding.DialogTipBinding
import com.cheng.blzb.manager.DialogEnum

class TipDialog : DialogFragment() {

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

    private var title: String? = null
    private var content: CharSequence? = null
    private var leftText: String? = null
    private var rightText: String? = null
    private var cancelable: Boolean = true

    lateinit var binding: DialogTipBinding

    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))
        setCancelable(cancelable)
        return super.onCreateView(inflater, container, savedInstanceState)
    }

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

        binding = DialogTipBinding.bind(view)

        binding.tvTitle.typeface = Constants.douyinsansB

        title = arguments?.getString("title")
        content = arguments?.getCharSequence("content")
        leftText = arguments?.getString("leftText")
        rightText = arguments?.getString("rightText")
        cancelable = arguments?.getBoolean("cancelable") ?: true

        if (!TextUtils.isEmpty(title)) {
            binding.tvTitle.text = title
        }

        if (!TextUtils.isEmpty(content)) {
            binding.tvContent.text = content
        }
        if (!TextUtils.isEmpty(leftText)) {
            binding.tvCancel.text = leftText
        }
        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(
            title: String? = null,
            content: CharSequence? = null,
            leftText: String? = null,
            rightText: String? = null,
            cancelable: Boolean = true
        ): TipDialog {
            val arg = Bundle()
            val fragment = TipDialog()
            arg.putString("title", title)
            arg.putCharSequence("content", content)
            arg.putString("leftText", leftText)
            arg.putString("rightText", rightText)
            arg.putBoolean("cancelable", cancelable)
            fragment.arguments = arg
            return fragment
        }
    }
}