package com.duben.heliapp.manager

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import com.duben.library.utils.json.JsonUtil
import com.duben.heliapp.common.AppConfig
import com.duben.heliapp.common.Constant
import com.duben.heliapp.mvp.model.VedioBean
import com.duben.heliapp.utils.AppPreferencesManager
import com.duben.heliapp.utils.ToastUtil
import com.duben.heliapp.video.tx.newrecommend.NewTxVideoActivity
import com.duben.heliapp.video.tx.TxVideoActivity

/**
 * 本地视频缓存管理
 */
object LocalVedioManager {

    var vedioCache: VedioBean? = null

    /**
     * 提交当前视频数据
     */
    fun commitVedio(data: VedioBean) {
        AppConfig.vedioId = data.vedioId
        cacheVedio(data)
        TrackManager.getInstance().commitVedio(data)
    }

    /**
     * 内存、本地都缓存视频
     */
    private fun cacheVedio(data: VedioBean) {
        vedioCache = data
        AppPreferencesManager.get().put(Constant.LOCAL_VEDIO, JsonUtil.toJson(data))
    }

    /**
     * 关闭缓存视频
     */
    fun closeCacheVedio() {
        vedioCache = null
        AppPreferencesManager.get().put(Constant.LOCAL_VEDIO, "")
    }

    /**
     * 是否有缓存的视频
     *
     * true-有
     */
    fun isCacheVedio(): Boolean {
        // 有缓存 且 不是推荐剧
        if (vedioCache != null && !vedioCache!!.isRecommendVedio) {
            return true
        }

        val localVedio = AppPreferencesManager.get().getString(Constant.LOCAL_VEDIO, "")
        if (!TextUtils.isEmpty(localVedio)) {
            try {
                val bean = JsonUtil.parseJson(localVedio, VedioBean::class.java)
                if (bean != null && !bean.isRecommendVedio) {
                    return true
                }
            } catch (e: Exception) {
                e.printStackTrace()
                return false
            }
        }
        return false
    }

    /**
     * 展示缓存视频
     */
    fun getCacheVedio(): VedioBean? {
        if (vedioCache != null) {
            return vedioCache as VedioBean
        }

        val localVedio = AppPreferencesManager.get().getString(Constant.LOCAL_VEDIO, "")
        if (!TextUtils.isEmpty(localVedio)) {
            return JsonUtil.parseJson(localVedio, VedioBean::class.java)
        }

        return null
    }

    /**
     * 根据类型跳转对应视频详情页 [自有或穿山甲]
     */
    fun startVedioDetailActivityForType(
        activity: Activity,
        data: VedioBean?,
        isPlayNext: Boolean = false,
        isThirdId: Boolean = false
    ) {
        if (data?.createType == 1) {
            //自有短剧
            val bundle = Bundle()
            bundle.putString(Constant.VEDIO_BEAN, JsonUtil.toJson(data))
            bundle.putBoolean(Constant.VEDIO_NEXT, isPlayNext)
            bundle.putBoolean(Constant.VEDIO_THIRD, isThirdId)
            readyGo(activity, TxVideoActivity::class.java, bundle)
        } else if (data?.createType == 2) {
            // 穿山甲
        } else {
            //推荐短剧
            val bundle = Bundle()
            bundle.putString(Constant.VEDIO_BEAN, JsonUtil.toJson(data))
            readyGo(activity, NewTxVideoActivity::class.java, bundle)
        }

    }

    fun readyGo(activity: Activity, clazz: Class<*>, bundle: Bundle) {
        val intent = Intent(activity, clazz)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        if (null != bundle) {
            intent.putExtras(bundle)
        }
        activity.startActivity(intent)
    }
}