package com.duben.microtribe.video.csj

import android.util.Log

import com.bytedance.sdk.dp.DPDramaDetailConfig
import com.bytedance.sdk.dp.DPSdk
import com.bytedance.sdk.dp.DPWidgetDrawParams
import com.bytedance.sdk.dp.IDPAdListener
import com.bytedance.sdk.dp.IDPDramaListener
import com.bytedance.sdk.dp.IDPDrawListener
import com.bytedance.sdk.dp.IDPWidget
import com.duben.microtribe.R
import com.duben.microtribe.common.AppConfig
import com.duben.microtribe.common.Constant
import com.duben.microtribe.manager.LocalVedioManager
import com.duben.microtribe.mvp.model.IndexList
import com.duben.microtribe.mvp.model.NineShowBean
import com.duben.microtribe.mvp.model.VipBean
import com.duben.microtribe.mvp.presenters.VideoPresenter
import com.duben.microtribe.mvp.views.VideoView
import com.duben.microtribe.ui.fragment.base.BaseFragment

/**
 * Created by limingqi on 2023/1/17
 */
class DramaTabFragment : BaseFragment(), VideoView {

    private val videoPresenter by lazy { VideoPresenter() }


    companion object {
        private const val TAG = "DramaTabFragment"
    }

    private var dpWidget: IDPWidget? = null

    private var isInited = false

    override fun initViewsAndEvents() {
        if (DPSdk.isStartSuccess()) {
            init()
        }
    }

    override fun getContentViewLayoutID() = R.layout.drama_home_frag_wrapper

    private fun init() {
        videoPresenter.attachView(this)

        if (isInited) {
            return
        }
        initDrawWidget()
        isInited = true
    }

    private fun initDrawWidget() {
        if (!DPHolderManager.isDPStarted) {
            this.view?.postDelayed({
                initDrawWidget()
            }, 100)
            return
        }

        val dramaDetailConfig = DPDramaDetailConfig.obtain(DPDramaDetailConfig.SPECIFIC_DETAIL)
            .setEnterDelegate { context, drama, current ->
                DramaApiDetailActivity.outerDrama = drama
                videoPresenter.getIndexList("" + drama.id, true)
            }
            .hideMore(false)
            .hideLeftTopTips(false, null)
            .listener(dramaListener) // 短剧详情页视频播放回调
            .adListener(dramaAdListener) // 短剧详情页激励视频回调

        dpWidget = DPSdk.factory().createDraw(
            DPWidgetDrawParams.obtain()
                .adOffset(0) //单位 dp，为 0 时可以不设置
                .drawContentType(DPWidgetDrawParams.DRAW_CONTENT_TYPE_ONLY_DRAMA)
                .drawChannelType(DPWidgetDrawParams.DRAW_CHANNEL_TYPE_RECOMMEND)
                .hideClose(true, null)
                .hideChannelName(true)
                .dramaDetailConfig(dramaDetailConfig)
                .listener(drawListener) // 混排流内视频监听
                .adListener(drawAdListener) // 混排流内广告监听
        )

        dpWidget?.fragment?.userVisibleHint = userVisibleHint
        dpWidget?.let {
            if (isAdded.not()) {
                return
            }
            childFragmentManager.beginTransaction().apply {
                replace(R.id.fl_container, it.fragment)
                this.commitNowAllowingStateLoss()
            }
        }
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume")

        if (AppConfig.fragmentClickFlag == Constant.FRAGMENT_CLICK_TWO) {
            dpWidget?.fragment?.onResume()

            dpWidget?.fragment?.userVisibleHint = true
        }
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "onPause")
        dpWidget?.fragment?.onPause()

        dpWidget?.fragment?.userVisibleHint = false

    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        dpWidget?.fragment?.onHiddenChanged(hidden)
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
//        dpWidget?.fragment?.userVisibleHint = isVisibleToUser
    }

    private var dramaListener: IDPDramaListener = object : IDPDramaListener() {
        override fun onDPSeekTo(position: Int, time: Long) {
            super.onDPSeekTo(position, time)
            Log.d(TAG, "onDPSeekTo:")
        }

        override fun onDPPageChange(position: Int, map: MutableMap<String, Any>?) {
            super.onDPPageChange(position, map)
            Log.d(TAG, "onDPPageChange:" + map?.toString())
        }

        override fun onDPVideoPlay(map: MutableMap<String, Any>?) {
            super.onDPVideoPlay(map)
            Log.d(TAG, "onDPVideoPlay:" + map?.toString())
        }

        override fun onDPVideoPause(map: MutableMap<String, Any>?) {
            super.onDPVideoPause(map)
            Log.d(TAG, "onDPVideoPause:" + map?.toString())
        }

        override fun onDPVideoContinue(map: MutableMap<String, Any>?) {
            super.onDPVideoContinue(map)
            Log.d(TAG, "onDPVideoContinue:" + map?.toString())
        }

        override fun onDPVideoCompletion(map: MutableMap<String, Any>?) {
            super.onDPVideoCompletion(map)
            Log.d(TAG, "onDPVideoCompletion:" + map?.toString())
        }

        override fun onDPVideoOver(map: MutableMap<String, Any>?) {
            super.onDPVideoOver(map)
            Log.d(TAG, "onDPVideoOver:" + map?.toString())
        }

        override fun onDPClose() {
            super.onDPClose()
            Log.d(TAG, "onDPClose")
        }

        override fun onDPRequestStart(map: MutableMap<String, Any>?) {
            super.onDPRequestStart(map)
            Log.d(TAG, "onDPRequestStart:" + map?.toString())
        }

        override fun onDPRequestFail(code: Int, msg: String?, map: MutableMap<String, Any>?) {
            super.onDPRequestFail(code, msg, map)
            Log.d(TAG, "onDPRequestFail:" + map?.toString())
        }

        override fun onDPRequestSuccess(list: MutableList<MutableMap<String, Any>>?) {
            super.onDPRequestSuccess(list)
            list?.forEach { it ->
                Log.d(TAG, "onDPRequestSuccess:" + it.toString())
            }
        }

        override fun onDramaSwitch(map: MutableMap<String, Any>?) {
            super.onDramaSwitch(map)
            Log.d(TAG, "onDramaSwitch:" + map?.toString())
        }
    }

    private val drawListener = object : IDPDrawListener() {
        override fun onDPRefreshFinish() {
            Log.d(TAG, "onDPRefreshFinish")
        }

        override fun onDPPageChange(position: Int) {
            Log.d(TAG, "onDPPageChange: $position")
        }

        override fun onDPVideoPlay(map: Map<String, Any>) {
            Log.d(TAG, "onDPVideoPlay: $map")
        }

        override fun onDPVideoCompletion(map: Map<String, Any>) {
            Log.d(TAG, "onDPVideoCompletion: $map")
//            dpWidget?.pauseForWatchTogether()
//            videoPresenter.getIndexList("" + map["drama_id"], true)
        }

        override fun onDPVideoOver(map: Map<String, Any>) {
            Log.d(TAG, "onDPVideoOver: $map")
        }

        override fun onDPClose() {
            Log.d(TAG, "onDPClose")
        }

        override fun onDPRequestStart(map: Map<String, Any>?) {
            Log.d(TAG, "onDPRequestStart: $map")
        }

        override fun onDPRequestSuccess(list: List<Map<String, Any>>) {
            Log.d(TAG, "onDPRequestSuccess: $list")
        }

        override fun onDPRequestFail(
            code: Int, msg: String, map: Map<String, Any>?
        ) {
            Log.d(TAG, "onDPRequestFail: code = $code, msg = $msg, map = $map")
        }

        override fun onDPClickAuthorName(map: Map<String, Any>) {
            Log.d(TAG, "onDPClickAuthorName: $map")
        }

        override fun onDPClickAvatar(map: Map<String, Any>) {
            Log.d(TAG, "onDPClickAvatar: $map")
        }

        override fun onDPClickComment(map: Map<String, Any>) {
            Log.d(TAG, "onDPClickComment: $map")
        }

        override fun onDPClickLike(
            isLike: Boolean, map: Map<String, Any>
        ) {
            Log.d(TAG, "onDPClickLike: isLike = $isLike, map = $map")
        }

        override fun onDPVideoPause(map: Map<String, Any>) {
            Log.d(TAG, "onDPVideoPause: $map")
        }

        override fun onDPVideoContinue(map: Map<String, Any>) {
            Log.d(TAG, "onDPVideoContinue: $map")
        }

        override fun onDPClickShare(map: MutableMap<String, Any>?) {
            Log.d(TAG, "onDPClickShare $map")
        }
    }

    private val dramaAdListener: IDPAdListener = object : IDPAdListener() {
        override fun onDPAdRequest(map: Map<String?, Any?>) {
            Log.d(TAG, "onDPAdRequest map =  $map")
        }

        override fun onDPAdRequestSuccess(map: Map<String?, Any?>) {
            Log.d(TAG, "onDPAdRequestSuccess map = $map")
        }

        override fun onDPAdRequestFail(code: Int, msg: String?, map: Map<String?, Any?>) {
            Log.d(TAG, "onDPAdRequestFail map = $map")
        }

        override fun onDPAdFillFail(map: Map<String?, Any?>) {
            Log.d(TAG, "onDPAdFillFail map = $map")
        }

        override fun onDPAdShow(map: Map<String?, Any?>) {
            Log.d(TAG, "onDPAdShow map = $map")
        }

        override fun onDPAdPlayStart(map: Map<String?, Any?>) {
            Log.d(TAG, "onDPAdPlayStart: map = $map")
        }

        override fun onDPAdPlayPause(map: Map<String?, Any?>) {
            Log.d(TAG, "onDPAdPlayPause map = $map")
        }

        override fun onRewardVerify(map: MutableMap<String, Any>?) {
            Log.d(TAG, "onRewardVerify map = $map")
        }

        override fun onSkippedVideo(map: MutableMap<String, Any>?) {
            Log.d(TAG, "onSkippedVideo map = $map")
        }

        override fun onDPAdPlayContinue(map: Map<String?, Any?>) {
            Log.d(TAG, "onDPAdPlayContinue map = $map")
        }

        override fun onDPAdPlayComplete(map: Map<String?, Any?>) {
            Log.d(TAG, "onDPAdPlayComplete map = $map")
        }

        override fun onDPAdClicked(map: Map<String?, Any?>) {
            Log.d(TAG, "onDPAdClicked map = $map")
        }
    }

    private val drawAdListener: IDPAdListener = object : IDPAdListener() {
        override fun onDPAdRequest(map: Map<String?, Any?>) {
            Log.d(TAG, "onDPAdRequest map =  $map")
        }

        override fun onDPAdRequestSuccess(map: Map<String?, Any?>) {
            Log.d(TAG, "onDPAdRequestSuccess map = $map")
        }

        override fun onDPAdRequestFail(code: Int, msg: String?, map: Map<String?, Any?>) {
            Log.d(TAG, "onDPAdRequestFail map = $map")
        }

        override fun onDPAdFillFail(map: Map<String?, Any?>) {
            Log.d(TAG, "onDPAdFillFail map = $map")
        }

        override fun onDPAdShow(map: Map<String?, Any?>) {
            Log.d(TAG, "onDPAdShow map = $map")
        }

        override fun onDPAdPlayStart(map: Map<String?, Any?>) {
            Log.d(TAG, "onDPAdPlayStart: map = $map")
        }

        override fun onDPAdPlayPause(map: Map<String?, Any?>) {
            Log.d(TAG, "onDPAdPlayPause map = $map")
        }

        override fun onDPAdPlayContinue(map: Map<String?, Any?>) {
            Log.d(TAG, "onDPAdPlayContinue map = $map")
        }

        override fun onDPAdPlayComplete(map: Map<String?, Any?>) {
            Log.d(TAG, "onDPAdPlayComplete map = $map")
        }

        override fun onDPAdClicked(map: Map<String?, Any?>) {
            Log.d(TAG, "onDPAdClicked map = $map")
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        videoPresenter.detachView()
    }

    override fun getIndexListSuc(indexList: IndexList) {
        LocalVedioManager.startVedioDetailActivityForType(
            requireActivity(),
            indexList.vedioMsg,
            true,
            isThirdId = true
        )
    }

    override fun getIndexListFail() {
        showToast("网络错误！")
    }

    override fun collectSuc() {
    }

    override fun collectFail() {
    }

    override fun cancelCollectSuc() {
    }

    override fun cancelCollectFail() {
    }

    override fun unlockSuc(indexList: IndexList) {
    }

    override fun unlockFail() {
    }

    override fun showTurnSuc(data: NineShowBean) {

    }

    override fun getVipProductSuc(data: VipBean) {

    }


}