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.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.base.decoration.SpacesItemDecoration
import com.example.base.extensions.onClick
import com.example.base.utils.DensityUtils
import com.example.base.utils.ScreenUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.cheng.blzb.R
import com.cheng.blzb.bean.AccountEntity
import com.cheng.blzb.common.Constants
import com.cheng.blzb.databinding.DialogAccountListBinding
import com.cheng.blzb.manager.DialogEnum

class AccountListDialog : DialogFragment() {
    private val btnText by lazy { arguments?.getString("btnText") ?: "我知道了" }
    private val mAdapter by lazy { AccountAdapter() }

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

    lateinit var binding: DialogAccountListBinding

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

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

        binding = DialogAccountListBinding.bind(view)

        binding.tvTitle.typeface = Constants.douyinsansB

        binding.mRecyclerView.adapter = mAdapter
        binding.mRecyclerView.addItemDecoration(SpacesItemDecoration(DensityUtils.dp2px(10f)))

        val listStr = arguments?.getString("list")
        if (!TextUtils.isEmpty(listStr)) {
            val list = Gson().fromJson<List<AccountEntity>>(listStr, object : TypeToken<List<AccountEntity>>(){}.type)
            mAdapter.setList(list)
        }

        binding.tvOk.text = btnText

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

        binding.ivClose.onClick { dismiss() }

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

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

    companion object {
        fun newInstance(list: List<AccountEntity>, btnText: String? = null): AccountListDialog {
            val arg = Bundle()
            arg.putString("list", Gson().toJson(list))
            arg.putString("btnText", btnText)
            val fragment = AccountListDialog()
            fragment.arguments = arg
            return fragment
        }
    }

    class AccountAdapter: BaseQuickAdapter<AccountEntity, BaseViewHolder>(R.layout.listitem_account_login_tip) {
        override fun convert(holder: BaseViewHolder, item: AccountEntity) {
            if (item.vip_type == "1") {
                holder.setGone(R.id.iv_vip_tag, true)
            } else if (item.vip_type == "2" || item.vip_type == "3") {
                holder.setImageResource(R.id.iv_vip_tag, if (item.vip_type == "2") R.mipmap.ic_vip_tag1 else R.mipmap.ic_vip_tag2)
                holder.setGone(R.id.iv_vip_tag, false)
            } else if (item.vip_type == "12" || item.vip_type == "11") {
                holder.setImageResource(R.id.iv_vip_tag, if (item.vip_type == "12") R.mipmap.ic_vip_tag3 else R.mipmap.ic_vip_tag4)
                holder.setGone(R.id.iv_vip_tag, false)
            }

            holder.setText(R.id.tv_username, item.name)
            holder.setText(R.id.tv_create_time, "${item.create_time} 注册")
            holder.setGone(R.id.iv_bind_wx, !item.bind.contains("weixin"))
            holder.setGone(R.id.iv_bind_phone, !item.bind.contains("phone"))
            if (!TextUtils.isEmpty(item.phone)) {
                holder.setText(R.id.tv_phone, item.phone.replaceRange(3, 7, "****"))
            } else {
                holder.setText(R.id.tv_phone, "")
            }
        }
    }
}