package com.gh.gamecenter.video.game

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import com.gh.gamecenter.common.base.activity.BaseActivity
import com.gh.gamecenter.common.base.activity.ToolBarActivity
import com.gh.gamecenter.common.base.adapter.FragmentAdapter
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.core.utils.NumberUtils
import com.gh.gamecenter.common.utils.observeNonNull
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.databinding.ActivityVideoGameBinding
import com.gh.gamecenter.common.mvvm.Status

class GameVideoActivity : ToolBarActivity() {

    private lateinit var mBinding: ActivityVideoGameBinding
    private lateinit var mViewModel: GameVideoViewModel

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

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // toolbar & status bar
        setNavigationTitle("视频合集")
        setStatusBarColor(R.color.text_181927.toColor())
        mTitleTv.setTextColor(Color.WHITE)
        mBinding = ActivityVideoGameBinding.bind(mContentView)

        // viewpager & tab content
        val gameId = intent.getStringExtra(EntranceConsts.KEY_GAMEID) ?: ""
        val titles = arrayListOf("最热", "最新")
        val fragments = ArrayList<Fragment>()

        val tag = "android:switcher:${mBinding.viewpager.id}:"
        val hottest = supportFragmentManager.findFragmentByTag("${tag}0") ?: GameVideoFragment()
        val hottestBundle = Bundle()
        hottestBundle.putString(EntranceConsts.KEY_GAMEID, gameId)
        hottestBundle.putString(EntranceConsts.KEY_SORT, "vote_recently:-1")
        hottest.arguments = hottestBundle
        fragments.add(hottest)

        val newest = supportFragmentManager.findFragmentByTag("${tag}1") ?: GameVideoFragment()
        val newestBundle = Bundle()
        newestBundle.putString(EntranceConsts.KEY_GAMEID, gameId)
        newestBundle.putString(EntranceConsts.KEY_SORT, "time.upload:-1")
        newest.arguments = newestBundle
        fragments.add(newest)

        // view
        mBinding.viewpager.offscreenPageLimit = fragments.size
        mBinding.viewpager.adapter =
            FragmentAdapter(supportFragmentManager, fragments, titles)
        mBinding.tabLayout.setupWithViewPager(mBinding.viewpager)
        mBinding.tabIndicator.setupWithTabLayout(mBinding.tabLayout)
        mBinding.tabIndicator.setupWithViewPager(mBinding.viewpager)

        // view model
        val factory = GameVideoViewModel.Factory(application, gameId ?: "")
        mViewModel = ViewModelProviders.of(this, factory).get(GameVideoViewModel::class.java)
        mViewModel.gameViewInfo.observeNonNull(this, callback = {
            if (it.status == Status.SUCCESS) {
                mBinding.gameIcon.displayGameIcon(
                    it.data?.game?.getIcon()
                        ?: it.data?.gameIcon, it.data?.game?.iconSubscript
                )
                mBinding.gameName.text = it.data?.game?.name
                mBinding.likeCount.text = NumberUtils.transSimpleCount(it.data?.likeCount ?: 0)
                mBinding.videoCount.text = NumberUtils.transSimpleCount(it.data?.videoCount ?: 0)
            } else if (it.status == Status.ERROR) {
                // do something
            }
        })

        mBinding.gameIcon.setOnClickListener {
            GameDetailActivity.startGameDetailActivity(
                this,
                gameId,
                BaseActivity.mergeEntranceAndPath(mEntrance, "视频合集")
            )
        }
        mBinding.gameName.setOnClickListener { mBinding.gameIcon.performClick() }

        DisplayUtils.transparentStatusBar(this)
    }

    override fun provideNavigationIcon(): Int {
        return R.drawable.ic_toolbar_back_white
    }

    override fun isAutoResetViewBackgroundEnabled(): Boolean = true

    override fun onDarkModeChanged() {
        mToolbar.setBackgroundColor(R.color.transparent.toColor(this))
        mToolbar.setNavigationIcon(R.drawable.ic_toolbar_back_white)
        mBinding.tabContainer.setBackgroundColor(R.color.background_white.toColor(this))
        for (i in 0 until mBinding.tabLayout.tabCount) {
            val tab = mBinding.tabLayout.getTabAt(i)
            if (tab != null) {
                val customView = tab.view.getChildAt(1) as? TextView
                customView?.setTextColor(ContextCompat.getColorStateList(this, R.color.text_tabbar_style))
            }
        }
    }

    companion object {

        @JvmStatic
        fun getIntent(context: Context, gameId: String, entrance: String?, path: String?): Intent {
            val intent = Intent(context, GameVideoActivity::class.java)
            intent.putExtra(EntranceConsts.KEY_GAMEID, gameId)
            intent.putExtra(EntranceConsts.KEY_ENTRANCE, mergeEntranceAndPath(entrance, path))
            return intent
        }
    }
}