package com.gh.gamecenter.personalhome.home.game

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.entity.RatingComment
import com.gh.gamecenter.personalhome.home.UserHistoryFragment
import com.gh.gamecenter.personalhome.rating.MyRating
import com.halo.assistant.HaloApp

class UserCommentHistoryFragment : ListFragment<MyRating, UserCommentHistoryViewModel>() {

    private var mUserId: String = ""

    private var mAdapter: UserCommentHistoryAdapter? = null
    private lateinit var mViewModel: UserCommentHistoryViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        arguments?.apply {
            mUserId = getString(USER_ID, "")
        }
        super.onCreate(savedInstanceState)
        mListRv?.overScrollMode = View.OVER_SCROLL_NEVER
        mListRv?.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_surface.toColor(requireContext()))
    }

    override fun getItemDecoration() = null

    override fun provideListViewModel(): UserCommentHistoryViewModel {
        mViewModel = viewModelProvider(UserCommentHistoryViewModel.Factory(HaloApp.getInstance().application, mUserId))

        return mViewModel
    }

    override fun provideListAdapter(): ListAdapter<*> {
        if (mAdapter == null) {
            mAdapter = UserCommentHistoryAdapter(requireContext(), mEntrance, this, mListViewModel)
        }
        return mAdapter!!
    }

    override fun onLoadEmpty() {
        super.onLoadEmpty()

        // RecyclerView 被隐藏的话会导致不能 AppBar 不能滑动
        mListRv.visibility = View.VISIBLE

//        if (mViewModel.type != UserCommentHistoryViewModel.TYPE.ALL) {
//            val params = mReuseNoData?.layoutParams as RelativeLayout.LayoutParams
//            params.setMargins(0, mAdapter?.titleBottom ?: 0, 0, 0)
//            mReuseNoData?.layoutParams = params
//        }
//        mReuseNoData?.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.background))
    }

    override fun onLoadError() {
        super.onLoadError()

        mListRv.visibility = View.VISIBLE
        mReuseNoConn?.setBackgroundColor(ContextCompat.getColor(requireContext(), com.gh.gamecenter.common.R.color.ui_background))
    }

    fun changeType(text: String) {
        when (text) {
            "全部" -> mViewModel.changeType(UserCommentHistoryViewModel.TYPE.ALL)
            "精华" -> mViewModel.changeType(UserCommentHistoryViewModel.TYPE.JINGXUAN)
            "安利墙" -> mViewModel.changeType(UserCommentHistoryViewModel.TYPE.ANLIWALL)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data != null && requestCode == 100 && resultCode == Activity.RESULT_OK) {
            val dataPosition = data.getIntExtra("DATA_POSITION_TAG", -1)
            if (mAdapter?.entityList?.isEmpty() == true) return
            val rating = mAdapter?.entityList?.get(dataPosition)
            val resultData = data.getParcelableExtra<RatingComment>(RatingComment::class.java.simpleName)
            rating?.apply {
                vote = resultData?.vote ?: 0
                reply = resultData?.reply ?: 0
                content = resultData?.content ?: ""
                star = resultData?.star ?: 0
                me.isVoted = resultData?.me?.isVoted ?: false
            }
            mAdapter?.notifyItemChanged(dataPosition)
        }
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mListRv?.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_surface.toColor(requireContext()))
    }

    companion object {

        const val USER_ID = "user_id"

        fun getInstance(entrance: String, userId: String): UserCommentHistoryFragment {
            return UserCommentHistoryFragment().apply {
                with(
                    bundleOf(
                        EntranceConsts.KEY_ENTRANCE to entrance,
                        UserHistoryFragment.USER_ID to userId
                    )
                )
            }
        }
    }

}