package com.gh.base

import android.graphics.Typeface
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.gh.download.DownloadManager
import com.gh.gamecenter.DownloadManagerActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.activity.ToolBarActivity
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.core.utils.SPUtils.getBoolean
import com.gh.gamecenter.entity.GameUpdateEntity
import com.gh.gamecenter.eventbus.EBDownloadStatus
import com.gh.gamecenter.packagehelper.PackageViewModel
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

// TODO:移动到module_download模块中
abstract class DownloadToolbarActivity : ToolBarActivity() {

    private var mDownloadCountHint: TextView? = null
    private var mPackageViewModel: PackageViewModel? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (!getBoolean(Constants.SP_TEENAGER_MODE) && showDownloadMenu()) {
            mPackageViewModel = viewModelProvider(PackageViewModel.Factory())
            mPackageViewModel?.filterSameUpdateLiveData?.observe(this) { updateList: List<GameUpdateEntity> ->
                updateDownloadCountHint(updateList)
            }
        }
    }

    override fun setToolbarMenu(res: Int) {
        // 青少年模式下要隐藏下载按钮
        if (getBoolean(Constants.SP_TEENAGER_MODE) && res == R.menu.menu_download) return
        super.setToolbarMenu(res)
    }

    override fun inflateMenu(res: Int) {
        super.inflateMenu(res)
        if (!getBoolean(Constants.SP_TEENAGER_MODE) && showDownloadMenu()) {
            createDownloadMenu(res)
        }
    }

    private fun createDownloadMenu(res: Int) {
        if (res != R.menu.menu_download) {
            menuInflater.inflate(R.menu.menu_download, mActionMenuView.menu)
        }
        if (mPackageViewModel != null) {
            updateDownloadCountHint(mPackageViewModel?.filterSameUpdateLiveData?.value)
        }
        val downloadMenuView = mActionMenuView.menu.findItem(R.id.menu_download).actionView
        mDownloadCountHint = downloadMenuView?.findViewById(R.id.menu_download_count_hint)
        mDownloadCountHint?.typeface = Typeface.createFromAsset(assets, Constants.DIN_FONT_PATH)
    }

    override fun onMenuItemClick(item: MenuItem?): Boolean {
        if (item!!.itemId == R.id.menu_download) {
//            MtaHelper.onEvent("下载管理", "下载管理入口", getActivityNameInChinese());
            val intent = DownloadManagerActivity.getDownloadMangerIntent(this, mEntrance)
            startActivity(intent)
            return true
        }
        return super.onMenuItemClick(item)
    }

    private fun updateDownloadCountHint(updateList: List<GameUpdateEntity>?) {
        if (mDownloadCountHint == null) return
        val count = DownloadManager.getInstance().getDownloadOrUpdateCount(updateList)
        if (count != null) {
            mDownloadCountHint?.visibility = View.VISIBLE
            mDownloadCountHint?.text = count
            val params = mDownloadCountHint?.layoutParams
            params?.width = if (count.isEmpty()) 6F.dip2px() else ConstraintLayout.LayoutParams.WRAP_CONTENT
            params?.height = if (count.isEmpty()) 6F.dip2px() else 14F.dip2px()
            (params as? ViewGroup.MarginLayoutParams)?.setMargins(
                0,
                if (count.isEmpty()) 0 else (-4F).dip2px(),
                if (count.isEmpty()) (-4F).dip2px() else (-8F).dip2px(),
                0
            )
            mDownloadCountHint?.setPadding(
                if (count.isEmpty()) 0 else 4F.dip2px(),
                0,
                if (count.isEmpty()) 0 else 4F.dip2px(),
                0
            )
            mDownloadCountHint?.minWidth = if (count.isEmpty()) 0 else 14F.dip2px()
            mDownloadCountHint?.layoutParams = params
        } else {
            mDownloadCountHint?.visibility = View.GONE
        }
    }

    protected open fun showDownloadMenu(): Boolean {
        return false
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(status: EBDownloadStatus?) {
        if (!getBoolean(Constants.SP_TEENAGER_MODE) && showDownloadMenu() && mPackageViewModel != null) {
            updateDownloadCountHint(mPackageViewModel?.filterSameUpdateLiveData?.value)
        }
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        if (showDownloadMenu() && getMenuItem(R.id.menu_download) != null) {
            (getMenuItem(R.id.menu_download).actionView?.findViewById(R.id.menu_download_iv) as ImageView).setImageResource(
                R.drawable.toolbar_download
            )
        }
    }
}