package com.gh.gamecenter.feedback.view.help

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.base.fragment.ToolbarFragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.entity.ErrorEntity
import com.gh.gamecenter.common.mvvm.Status
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.common.utils.toObject
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.common.view.CustomDividerItemDecoration
import com.gh.gamecenter.feedback.R
import com.gh.gamecenter.feedback.databinding.FragmentQaCategoryBinding

class HelpQaCategoryFragment : ToolbarFragment() {

    private var mQaCollectionId = ""
    private var mQaId = ""
    private var mViewModel: HelpQaCategoryViewModel? = null
    private lateinit var mBinding: FragmentQaCategoryBinding
    private var mAdapter: HelpQaCategoryAdapter? = null

    override fun getLayoutId(): Int = R.layout.fragment_qa_category

    override fun getInflatedLayout(): View {
        mBinding = FragmentQaCategoryBinding.inflate(layoutInflater, null, false)
        return mBinding.root
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mQaCollectionId = arguments?.getString(EntranceConsts.KEY_QA_COLLECTION_ID) ?: ""
        mQaId = arguments?.getString(EntranceConsts.KEY_QA_ID) ?: ""

        val factory = HelpQaCategoryViewModel.Factory(mQaCollectionId)
        mViewModel = viewModelProvider(factory)
        mViewModel?.categoryLiveData?.observe(this, Observer {
            mBinding.includeLoading.root.visibility = View.GONE
            if (it.status == Status.SUCCESS) {
                if (it.data.isNullOrEmpty()) {
                    mBinding.includeNoneData.root.findViewById<TextView>(R.id.reuseNoneDataTv)
                        .setText(R.string.game_empty)
                    mBinding.includeNoneData.root.visibility = View.VISIBLE
                } else {
                    mBinding.includeNoneData.root.visibility = View.GONE
                    mAdapter?.setListData(it.data!!)
                }
                mBinding.includeNoConnection.root.visibility = View.GONE
            } else {
                val errorEntity = it.exception?.response()?.errorBody()?.string()?.toObject<ErrorEntity>()
                if (errorEntity?.code == 403083) { // 隐藏
                    mBinding.includeNoneData.root.visibility = View.VISIBLE
                    mBinding.includeNoConnection.root.visibility = View.GONE
                    mBinding.includeNoneData.root.findViewById<TextView>(R.id.reuseNoneDataTv)
                        .setText(R.string.content_delete_hint)
                    toast(R.string.comment_failed_unable)
                } else {
                    mBinding.includeNoneData.root.visibility = View.GONE
                    mBinding.includeNoConnection.root.visibility = View.VISIBLE
                }

            }
        })
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mBinding.listRv.apply {
            layoutManager = LinearLayoutManager(context)
            addItemDecoration(getItemDecoration())
            mAdapter = HelpQaCategoryAdapter(requireContext(), mQaCollectionId)
            adapter = mAdapter
        }

        mBinding.includeNoConnection.root.setOnClickListener {
            mBinding.includeLoading.root.visibility = View.VISIBLE
            mBinding.includeNoConnection.root.visibility = View.GONE
            mViewModel?.initData()
        }
    }

    fun getItemDecoration(): RecyclerView.ItemDecoration {
        return CustomDividerItemDecoration(
            requireContext(),
            onlyDecorateTheFirstItem = false,
            notDecorateTheFirstItem = false,
            notDecorateTheLastItem = true,
            notDecorateTheFirstTwoItems = false
        ).apply {
            setDrawable(R.drawable.divider_item_line_space_16.toDrawable(requireContext())!!)
        }
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mBinding.listRv.run {
            if (itemDecorationCount > 0) {
                removeItemDecorationAt(0)
                addItemDecoration(getItemDecoration())
            }
        }
    }
}