package com.gh.gamecenter.qa.editor

import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.common.view.CustomDividerItemDecoration
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.feature.entity.ArticleEntity

class ArticleFragment : ListFragment<ArticleEntity, ArticleViewModel>() {

    private var mAdapter: ArticleAdapter? = null
    private var mArticleType: InsertArticleWrapperActivity.ArticleType =
        InsertArticleWrapperActivity.ArticleType.MINE_ARTICLE

    override fun onCreate(savedInstanceState: Bundle?) {
        mArticleType =
            arguments?.getSerializable(InsertArticleWrapperActivity.KEY_ARTICLE_TYPE) as InsertArticleWrapperActivity.ArticleType
        super.onCreate(savedInstanceState)
    }

    override fun provideListViewModel(): ArticleViewModel {
        return viewModelProvider(ArticleViewModel.Factory(mArticleType))
    }

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

    override fun getItemDecoration(): RecyclerView.ItemDecoration {
        val insetDivider = ContextCompat.getDrawable(requireContext(), R.drawable.divider_item_line_space_20)
        val itemDecoration = CustomDividerItemDecoration(requireContext(), notDecorateTheLastItem = true)
        itemDecoration.setDrawable(insetDivider!!)
        return itemDecoration
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        if ((mListRv?.itemDecorationCount ?: 0) > 0) {
            mListRv?.removeItemDecorationAt(0)
            mListRv?.addItemDecoration(itemDecoration)
        }
    }
}
