package com.gh.gamecenter.personalhome.rating

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.ViewModelProviders
import com.gh.gamecenter.common.base.activity.BaseActivity
import com.gh.gamecenter.common.constant.EntranceConsts
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.updateStatusBarColor
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import io.reactivex.Single

class RatingActivity : ListActivity<MyRating, NormalListViewModel<MyRating>>() {

    private var mAdapter: RatingAdapter? = null
    private var mUserId: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        mUserId = intent.getStringExtra(EntranceConsts.KEY_USER_ID)
        super.onCreate(savedInstanceState)
        if (mUserId == UserManager.getInstance().userId) {
            setNavigationTitle("我的评分")
        } else {
            setNavigationTitle("Ta的评分")
        }
        updateStatusBarColor(R.color.background_white, R.color.background_white)
    }

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

    override fun provideDataSingle(page: Int): Single<MutableList<MyRating>> {
        return RetrofitManager.getInstance().api.getMyRating(mUserId, page, "view:default")
    }

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

    override fun isAutoResetViewBackgroundEnabled(): Boolean = true

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        updateStatusBarColor(R.color.background_white, R.color.background_white)
    }


    companion object {
        fun getIntent(context: Context, userId: String, entrance: String, path: String): Intent {
            val intent = Intent(context, RatingActivity::class.java)
            intent.putExtra(EntranceConsts.KEY_ENTRANCE, BaseActivity.mergeEntranceAndPath(entrance, path))
            intent.putExtra(EntranceConsts.KEY_USER_ID, userId)
            return intent
        }
    }
}