package com.gh.gamecenter.gamedetail.myrating

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.MainActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.common.utils.safelyGetInRelease
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.common.view.CustomDividerItemDecoration
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.databinding.FragmentListNodataSkipNewBinding
import com.gh.gamecenter.entity.RatingComment
import com.gh.gamecenter.fragment.MainWrapperFragment
import com.gh.gamecenter.personalhome.rating.MyRating

class MyRatingFragment : ListFragment<MyRating, MyRatingViewModel>() {

    private var mAdapter: MyRatingAdapter? = null
    private val mBinding by lazy { FragmentListNodataSkipNewBinding.inflate(layoutInflater) }

    override fun getLayoutId(): Int = 0
    override fun getInflatedLayout(): View = mBinding.root

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
//        mCachedView.setBackgroundColor(R.color.white.toColor())
        mBinding.reuseNoneData.reuseNoneDataDescTv.visibility = View.VISIBLE
        mBinding.reuseNoneData.reuseNoneDataDescTv.text = getString(R.string.my_game_comment_no_data_desc)
        mBinding.reuseNoneData.reuseNoneDataTv.text = "你还没有游戏评论~"
        val layoutParam = mBinding.reuseNoneData.reuseResetLoadTv.layoutParams
        layoutParam.width = DisplayUtils.dip2px(150F)
        mBinding.reuseNoneData.reuseResetLoadTv.layoutParams = layoutParam
        mBinding.reuseNoneData.reuseResetLoadTv.text = "去首页看看"
        mBinding.reuseNoneData.reuseResetLoadTv.visibility = View.VISIBLE
        mBinding.reuseNoneData.reuseResetLoadTv.setOnClickListener {
            MainActivity.skipToMainActivity(requireContext(), MainWrapperFragment.INDEX_HOME)
            MtaHelper.onEvent("我的光环_新", "我的游戏评论", "去首页看看")
        }
    }

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

    override fun provideListViewModel(): MyRatingViewModel {
        return ViewModelProviders.of(this).get(MyRatingViewModel::class.java)
    }

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

    override fun getItemDecoration(): RecyclerView.ItemDecoration {
        val itemDecoration = CustomDividerItemDecoration(
            requireContext(),
            onlyDecorateTheFirstItem = false,
            notDecorateTheFirstItem = false,
            notDecorateTheLastItem = true,
            notDecorateTheFirstTwoItems = false
        )
        itemDecoration.setDrawable(R.drawable.divider_item_line_space_16.toDrawable()!!)
        return itemDecoration
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mBinding.reuseNoneData.reuseNoneDataTv.setTextColor(R.color.text_title.toColor(requireContext()))
        mBinding.reuseNoneData.reuseNoneDataDescTv.setTextColor(R.color.text_subtitleDesc.toColor(requireContext()))
        mBinding.reuseNoneData.reuseNoneDataIv.setImageResource(R.drawable.ic_empty_data)
    }

}