package com.duben.microtribe.manager

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import com.bytedance.sdk.dp.DPDrama
import com.bytedance.sdk.dp.DPSdk
import com.bytedance.sdk.dp.IDPWidgetFactory
import com.duben.microtribe.ad.express.ExpressManager
import com.duben.library.utils.json.JsonUtil
import com.duben.microtribe.common.AppConfig
import com.duben.microtribe.common.Constant
import com.duben.microtribe.mvp.model.VedioBean
import com.duben.microtribe.utils.AppPreferencesManager
import com.duben.microtribe.utils.ToastUtil
import com.duben.microtribe.video.csj.DramaApiDetailActivity
import com.duben.microtribe.video.tx.newrecommend.NewTxVideoActivity
import com.duben.microtribe.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) {
            requestDrama(activity, data)
        } else {
            //推荐短剧
            val bundle = Bundle()
            bundle.putString(Constant.VEDIO_BEAN, JsonUtil.toJson(data))
            readyGo(activity, NewTxVideoActivity::class.java, bundle)
        }

        if (!UserManager.getInstance().vipFlag) {
            ExpressManager.instance.preLoadAd()
        }
    }

    // 请求穿山甲数据
    private fun requestDrama(activity: Activity, data: VedioBean?) {
        if (DPSdk.isStartSuccess() && data != null) {
            DPSdk.factory().requestDrama(
                arrayListOf(data.thirdId.toLong()), object : IDPWidgetFactory.DramaCallback {
                    override fun onError(p0: Int, p1: String?) {
                        ToastUtil.show(activity, "网络错误！")
                    }

                    override fun onSuccess(
                        dataList: MutableList<out DPDrama>?,
                        info: MutableMap<String, Any>?
                    ) {
                        dataList?.takeIf { it.isNotEmpty() }?.first()?.let {
                            DramaApiDetailActivity.outerDrama = it
                            //穿山甲短剧
                            val bundle = Bundle()
                            bundle.putString(Constant.VEDIO_BEAN, JsonUtil.toJson(data))
                            readyGo(activity, DramaApiDetailActivity::class.java, bundle)
                        }

                    }

                }
            )

        } else {
            ToastUtil.show(activity, "网络错误！")
        }
    }

    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)
    }
}