package com.gh.gamecenter.qa.editor

import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
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.AnswerEntity

class AnswerFragment : ListFragment<AnswerEntity, AnswerViewModel>() {

    private var mAdapter: AnswerAdapter? = null
    private var mAnswerType: InsertAnswerWrapperActivity.AnswerType = InsertAnswerWrapperActivity.AnswerType.MINE_ANSWER

    override fun onCreate(savedInstanceState: Bundle?) {
        mAnswerType =
            arguments?.getSerializable(InsertAnswerWrapperActivity.KEY_ANSWER_TYPE) as InsertAnswerWrapperActivity.AnswerType
        super.onCreate(savedInstanceState)
    }

    override fun provideListViewModel(): AnswerViewModel {
        return viewModelProvider(AnswerViewModel.Factory(mAnswerType))
    }

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

    override fun getItemDecoration(): 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)
            itemDecoration?.let {
                mListRv?.addItemDecoration(it)
            }
        }
    }

}
