package com.gh.gamecenter.collection

import android.os.Bundle
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.CollectionUtils
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.common.view.CustomDividerItemDecoration
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.common.baselist.LoadType
import com.gh.gamecenter.eventbus.EBCollectionChanged
import com.gh.gamecenter.history.IBatchDelete
import com.gh.gamecenter.history.ManageOption
import com.gh.gamecenter.feature.entity.ArticleEntity
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class CommunityArticleFragment : ListFragment<ArticleEntity, CommunityArticleViewModel>(), IBatchDelete {

    private var mType = Type.COLLECTION.value
    private var mViewModel: CommunityArticleViewModel? = null
    private var mAdapter: CommunityArticleAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        mType = arguments?.getString(EntranceConsts.KEY_TYPE, Type.COLLECTION.value) ?: Type.COLLECTION.value
        super.onCreate(savedInstanceState)
        mCachedView.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_surface.toColor(requireContext()))
    }

    override fun provideListAdapter(): CommunityArticleAdapter {
        if (mAdapter == null) {
            mAdapter = CommunityArticleAdapter(requireContext(), mViewModel!!, mEntrance)
        }
        return mAdapter!!
    }

    override fun provideListViewModel(): CommunityArticleViewModel {
        if (mViewModel == null) {
            mViewModel = ViewModelProviders.of(this).get(CommunityArticleViewModel::class.java).apply { type = mType }
        }

        return mViewModel!!
    }

    // 收藏事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(changed: EBCollectionChanged) {
        if (changed.collectionType == CollectionUtils.CollectionType.COMMUNITY_ARTICLE
            && mType == Type.COLLECTION.value
        ) {
            mListViewModel.load(LoadType.REFRESH)
        }
    }

    override fun provideSyncAdapter(): RecyclerView.Adapter<*>? {
        return mAdapter
    }

    override fun addSyncPageObserver(): Boolean {
        return true
    }

    override fun changeOption(option: ManageOption) {
        mAdapter?.changeOption(option)
    }

    enum class Type(val value: String) {
        COLLECTION("collection"),

        HISTORY("history")
    }

    override fun getItemDecoration(): RecyclerView.ItemDecoration {
        val itemDecoration = CustomDividerItemDecoration(
            requireContext(),
            onlyDecorateTheFirstItem = false,
            notDecorateTheFirstItem = false,
            notDecorateTheLastItem = true,
            notDecorateTheFirstTwoItems = false
        )
        itemDecoration.setDrawable(com.gh.gamecenter.common.R.drawable.divider_item_line_space_16.toDrawable(requireContext())!!)
        return itemDecoration
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mCachedView.post {
            mCachedView.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_surface.toColor(requireContext()))
        }
        if ((mListRv?.itemDecorationCount ?: 0) > 0) {
            mListRv?.removeItemDecorationAt(0)
            mListRv?.addItemDecoration(itemDecoration)
        }
    }
}