package com.gh.gamecenter.cloudarchive

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.CheckLoginUtils
import com.gh.common.util.NewFlatLogUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.eventbus.EBReuse
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.common.view.CustomDividerItemDecoration
import com.gh.gamecenter.databinding.FragmentMyArchiveBinding
import com.gh.gamecenter.entity.ArchiveEntity
import com.gh.gamecenter.entity.GameEntity
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

open class MyArchiveFragment : ListFragment<ArchiveEntity, CloudArchiveManagerViewModel>() {
    protected var mGameEntity: GameEntity? = null
    protected var mType: Type = Type.MY_ARCHIVE

    protected val mBinding: FragmentMyArchiveBinding by lazy { FragmentMyArchiveBinding.inflate(layoutInflater) }
    protected val mViewModel: CloudArchiveManagerViewModel by lazy {
        viewModelProvider(
            CloudArchiveManagerViewModel.Factory(
                mType, mGameEntity?.id ?: "", ""
            )
        )
    }
    protected val mAdapter by lazy {
        MyArchiveAdapter(
            requireContext(),
            this,
            provideListViewModel(),
            mType,
            mGameEntity?.getUniquePackageName() ?: ""
        )
    }

    override fun getLayoutId(): Int = 0

    override fun getInflatedLayout() = mBinding.root

    override fun provideListAdapter(): ListAdapter<*> = mAdapter

    override fun provideListViewModel(): CloudArchiveManagerViewModel = mViewModel

    override fun getItemDecoration(): RecyclerView.ItemDecoration =
        CustomDividerItemDecoration(requireContext(), notDecorateTheLastTwoItems = true).apply {
            setDrawable(R.drawable.divider_item_line_space_16.toDrawable(requireContext())!!)
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        mGameEntity = requireArguments().getParcelable(EntranceConsts.KEY_GAME)
        super.onCreate(savedInstanceState)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mBinding.reuseNoneData.root.setBackgroundColor(R.color.background_white.toColor(requireContext()))
        mBinding.reuseDataException.root.setBackgroundColor(R.color.background_white.toColor(requireContext()))
        mBinding.reuseNoConnection.root.setBackgroundColor(R.color.background_white.toColor(requireContext()))
        mBinding.reuseNoLogin.root.setBackgroundColor(R.color.background_white.toColor(requireContext()))

        mBinding.reuseNoneData.reuseNoneDataTv.text = "还没有存档噢~"
        mBinding.reuseNoneData.reuseNoneDataDescTv.text = "点击下方按钮上传您的游戏存档吧！"
        mBinding.reuseNoneData.reuseNoneDataDescTv.visibility = View.VISIBLE

        mBinding.reuseNoLogin.reuseNoneDataTv.text = "登录光环助手查看您的游戏存档~"
        mBinding.reuseNoLogin.reuseResetLoadTv.text = "立即登录"
        mBinding.reuseNoLogin.reuseNoneDataDescTv.visibility = View.GONE
        mBinding.reuseNoLogin.reuseResetLoadTv.visibility = View.VISIBLE
        mBinding.reuseNoLogin.reuseResetLoadTv.layoutParams =
            mBinding.reuseNoLogin.reuseResetLoadTv.layoutParams.apply { width = 136F.dip2px() }
        mBinding.reuseNoLogin.reuseResetLoadTv.setOnClickListener {
            ifLogin("云存档-我的存档")
        }
        mBinding.reuseNoLogin.root.goneIf(CheckLoginUtils.isLogin())

        mViewModel.shareSuccess.observeNonNull(viewLifecycleOwner) {
            if (it && requireActivity() is CloudArchiveManagerActivity) {
                (requireActivity() as CloudArchiveManagerActivity).updateMyShareArchive()
            }
        }
        if (mType == Type.MY_ARCHIVE) {
            NewFlatLogUtils.logCloudArchiveTabShow(mGameEntity?.id ?: "", mGameEntity?.name ?: "", "我的存档")
        }
    }

    override fun onLoadEmpty() {
        super.onLoadEmpty()
        mBinding.tipsTv.visibility = View.GONE
    }

    override fun onLoadNotFound() {
        super.onLoadNotFound()
        mBinding.tipsTv.visibility = View.GONE
    }

    override fun onLoadError() {
        super.onLoadError()
        mBinding.tipsTv.visibility = View.GONE
    }

    override fun onLoadDone() {
        super.onLoadDone()
        mBinding.tipsTv.visibility = View.VISIBLE
    }

    override fun onLoadRefresh() {
        super.onLoadRefresh()
        mBinding.reuseNoLogin.root.goneIf(CheckLoginUtils.isLogin())
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun onEventMainThread(reuse: EBReuse) {
        if (reuse.type == Constants.LOGIN_TAG) {
            onLoadRefresh()
        }
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mBinding.reuseNoneData.root.setBackgroundColor(R.color.background_white.toColor(requireContext()))
        mBinding.reuseDataException.root.setBackgroundColor(R.color.background_white.toColor(requireContext()))
        mBinding.reuseNoConnection.root.setBackgroundColor(R.color.background_white.toColor(requireContext()))
        mBinding.reuseNoLogin.root.setBackgroundColor(R.color.background_white.toColor(requireContext()))
    }

    enum class Type(val value: String) {
        MY_ARCHIVE("my_archive"),
        MY_DOWNLOAD_ARCHIVE("my_download_archive"),
        MY_SHARE_ARCHIVE("my_share_archive");

        companion object {
            fun fromString(typeString: String): Type {
                return values().find { typeString == it.value } ?: MY_ARCHIVE
            }
        }
    }
}