package com.gh.gamecenter.gamedetail.rating.logs

import android.content.Context
import android.content.Intent
import android.graphics.Paint
import android.os.Bundle
import android.widget.TextView
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.view.VerticalItemDecoration
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.ListActivity
import com.gh.gamecenter.common.baselist.NormalListViewModel
import com.gh.gamecenter.common.utils.setRootBackgroundColor
import com.gh.gamecenter.databinding.ActivityGameCommentLogsBinding
import com.gh.gamecenter.entity.RatingComment
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import io.reactivex.Single

class CommentLogsActivity : ListActivity<RatingComment, NormalListViewModel<RatingComment>>() {

    private var mAdapter: CommentLogsAdapter? = null

    private lateinit var mGameId: String
    private lateinit var mCommentId: String
    private lateinit var mBinding: ActivityGameCommentLogsBinding

    override fun getLayoutId(): Int {
        return R.layout.activity_game_comment_logs
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        mGameId = intent.getStringExtra(EntranceConsts.KEY_GAMEID) ?: ""
        mCommentId = intent.getStringExtra(EntranceConsts.KEY_COMMENTID) ?: ""
        super.onCreate(savedInstanceState)
        mBinding = ActivityGameCommentLogsBinding.bind(mContentView)
        setNavigationTitle("评论修改历史")
        findViewById<TextView>(R.id.delete)?.paint?.flags = Paint.STRIKE_THRU_TEXT_FLAG
    }

    override fun getItemDecoration(): RecyclerView.ItemDecoration {
        return VerticalItemDecoration(this, 8f, false)
    }

    override fun provideListAdapter(): CommentLogsAdapter {
        if (mAdapter == null) mAdapter = CommentLogsAdapter(this, mListViewModel)
        return mAdapter!!
    }

    override fun provideDataSingle(page: Int): Single<MutableList<RatingComment>> {
        return RetrofitManager.getInstance().api.getGameCommentLogs(mGameId, mCommentId, page)
    }

    override fun provideListViewModel(): NormalListViewModel<RatingComment> {
        val factory = NormalListViewModel.Factory(HaloApp.getInstance().application, this)
        return ViewModelProviders.of(this, factory)
            .get(NormalListViewModel::class.java) as NormalListViewModel<RatingComment>
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mBinding.root.setRootBackgroundColor(R.color.background)
        if (mListRv != null) {
            mListRv.recycledViewPool.clear()
            mListRv.adapter?.notifyItemRangeChanged(0, mListRv.adapter?.itemCount ?: 0)
            if (mListRv.itemDecorationCount > 0) {
                mListRv.removeItemDecorationAt(0)
                mListRv.addItemDecoration(itemDecoration)
            }
        }
    }

    override fun getBusinessId(): Pair<String, String> {
        return if (intent.getStringExtra(EntranceConsts.KEY_GAMEID) != null && intent.getStringExtra(EntranceConsts.KEY_COMMENTID) != null) {
            Pair(
                intent.getStringExtra(EntranceConsts.KEY_GAMEID) ?: "",
                intent.getStringExtra(EntranceConsts.KEY_COMMENTID) ?: ""
            )
        } else {
            super.getBusinessId()
        }
    }

    companion object {
        fun getIntent(context: Context, gameId: String, commentId: String): Intent {
            val intent = Intent(context, CommentLogsActivity::class.java)
            intent.putExtra(EntranceConsts.KEY_GAMEID, gameId)
            intent.putExtra(EntranceConsts.KEY_COMMENTID, commentId)
            return intent
        }
    }
}