package com.gh.gamecenter.qa.questions.draft

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.core.utils.ToastUtils
import com.gh.gamecenter.common.utils.checkStoragePermissionBeforeAction
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.ListAdapter
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.common.baselist.LoadType
import com.gh.gamecenter.common.eventbus.EBReuse
import com.gh.gamecenter.qa.draft.CommunityDraftWrapperActivity
import com.gh.gamecenter.qa.entity.QuestionDraftEntity
import com.gh.gamecenter.qa.questions.edit.QuestionEditActivity
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class QuestionDraftFragment : ListFragment<QuestionDraftEntity, QuestionDraftViewModel>() {

    var mAdapter: QuestionDraftAdapter? = null
    lateinit var mViewModel: QuestionDraftViewModel

    override fun provideListAdapter(): ListAdapter<*> {
        return mAdapter ?: QuestionDraftAdapter(requireContext(), mViewModel) {
            if (activity is CommunityDraftWrapperActivity) {
                checkStoragePermissionBeforeAction {
                    val intent = QuestionEditActivity.getDraftIntent(requireContext(), it)
                    startActivity(intent)
                }
            } else {
                checkStoragePermissionBeforeAction {
                    val intent = Intent()
                    intent.putExtra(QuestionDraftEntity::class.java.simpleName, it)
                    requireActivity().setResult(Activity.RESULT_OK, intent)
                    requireActivity().finish()
                }
            }
        }.apply {
            mAdapter = this
        }
    }

    override fun provideListViewModel(): QuestionDraftViewModel {
        mViewModel = viewModelProvider()
        return mViewModel
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (activity is QuestionDraftActivity) {
            setNavigationTitle("问题草稿")
        }
        mViewModel.deleteDraftSuccess.observe(this, Observer { pair ->
            if (pair.second) {
                val draftId = pair.first
                val index = mAdapter?.entityList?.indexOfFirst { it.id == draftId } ?: -1
                if (index >= 0) {
                    mAdapter?.entityList?.removeAt(index)
                    if (mAdapter?.entityList.isNullOrEmpty()) {
                        mListViewModel.load(LoadType.REFRESH)
                    } else {
                        mAdapter?.notifyItemRemoved(index)
                    }

                    ToastUtils.showToast("删除成功")
                }

            }
        })
    }

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

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(reuse: EBReuse) {
        if (QuestionEditActivity.QUESTION_DRAFT_CHANGE_TAG == reuse.type) {
            mBaseHandler.postDelayed({ mListViewModel.load(LoadType.REFRESH) }, 100)
        }
    }

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