package com.gh.gamecenter.forum.home.follow

import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Build
import android.os.Bundle
import android.widget.TextView
import androidx.activity.viewModels
import androidx.core.content.ContextCompat
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.activity.BaseActivity
import com.gh.gamecenter.common.utils.DarkModeUtils.isDarkModeOn
import com.gh.gamecenter.common.utils.toArrayList
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.entity.FollowUserEntity
import com.gh.gamecenter.forum.home.follow.fragment.FollowDynamicFragment
import com.gh.gamecenter.forum.home.follow.viewmodel.FollowDynamicViewModel

class FollowDynamicActivity : BaseActivity() {

    private val viewModel by viewModels<FollowDynamicViewModel>()

    override fun getLayoutId(): Int = R.layout.activity_amway

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (Build.VERSION.SDK_INT != Build.VERSION_CODES.O) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }
        DisplayUtils.transparentStatusAndNavigation(this)
        DisplayUtils.setLightStatusBar(this, true)
        val containerFragment = supportFragmentManager.findFragmentByTag(FollowDynamicFragment::class.java.name)
            ?: FollowDynamicFragment().with(intent.extras)
        // 若 placeholder 外层为 RelativeLayout 的话，会出现莫名的偏移
        supportFragmentManager.beginTransaction()
            .replace(R.id.placeholder, containerFragment, FollowDynamicFragment::class.java.name)
            .commitAllowingStateLoss()
    }

    override fun onDarkModeChanged() {
        val tv = findViewById<TextView>(ID_NIGHT_INDICATOR)
        if (tv != null) {
            tv.text = if (isDarkModeOn(this)) "深色模式" else "浅色模式"
            tv.alpha = if (isDarkModeOn(this)) 0.8f else 0.15f
        }
        if (isAutoResetViewBackgroundEnabled) {
            updateStaticViewBackground(window.decorView)
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.navigationBarColor = ContextCompat.getColor(this, com.gh.gamecenter.common.R.color.ui_surface)
        }
    }

    override fun handleBackPressed(): Boolean {
        viewModel.finish()
        return true
    }

    companion object {

        const val KEY_SELECTED_POSITION = "key_selected_position"
        const val KEY_USERS = "key_users"

        fun start(context: Context, position: Int, followedList: List<FollowUserEntity>) {
            val intent = Intent(context, FollowDynamicActivity::class.java)
            intent.putParcelableArrayListExtra(KEY_USERS, followedList.toArrayList())
            intent.putExtra(KEY_SELECTED_POSITION, position)

            context.startActivity(intent)
        }
    }
}