package com.cheng.blzb.ui.fragment.push.subscribe

import com.cheng.blzb.R
import com.cheng.blzb.common.Constants
import com.cheng.blzb.databinding.FragmentSubscribeBinding
import com.cheng.blzb.event.SubscriptionEvent
import com.cheng.blzb.manager.DialogEnum
import com.cheng.blzb.ui.activity.PublicActivity
import com.cheng.blzb.ui.dialog.TipDialog
import com.cheng.blzb.ui.fragment.push.subscribe.add.AddSubscribeFragment
import com.example.base.common.RxBus
import com.example.base.decoration.FirstItemOffsetDecoration
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 com.example.base.widget.EmptyView
import com.example.base.widget.PageStatus

class SubscribeFragment: BaseFragment<FragmentSubscribeBinding, SubscribeViewModel>() {
    private val mAdapter by lazy { SubscribeAdapter() }
    private val mEmptyView by lazy { EmptyView(requireContext()) }

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

        binding.tvTitle.typeface = Constants.pmzdbt

        binding.mRecyclerView.adapter = mAdapter
        binding.mRecyclerView.addItemDecoration(FirstItemOffsetDecoration(DensityUtils.dp2px(7f), FirstItemOffsetDecoration.top))
        mEmptyView.setNoDataText("暂无订阅")
        mEmptyView.setNoDataLogo(R.mipmap.ic_empty_subscribe)
        mEmptyView.setBtnVisible(false)
        mAdapter.setEmptyView(mEmptyView)
    }

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

    override fun initListener() {
        super.initListener()

        binding.layoutArea.onClick {
            PublicActivity.start(requireContext(), AddSubscribeFragment::class.java, Pair("type", 1))
        }

        binding.layoutSource.onClick {
            PublicActivity.start(requireContext(), AddSubscribeFragment::class.java, Pair("type", 2))
        }

        mAdapter.setOnItemChildClickListener { _, view, i ->
            val item = mAdapter.getItem(i)
            when(view.id) {
                R.id.tv_delete -> {
                    val f = TipDialog.newInstance("温馨提示", "您确定要删除订阅吗？")
                    f.setOnSelectListener {
                        if (it == DialogEnum.CLICK_OK) {
                            mViewModel.deleteSubscription(item.id)
                        }
                    }
                    f.show(childFragmentManager, TipDialog::class.java.simpleName)
                }

                R.id.tv_edit -> {
                    PublicActivity.start(requireContext(), AddSubscribeFragment::class.java, Pair("type", item.type), Pair("item", item))
                }
            }
        }
    }

    override fun initObserve() {
        super.initObserve()
        mViewModel.subscribeLiveData.observe(this) {
            mAdapter.setList(it)
            if (it.isNotEmpty()) {
                mEmptyView.setStatus(PageStatus.GONG)
            } else {
                mEmptyView.setStatus(PageStatus.NO_DATA)
            }
        }

        mViewModel.deleteLiveData.observe(this) {
            RxBus.defaultInstance.post(SubscriptionEvent())
            toast("删除成功")
        }
        val subscriptionEvent = RxBus.defaultInstance.toObservable(SubscriptionEvent::class.java).subscribe {
            mViewModel.getSubscriptionList()
        }
        addDisposable(subscriptionEvent)
    }
}