package com.gh.gamecenter.subject

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import com.gh.base.DownloadToolbarActivity
import com.gh.common.util.DirectUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.common.utils.updateStatusBarColor
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.entity.SubjectData

/**
 * 专题详情
 */
open class SubjectActivity : DownloadToolbarActivity() {
    private var subjectData: SubjectData? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        updateStatusBarColor(com.gh.gamecenter.common.R.color.ui_surface, com.gh.gamecenter.common.R.color.ui_surface)
        subjectData = intent.getBundleExtra(NORMAL_FRAGMENT_BUNDLE)?.getParcelable(EntranceConsts.KEY_SUBJECT_DATA)
            ?: intent.getParcelableExtra(EntranceConsts.KEY_SUBJECT_DATA)
            ?: SubjectData("", "", false)
    }

    override fun showDownloadMenu() = true

    override fun getActivityNameInChinese() = "专题详情"

    override fun getBusinessId(): Pair<String, String> {
        return Pair(subjectData?.subjectId ?: "", "")
    }

    override fun provideNormalIntent(): Intent {
        return getTargetIntent(this, SubjectActivity::class.java, SubjectFragment::class.java)
    }

    override fun isAutoResetViewBackgroundEnabled() = true

    override fun onMenuItemClick(item: MenuItem?): Boolean {
        if (item!!.itemId == R.id.menu_download) {
            DirectUtils.directToDownloadManager(
                this,
                "游戏专题",
                gameColumnId = subjectData?.subjectId ?: "",
                gameColumnName = subjectData?.subjectName ?: "",
            )
            return true
        }
        return super.onMenuItemClick(item)
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()

        updateStatusBarColor(com.gh.gamecenter.common.R.color.ui_surface, com.gh.gamecenter.common.R.color.ui_surface)
    }

    companion object {
        /**
         * 启动专题页面
         */
        @JvmStatic
        fun startSubjectActivity(context: Context,
                                 id: String?,
                                 name: String?,
                                 isOrder: Boolean,
                                 exposureSourceList: ArrayList<ExposureSource>? = null,
                                 entrance: String?,
                                 subjectType: SubjectData.SubjectType = SubjectData.SubjectType.NORMAL
        ) {
            MtaHelper.onEvent("详情页面", "专题详情", name)

            val bundle = Bundle()
            bundle.putString(EntranceConsts.KEY_ENTRANCE, entrance)
            val subjectData = SubjectData(subjectId = id, subjectName = name, isOrder = isOrder, subjectType = subjectType)
            bundle.putParcelable(EntranceConsts.KEY_SUBJECT_DATA, subjectData)
            bundle.putString(EntranceConsts.KEY_ENTRANCE, entrance)
            if (exposureSourceList != null) {
                bundle.putParcelableArrayList(
                    EntranceConsts.KEY_EXPOSURE_SOURCE_LIST,
                    ArrayList(exposureSourceList)
                )
            }
            context.startActivity(
                getTargetIntent(
                    context,
                    SubjectActivity::class.java,
                    SubjectFragment::class.java,
                    bundle
                )
            )
        }

        /**
         * 启动广告专题页面
         */
        @JvmStatic
        fun startAdSubjectActivity(
            context: Context,
            adId: String,
            codeId: String,
            name: String,
            adIconActive: Boolean,
            exposureSourceList: ArrayList<ExposureSource>? = null,
            entrance: String?
        ) {
            val bundle = Bundle()
            bundle.putString(EntranceConsts.KEY_ENTRANCE, entrance)
            val subjectData =
                SubjectData(
                    subjectId = "",
                    adId = adId,
                    codeId = codeId,
                    subjectName = name,
                    isOrder = false,
                    isAdData = adIconActive
                )
            bundle.putParcelable(EntranceConsts.KEY_SUBJECT_DATA, subjectData)
            bundle.putString(EntranceConsts.KEY_ENTRANCE, entrance)
            if (exposureSourceList != null) {
                bundle.putParcelableArrayList(
                    EntranceConsts.KEY_EXPOSURE_SOURCE_LIST,
                    ArrayList(exposureSourceList)
                )
            }
            context.startActivity(
                getTargetIntent(
                    context,
                    SubjectActivity::class.java,
                    AdSubjectListFragment::class.java,
                    bundle
                )
            )
        }
    }
}