package com.cheng.blzb.ui.fragment.bid.publish.contacts

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Build
import android.text.TextUtils
import android.view.View
import com.cheng.blzb.bean.ContactsEntity
import com.cheng.blzb.databinding.FragmentAddContactsBinding
import com.cheng.blzb.event.ContactsEvent
import com.cheng.blzb.manager.ContactsManager
import com.cheng.blzb.manager.DialogEnum
import com.cheng.blzb.ui.dialog.TipDialog
import com.example.base.common.RxBus
import com.example.base.decoration.GridSpaceItemDecoration
import com.example.base.extensions.onClick
import com.example.base.extensions.toast
import com.example.base.ui.BaseFragment
import com.example.base.utils.DensityUtils
import java.util.UUID

class AddContactsFragment: BaseFragment<FragmentAddContactsBinding, AddContactsViewModel>() {
    private val mAdapter by lazy { ContactsJobAdapter() }

    private var contactsInfo: ContactsEntity? = null

    override fun initView() {
        super.initView()
        mTitleBar?.setBackgroundColor(Color.WHITE)

        binding.rvJob.adapter = mAdapter
        binding.rvJob.addItemDecoration(GridSpaceItemDecoration(3, DensityUtils.dp2px(8f), DensityUtils.dp2px(8f)))
        mAdapter.setList(ContactsEntity.getPositionList())
    }

    override fun initData() {
        super.initData()
        contactsInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            arguments?.getSerializable("item", ContactsEntity::class.java)
        } else {
            arguments?.getSerializable("item") as? ContactsEntity
        }
        setData()
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun initListener() {
        super.initListener()
        mAdapter.setOnItemClickListener { _, _, i ->
            val item = mAdapter.getItem(i)
            mAdapter.data.find { it.isChecked }?.isChecked = false
            item.isChecked = true
            mAdapter.notifyDataSetChanged()

            binding.etJob.setText(item.name)
        }

        binding.btnDelete.onClick {
            if (contactsInfo != null) {
                val f = TipDialog.newInstance("温馨提示", "确定删除该联系人？")
                f.setOnSelectListener {
                    if (it == DialogEnum.CLICK_OK) {
                        ContactsManager.delete(contactsInfo!!.id)
                        RxBus.defaultInstance.post(ContactsEvent(contactsInfo!!, ContactsEvent.TYPE_DELETE))
                        requireActivity().finish()
                    }
                }
                f.show(childFragmentManager, "")
            }
        }

        binding.btnNext.onClick {
            val name = binding.etName.text.toString().trim()
            val phone = binding.etPhone.text.toString().trim()
            val job = binding.etJob.text.toString().trim()
            if (TextUtils.isEmpty(name)) {
                toast("请输入姓名")
                return@onClick
            }
            if (TextUtils.isEmpty(phone)) {
                toast("请输入联系方式")
                return@onClick
            }
            if (TextUtils.isEmpty(job) && mAdapter.data.find { it.isChecked } == null) {
                toast("请输入职位")
                return@onClick
            }
            if (contactsInfo == null) {
                contactsInfo = ContactsEntity(UUID.randomUUID().toString(), name, phone, mAdapter.data.find { it.isChecked }?.name ?: job)
                ContactsManager.add(contactsInfo!!)
                RxBus.defaultInstance.post(ContactsEvent(contactsInfo!!, ContactsEvent.TYPE_ADD))
            } else {
                contactsInfo!!.name = name
                contactsInfo!!.phone = phone
                contactsInfo!!.job = job
                ContactsManager.update(contactsInfo!!)
                RxBus.defaultInstance.post(ContactsEvent(contactsInfo!!, ContactsEvent.TYPE_UPDATE))
            }
            requireActivity().finish()
        }
    }

    override fun initObserve() {
        super.initObserve()
    }

    private fun setData() {
        if (contactsInfo != null) {
            binding.etName.setText(contactsInfo!!.name)
            binding.etPhone.setText(contactsInfo!!.phone)
            binding.etJob.setText(contactsInfo!!.job)
        }
        binding.btnDelete.visibility = if (contactsInfo != null) View.VISIBLE else View.GONE
    }
}