package com.gh.gamecenter.qa.questions.invite

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.RecyclerView
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.constant.EntranceConsts
import com.gh.gamecenter.common.utils.DialogHelper
import com.gh.gamecenter.common.utils.ifLogin
import com.gh.gamecenter.common.utils.observeNonNull
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.qa.answer.detail.SimpleAnswerDetailActivity
import com.gh.gamecenter.qa.entity.InviteEntity
import com.gh.gamecenter.qa.entity.QuestionsDetailEntity
import com.halo.assistant.HaloApp

class QuestionsInviteFragment : ListFragment<InviteEntity, QuestionsInviteViewModel>() {

    private var mQuestionsDetailEntity: QuestionsDetailEntity? = null
    private var mAdapter: QuestionsInviteAdapter? = null
    private var mSearchKey: String? = null

    private lateinit var mViewModel: QuestionsInviteViewModel
    private lateinit var mFactory: QuestionsInviteViewModel.Factory

    override fun onCreate(savedInstanceState: Bundle?) {
        if (arguments != null) {
            mQuestionsDetailEntity = requireArguments().getParcelable(QuestionsDetailEntity::class.java.simpleName)
            mSearchKey = requireArguments().getString(EntranceConsts.KEY_INVITE_SEARCH_KEY, null)
        }
        super.onCreate(savedInstanceState)
        mListRefresh!!.isEnabled = false

        mViewModel.invite.observeNonNull(this) {
            mAdapter?.inviteSuccess(it)
            toast(R.string.invite_success)
        }

        mViewModel.inviteError.observeNonNull(this) {
            showAnsweredDialog(it)
        }
    }

    override fun provideListViewModel(): QuestionsInviteViewModel {
        mFactory =
            QuestionsInviteViewModel.Factory(HaloApp.getInstance().application, mQuestionsDetailEntity?.id, mSearchKey)
        mViewModel = viewModelProvider(mFactory)
        return mViewModel
    }

    override fun loadEmpty() {
        loadDone()
    }

    override fun provideListAdapter(): ListAdapter<*> {
        if (mAdapter == null) mAdapter =
            QuestionsInviteAdapter(requireContext(), this, mEntrance, "问题详情-邀请回答") else mAdapter
        return mAdapter!!
    }

    override fun getItemDecoration(): RecyclerView.ItemDecoration? {
        return null
    }

    override fun <LIST : Any?> onListClick(view: View?, position: Int, data: Any?) {
        when (view?.id) {
            R.id.questionsinvite_item_invite -> {
                val inviteEntity = data as InviteEntity
                val me = inviteEntity.me
                if (me == null || !me.isUserInvite) {
                    ifLogin("问题详情-邀请回答-[邀请]") { postInvite(inviteEntity.id) }
                }
            }
        }
    }

    fun search() {
        val parentFragment = parentFragment
        if (parentFragment is QuestionsInviteWrapperFragment) {
            mSearchKey = parentFragment.searchKey
            mListViewModel.searchKey = mSearchKey
            mListViewModel.load(LoadType.REFRESH)
        }
    }

    private fun postInvite(userId: String?) {
        mViewModel.invite(userId)
    }

    private fun showAnsweredDialog(answerId: String) {
        DialogHelper.showDialog(requireContext(),
            "已经回答",
            "Ta已经回答了这个问题，赶紧前往查看吧",
            "立即查看",
            "关闭",
            { startActivity(SimpleAnswerDetailActivity.getIntent(requireContext(), answerId, "邀请达人", "达人邀请")) })
    }

    override fun autoLoadMore() {
        mListViewModel.load(LoadType.NORMAL)
    }

    override fun theNumberNeededToFillAScreen(): Int {
        return 10
    }

    override fun getAutoLoadMoreDelay(): Int {
        return 100
    }

    companion object {
        fun getInstance(questionsDetailEntity: QuestionsDetailEntity): QuestionsInviteFragment {
            val fragment = QuestionsInviteFragment()
            val args = Bundle()
            args.putParcelable(QuestionsDetailEntity::class.java.simpleName, questionsDetailEntity)
            fragment.arguments = args
            return fragment
        }
    }
}