package com.cheng.blzb.ui.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cheng.blzb.R
import com.cheng.blzb.bean.BidTypeEntity
import com.cheng.blzb.databinding.DialogSelectBidTypeBinding
import com.cheng.blzb.manager.UserConfigManager
import com.example.base.extensions.getColor
import com.example.base.extensions.onClick
import com.example.base.extensions.toast
import com.example.base.utils.ScreenUtils

class SelectBidTypeDialog : DialogFragment() {
    private val mAdapter by lazy { BidTypeAdapter() }

    private var bidType: BidTypeEntity? = null

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

    private lateinit var binding: DialogSelectBidTypeBinding

    override fun onStart() {
        super.onStart()
        val window = dialog?.window
        val windowParams = window?.attributes
        windowParams?.dimAmount = 0.7f
        windowParams?.width = ScreenUtils.getWindowSize().x
        windowParams?.gravity = Gravity.BOTTOM
        windowParams?.windowAnimations = R.style.dialog_bottom
        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)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val view = layoutInflater.inflate(R.layout.dialog_select_bid_type, null)

        binding = DialogSelectBidTypeBinding.bind(view)

        bidType = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            arguments?.getSerializable("type", BidTypeEntity::class.java)
        } else {
            arguments?.getSerializable("type") as? BidTypeEntity
        }

        binding.mRecyclerView.adapter = mAdapter
        val typeList = UserConfigManager.getBidTypes()
        if (bidType != null) {
            typeList.find { it.id == bidType!!.id }?.isChecked = true
        }
        mAdapter.setList(typeList)

        mAdapter.setOnItemClickListener { _, _, i ->
            val item = mAdapter.getItem(i)
            mAdapter.data.find { it.isChecked }?.isChecked = false
            item.isChecked = true
            bidType = item
            mAdapter.notifyDataSetChanged()
        }

        binding.btnNext.onClick {
            if (bidType == null) {
                toast("请选择类别")
                return@onClick
            }
            mOnBackListener?.invoke(bidType!!)
            dismiss()
        }

        binding.ivClose.onClick { dismiss() }

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

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

    companion object {
        fun newInstance(type: BidTypeEntity?): SelectBidTypeDialog {
            val arg = Bundle()
            arg.putSerializable("type", type)
            val fragment = SelectBidTypeDialog()
            fragment.arguments = arg
            return fragment
        }
    }

    class BidTypeAdapter : BaseQuickAdapter<BidTypeEntity, BaseViewHolder>(R.layout.listitem_bid_type) {
        override fun convert(holder: BaseViewHolder, item: BidTypeEntity) {
            holder.setText(R.id.tv_name, item.name)
            holder.setTextColor(R.id.tv_name, if (item.isChecked) getColor(R.color.color_125ffe) else getColor(R.color.color_1a1a1a))
            holder.getView<TextView>(R.id.tv_name).typeface = if (item.isChecked) Typeface.DEFAULT_BOLD else Typeface.DEFAULT

            holder.itemView.setBackgroundResource(if (item.isChecked) R.drawable.shape_f1f6f_cor4 else 0)
        }

    }
}