package com.cheng.blzb.ui.fragment.mine.seat

import android.annotation.SuppressLint
import com.cheng.blzb.R
import com.cheng.blzb.bean.CorpEntity
import com.cheng.blzb.databinding.FragmentSeatManageBinding
import com.cheng.blzb.event.PaySuccessEvent
import com.cheng.blzb.manager.DialogEnum
import com.cheng.blzb.ui.dialog.BuySeatDialog
import com.cheng.blzb.ui.dialog.SeatAuthDialog
import com.cheng.blzb.ui.dialog.TipDialog
import com.example.base.common.RxBus
import com.example.base.extensions.onClick
import com.example.base.extensions.toast
import com.example.base.ui.BaseFragment
import com.example.base.widget.EmptyView
import com.example.base.widget.PageStatus
import com.google.gson.JsonObject
import okhttp3.RequestBody.Companion.toRequestBody

class SeatManageFragment: BaseFragment<FragmentSeatManageBinding, SeatManageViewModel>() {
    private val mEmptyView by lazy { EmptyView(requireContext()) }
    private val mAdapter by lazy { SeatManageAdapter() }

    private var corpInfo: CorpEntity? = null

    private var seatDialog: BuySeatDialog? = null
    private var dismissDialog = false

    override fun initView() {
        super.initView()
        mTitleBar?.background = null

        binding.mRecyclerView.adapter = mAdapter
        mEmptyView.setNoDataText("暂无数据")
        mEmptyView.setNoDataLogo(R.mipmap.ic_empty_seat)
        mAdapter.setEmptyView(mEmptyView)
    }

    override fun initData() {
        super.initData()
        mViewModel.getCorpInfo()
    }

    override fun initListener() {
        super.initListener()
        binding.tvAdd.onClick {
            mViewModel.getGoodsList()
        }

        binding.btnNext.onClick {
            if (corpInfo!!.sub_user_limit > corpInfo!!.bind_count) {
                showSeatAuthDialog()
            } else {
                mViewModel.getGoodsList()
            }
        }

        mAdapter.setOnItemChildClickListener { _, view, i ->
            val item = mAdapter.getItem(i)
            when(view.id) {
                R.id.btn_delete -> {
                    val f = TipDialog.newInstance("解除席位授权", "是否解除尾号${item.phone.substring(7)}的用户席位？")
                    f.setOnSelectListener {
                        if (it == DialogEnum.CLICK_OK) {
                            mViewModel.deleteSeat(item.id)
                        }
                    }
                    f.show(childFragmentManager, TipDialog::class.java.simpleName)
                }
            }

        }
    }

    @SuppressLint("SetTextI18n")
    override fun initObserve() {
        super.initObserve()
        mViewModel.corpInfoLiveData.observe(this) {
            corpInfo = it
            mViewModel.getSeatList()
        }

        mViewModel.seatListLiveData.observe(this) {
            mAdapter.setList(it)
            binding.tvCount.text = "当前席位(${it.size}/${corpInfo!!.sub_user_limit})"
            if (it.isNotEmpty()) {
                mEmptyView.setStatus(PageStatus.GONG)
            } else {
                mEmptyView.setStatus(PageStatus.NO_DATA)
            }
        }

        mViewModel.addLiveData.observe(this) {
            toast("授权成功")
            mViewModel.getSeatList()
        }

        mViewModel.deleteLiveData.observe(this) {
            toast("解除成功")
            mViewModel.getSeatList()
        }

        mViewModel.goodsLiveData.observe(this) {
            if (it.isNotEmpty()) {
                seatDialog = BuySeatDialog.newInstance(it[0])
                seatDialog!!.show(childFragmentManager, "")
            } else {
                toast("获取商品失败，请稍后再试")
            }
        }

        val paySuccessEvent = RxBus.defaultInstance.toObservable(PaySuccessEvent::class.java).subscribe {
            dismissDialog = true
            if (!isStateSaved) dismissSeatDialog()
            mViewModel.getCorpInfo()
        }
        addDisposable(paySuccessEvent)
    }

    private fun showSeatAuthDialog() {
        val f = SeatAuthDialog.newInstance()
        f.setOnSelectListener { phone ->
            val jsonObject = JsonObject()
            jsonObject.addProperty("phone", phone)
            mViewModel.addSeat(jsonObject.toString().toRequestBody())
        }
        f.show(childFragmentManager, "")
    }

    private fun dismissSeatDialog() {
        if (dismissDialog) {
            seatDialog?.dismiss()
            seatDialog = null
            dismissDialog = false
        }
    }

    override fun onResume() {
        super.onResume()
        dismissSeatDialog()
    }
}