class CastControlComponent(feedContext: IFeedContext) : AbsFeedComponent(feedContext) {
    companion object {
        const val TAG = "CastControlComponent"
    }

    /** 投屏服务实例 */
    private val castService = ICastService.instance

    /** 投屏ViewModel */
    private val castVM = ICastService.instance.getViewModel()

    /** 投屏悬浮组件 */
    private var castFlowItem: ICastFlowItemHelper? = null

    /** 场景标识，每创建一个component会生成一个 */
    private var compenentId: String? = null

    /** 视频是否需要延迟暂停，有时候需要暂停的时候视频首帧还没有渲染，需要在首帧渲染后再暂停 */
    private var isDelayPause: Boolean = false

    /** 请求列表监听 */
    private var requestListObserver = Observer<String> {
        // 非当前场景不请求
        if (castVM?.castComponentId()?.value != compenentId) {
            return@Observer
        }
        getVideoIdList(it)
    }

    private var isPauseFromCast = false

    override fun onPageSelected(position: Int) {
        super.onPageSelected(position)
        val aweme = feedContext.feedDataContext().getItem(position)
        tryShowControlView(aweme)
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        compenentId = this.javaClass.name + "@" + Integer.toHexString(System.identityHashCode(this))
        castVM?.showComponentId()?.value = compenentId
        super.onViewCreated(view, savedInstanceState)
        val fragment = feedContext.getFragmentP() ?: return
        feedContext.feedPlayerContext()
            .observer(fragment, object : FeedPlayListener {
                override fun onResumePlay(param: FeedResumePlayParam) {
                    if (castVM?.showComponentId()?.value != compenentId) {
                        return
                    }
                    // 当前视频正在投屏或者搜索面板展示时暂停投屏
                    if (castService.isCurrentVideoCasting(
                            feedContext.feedDataContext().getCurrentItem()?.aid
                        ) ||
                        castVM?.isSearchViewShowing()?.value == true
                    ) {
                        feedContext.feedPlayerContext()
                            .tryPause(feedContext.feedDataContext().getCurrentItem(), false)
                        return
                    }
                    super.onResumePlay(param)
                }

                override fun onPausePlay(param: FeedPausePlayParam) {
                    super.onPausePlay(param)
                }

                override fun onRenderFirstFrame(param: FeedFirstFrameParam) {
                    super.onRenderFirstFrame(param)
                    // 首帧渲染后进行延迟暂停
                    if (castService.isCurrentVideoCasting(
                            feedContext.feedDataContext().getCurrentItem()?.aid
                        ) ||
                        castVM?.isSearchViewShowing()?.value == true || isDelayPause
                    ) {
                        isPauseFromCast = true
                        isDelayPause = false
                        feedContext.feedPlayerContext()
                            .tryPause(feedContext.feedDataContext().getCurrentItem(), false)
                        return
                    }
                }
            })
        castVM?.isSearchViewShowing()?.observe(fragment) {
            if ((castVM?.showingParams()?.value?.aweme?.aid != feedContext.feedDataContext()
                    .getCurrentItem()?.aid)
            ) {
                return@observe
            }
            if (it) {
                feedContext.feedPlayerContext()
                    .tryPause(feedContext.feedDataContext().getCurrentItem(), false)
            } else {
                feedContext.feedPlayerContext()
                    .tryResume(feedContext.feedDataContext().getCurrentItem())
            }
        }
        castVM?.castPlayState()?.observe(fragment) {
            // 非当前场景不进行操作
            if (castVM?.castComponentId()?.value != compenentId) {
                return@observe
            }
            if (it == CastPlayState.STATE_COMPLETE) {
                // 非联投场景
                if (CastSourceUIManager.getCastSourceImpl()?.canContinuousPlay() != true) {
                    // 移除播控页面
                    castVM?.castingParams()?.value = null
                    castVM?.castPlayState()?.value = null
                    castVM?.isShowFlowItem()?.value = true
                    // 进行挂件展示，一定时间后移除
                    ICastService.instance.startDelayHideCastFlowItemRunnable()
                    return@observe
                }
                // 联投场景
                val awemeList = feedContext.feedDataContext().getItemList()
                val currentCastAweme = castVM?.castingParams()?.value?.aweme
                var nowPosition = -1
                awemeList?.forEachIndexed { index, aweme ->
                    if (aweme?.aid == currentCastAweme?.aid) {
                        nowPosition = index
                    }
                }
                // 当前列表已经播完，或当前列表没有正在投屏的视频，退出投屏
                if (nowPosition == awemeList.size - 1 || nowPosition == -1) {
                    castVM?.castingParams()?.value = null
                    castVM?.castPlayState()?.value = null
                    return@observe
                }
                for (index in nowPosition + 1..awemeList.size - 1) {
                    if (index > awemeList.size - 1) {
                        return@observe
                    }
                    val nextAweme = awemeList.get(index)
                    val nextCastParams = CastParams.builder(nextAweme) {
                        this.fragment = WeakReference(fragment)
                        activity = WeakReference(feedContext.getActivityP() as FragmentActivity?)
                        context = WeakReference(feedContext.getContextP())
                        castHelper = object : CastParams.DefaultBusinessHelper() {
                            override fun getCurrentPositon(): Long {
                                return feedContext.feedPlayerContext().currentPosition(nextAweme)
                                    .toLong()
                            }

                            override fun getSearchViewTargetHeight(): Int {
                                return 0
                            }
                        }
                        isLandscape = false
                        sceneId = CastSourceConstant.SCENE_ID_DY_FEED
                        enterFrom = eventType
                    }
                    // 如果是非仅可投设备和仅可投视频则跳过该视频
                    if (castService.isShowCastIcon(nextAweme, false, eventType) &&
                        castService.isCastEnable(nextAweme) && !(castService.isOfficialDeviceOnlyVideo(
                            nextCastParams
                        )
                                && !CastUIUtils.isOfficialDevice(CastSourceUIManager.getCastingDevice()))
                    ) {
                        // 当前视频为正在投屏的视频，自动跳转到下一个可投视频
                        if (currentAweme?.aid?.equals(castVM?.castingParams()?.value?.aweme?.aid) == true) {
                            if (castVM?.showComponentId()?.value == compenentId) {
                                // 下个视频直接进行投屏
                                castVM?.isContinuingCast()?.value = true
                                feedContext.feedViewPagerContext().moveToIndex(index)
                            }
                        }
                        // 对齐TV端回调
                        castVM?.castingVideoIdFromTV()?.value = nextAweme.video?.playAddr?.originUri
                        // 如果当前页面就是要跳转的页面，则onPageSelected不会再次调用，这里需要走一遍onPageSelected逻辑
                        if (index == feedContext.feedDataContext().getCurrentPosition()) {
                            tryShowControlView(nextAweme)
                        }
                        return@observe
                    }
                }
                // 走到这说明列表里没有可进行投屏的视频了，退出联投
                castVM?.castingParams()?.value = null
                castVM?.castPlayState()?.value = null
            } else if (it == CastPlayState.STATE_STOP) {
                // 手动退出后移除播控
                castVM?.isShowFlowItem()?.value = false
                castVM?.castingParams()?.value = null
                castVM?.castComponentId()?.value = null
                castVM?.castPlayState()?.value = null
                // todo 收到stop SDK内部没有把状态清除，这里需要看一下是外部清除还是内部自动清除
                if (!castService.isCurrentVideoCasting(
                        feedContext.feedDataContext().getCurrentItem()?.aid
                    )
                ) {
                    return@observe
                }
                feedContext.feedPlayerContext().seek(
                    feedContext.feedDataContext().getCurrentItem(),
                    castVM?.castProgress()?.value ?: 0f
                )
            }
        }
        LiveStatusViewModel.get(activity).feedFollowStatusLiveData.observe(fragment) {
            if (Mob.EnterFrom.HOMEPAGE_FOLLOW != eventType) return@observe
            if (it.isShowing) {
                if (castVM?.isShowFlowItem()?.value == true) {
                    castVM?.isShowFlowItem()?.value = false
                }
            } else {
                tryShowControlView(currentAweme)
            }
        }
        castVM?.isShowFlowItem()?.observe(fragment) {
            Log.d(TAG, "the showComponentId is:" + castVM?.showComponentId()?.value)
            if (castVM?.showComponentId()?.value != compenentId) {
                return@observe
            }
            if (it) {
                if (Mob.EnterFrom.HOMEPAGE_FOLLOW == eventType && LiveStatusViewModel.get(activity).feedFollowStatusLiveData.value?.isShowing == true) {
                    return@observe
                }
                addFlowItem()
            } else {
                removeFlowItem()
            }
        }
        castVM?.castingParams()?.observe(fragment) {
            if (it == null || !castService.isCurrentVideoCasting(
                    feedContext.feedDataContext().getCurrentItem()?.aid
                )
            ) {
                if (castVM?.castComponentId()?.value != compenentId) return@observe
                feedContext.feedPlayerContext().getPlayerManager().unmuteVideo()
                if (feedContext.feedDataContext().getCurrentItem()?.aid == castVM?.showingParams()?.value?.aweme?.aid && isPauseFromCast) {
                    feedContext.feedPlayerContext()
                        .tryResume(feedContext.feedDataContext().getCurrentItem())
                    isPauseFromCast = false
                }
                return@observe
            }
            isPauseFromCast = true
            // 投屏时当前视频正在播放
            if (feedContext.feedPlayerContext()
                    ?.getPlayerManager()?.simPlayer?.playState?.isPlaying == true
            ) {
                feedContext.feedPlayerContext()
                    .tryPause(feedContext.feedDataContext().getCurrentItem(), false)
            } else {
                if (feedContext.feedPlayerContext()
                        ?.getPlayerManager()?.simPlayer?.playState?.isPaused != true
                ) {
                    isDelayPause = true
                }
            }

        }

        // 请求投屏数据列表的入口
        castVM?.castingVideoIdFromTV()?.observeForever(requestListObserver)
//        ViewModelProviders.of(feedContext.getActivityP() as FragmentActivity).get(CleanModeViewModel::class.java).observeCleanModeCommand(fragment) {
////'            if (castVM?.showComponentId()?.value != compenentId) return@observeCleanModeCommand
////            if (it.stateOn) {
////                if (castFlowItem != null) {
////                    (castFlowItem as? View)?.visibility = View.GONE
////                }
////            } else {
////                if (castFlowItem != null) {
////                    (castFlowItem as? View)?.visibility = View.VISIBLE
////                }
////            }'
//        }

    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
        super.onPageScrolled(position, positionOffset, positionOffsetPixels)
    }

    fun addFlowItem() {
        ServiceManager.get().getService(IRecommendFeedComponentService::class.java)
            ?.feedFragmentPanelService?.lastFissionFeedPendantManager?.isPendantVisible()
        val container = (feedContext.getFragmentP()?.view as? ViewGroup) ?: return
        val context = feedContext.getContextP() ?: return
        if (castFlowItem == null) {
            castFlowItem = castService.showCastFlowItem(context, container)
        } else {
            castService.showCastFlowItem(context, container)
        }
        castFlowItem?.updateIconState(castService.isCanCastType(currentAweme) && castService.isCastEnable(currentAweme))
    }

    fun removeFlowItem() {
        castFlowItem?.let {
            it.hideCastFlowItem()
            castFlowItem = null
        }
    }

    override fun onRefreshResult(list: List<Aweme>) {
        super.onRefreshResult(list)
        if (castVM?.castComponentId()?.value == compenentId|| (castVM?.isShowFlowItem()?.value == true && castVM?.showComponentId()?.value == compenentId)) {
            val groupIdList = mutableListOf<String>()
            list?.forEach { aweme ->
                if (castService.isCanCastType(aweme)) {
                    aweme.aid?.let { itemGroupId ->
                        groupIdList.add(itemGroupId)
                    }
                }
            }
            // 提前请求视频是否可以投屏
            castService.fetchCastEnable(groupIdList)
        }
        if (castVM?.castComponentId()?.value != compenentId) {
            return
        }
        // 投屏列表数据需要更新，手动强制请求下
        val currentVideoId = castVM?.castingParams()?.value?.aweme?.video?.playAddr?.originUri
        castVM?.castingVideoIdFromTV()?.value = currentVideoId
    }

    fun getVideoIdList(currentVideoId: String?) {
        if (currentVideoId == null) return
        val videoList: MutableList<Aweme> = mutableListOf()
        val awemeList = feedContext.feedDataContext().getItemList()
        if (awemeList.isNullOrEmpty()) {
            return
        }
        fetchCastEnable()
        var nIndex = -1
        for (index in 0..awemeList.size - 1) {
            if (awemeList.get(index)?.video?.playAddr?.originUri == currentVideoId) {
                nIndex = index
                Log.d("liubo_con", "now the show is :" + castVM?.showingParams()?.value?.aweme?.desc)
                Log.d("liubo_con", "now the cast is :" + castVM?.castingParams()?.value?.aweme?.desc)
                val isCurrentPage = castVM?.showingParams()?.value?.aweme?.aid == castVM?.castingParams()?.value?.aweme?.aid
                if (currentVideoId != castVM?.castingParams()?.value?.aweme?.video?.playAddr?.originUri
                    && castVM?.showComponentId()?.value == compenentId &&
                    castVM?.castPlayState()?.value != CastPlayState.STATE_COMPLETE &&
                    isCurrentPage) {
                    castVM?.isContinuingCast()?.value = true
                    feedContext.feedViewPagerContext().moveToIndex(index)
                } else {
                    updateCastingParams(awemeList.get(index))
                }
                break
            }
        }
        if (nIndex == -1) {
            Log.d(TAG, "update all list")
            for (index in 0..awemeList.size - 1) {
                val aweme = awemeList.get(index)
                if (castService.isShowCastIcon(aweme, false, eventType) &&
                    castService.isCastEnable(aweme) && !videoList.contains(aweme)
                ) {
                    videoList.add(aweme)
                    if (videoList.size >= 20) {
                        break
                    }
                }
            }
        } else {
            Log.d(TAG, "update part list")
            for (index in nIndex + 1..awemeList.size - 1) {
                val aweme = awemeList.get(index)
                if (castService.isShowCastIcon(aweme, false, eventType) &&
                    castService.isCastEnable(aweme) && !videoList.contains(aweme)
                ) {
                    videoList.add(aweme)
                    if (videoList.size >= 20) {
                        break
                    }
                }
            }
            // 提前6楼请求数据 todo 把这个6改成配置的
            if (videoList.size <= 6) {
                Log.d(TAG, "need load more data")
                feedContext.feedDataContext().doLoadMore(object : FeedAppendDataHandler {
                    override fun beforeAppendData(
                        originData: FeedItemList,
                        newData: FeedItemList
                    ): Boolean {
                        return false
                    }
                })
            }
        }
        castVM?.updateVideoIdList()?.value = videoList
    }

    override fun onResume() {
        super.onResume()
    }

    fun updateCastingParams(aweme: Aweme) {
        if (castVM?.castingParams()?.value?.aweme?.aid == aweme?.aid) {
            return
        }
        val castParams = CastParams.builder(aweme) {
            // fragment可能有问题
            fragment = WeakReference(feedContext.getFragmentP())
            activity = WeakReference(feedContext.getActivityP() as FragmentActivity?)
            context = WeakReference(feedContext.getContextP())
            castHelper = object : CastParams.DefaultBusinessHelper() {
                override fun getCurrentPositon(): Long {
                    return feedContext.feedPlayerContext().currentPosition(aweme).toLong()
                }

                override fun getSearchViewTargetHeight(): Int {
                    return 0
                }
            }
            isLandscape = false
            sceneId = CastSourceConstant.SCENE_ID_DY_FEED
            enterFrom = eventType
        }
        castVM?.castComponentId()?.value = compenentId
        castVM?.updateCastingParamsFromTV()?.value = castParams
    }

    override fun onPause() {
        super.onPause()
    }

    override fun onStop() {
        super.onStop()
    }

    override fun onDestroy() {
        castVM?.castingVideoIdFromTV()?.removeObserver(requestListObserver)
        super.onDestroy()

    }

    fun tryShowControlView(aweme: Aweme?) {
        aweme ?: return
        val fragment = feedContext.getFragmentP()
        fragment?.let {
            // 只有视频才进行操作
            val castParams = CastParams.builder(aweme) {
                this.fragment = WeakReference(it)
                activity = WeakReference(feedContext.getActivityP() as FragmentActivity?)
                context = WeakReference(feedContext.getContextP())
                sceneId = CastSourceConstant.SCENE_ID_DY_FEED
                castHelper = object : CastParams.DefaultBusinessHelper() {
                    override fun getCurrentPositon(): Long {
                        return feedContext.feedPlayerContext().currentPosition(aweme).toLong()
                    }

                    override fun getSearchViewTargetHeight(): Int {
                        return 0
                    }
                }
                enterFrom = eventType
                isLandscape = false
            }
            castVM?.showingParams()?.value = castParams
            castService.tryShowCastControlView(castParams)
        }
    }

    override fun handleLoadMore(list: List<Aweme>, hasMore: Boolean) {
        val groupIdList = mutableListOf<String>()
        if (castVM?.castComponentId()?.value == compenentId || (castVM?.isShowFlowItem()?.value == true && castVM?.showComponentId()?.value == compenentId)) {
            list?.forEach { aweme ->
                if (castService.isCanCastType(aweme)) {
                    aweme.aid?.let { itemGroupId ->
                        groupIdList.add(itemGroupId)
                    }
                }
            }
            // 提前请求视频是否可以投屏
            castService.fetchCastEnable(groupIdList)
        }

    }

    fun fetchCastEnable() {
        val groupIdList = mutableListOf<String>()
        val awemeList = feedContext.feedDataContext().getItemList()
        awemeList.forEach { itemAweme ->
            if (castService.isCanCastType(itemAweme)) {
                itemAweme.aid?.let { itemGroupId ->
                    groupIdList.add(itemGroupId)
                }
            }
        }
        // 提前请求视频是否可以投屏
        castService.fetchCastEnable(groupIdList)
    }

    override fun handleResumeP(params: FeedLifecycleParams) {
        super.handleResumeP(params)
        Log.d(TAG, "on resume")
        if (compenentId == null) return
        // onResume被调用说明发生了场景变化，一些数据需要重置，主feed的初次打开不知道为啥不走onResume，保险起见数据在onViewCreate的时候也重置一次
        castVM?.showComponentId()?.value = compenentId
        if (castVM?.castComponentId()?.value == compenentId || (castVM?.isShowFlowItem()?.value == true && castVM?.showComponentId()?.value == compenentId)) {
            fetchCastEnable()
        }
        val aweme = feedContext.feedDataContext().getCurrentItem()
        tryShowControlView(aweme)
        if (castVM?.castComponentId()?.value == castVM?.showComponentId()?.value && castVM?.showComponentId()?.value != null) {
            castVM?.castingVideoIdFromTV()?.value = castVM?.castingParams()?.value?.aweme?.video?.playAddr?.originUri
        }
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "on start")
        // onResume被调用说明发生了场景变化，一些数据需要重置，主feed的初次打开不知道为啥不走onResume，保险起见数据在onViewCreate的时候也重置一次
        castVM?.showComponentId()?.value = compenentId
    }
}

@ServiceImpl(service = [IBaseListFragmentComponentFactory::class], singleton = false)
class CastControlComponentFactory : IBaseListFragmentComponentFactory {

    override fun getId(): String {
        return "com.ss.android.ugc.aweme.feed.plato.business.contentconsumption.cast.CastControlComponentFactory"
    }

    override fun isMeetCondition(feedParam: FeedParam): Boolean {
        if (SVCastIconShowExperiment.supportPortrait()
            && SVCastIconShowExperiment.supportScene(feedParam.eventType) && !isTogether(feedParam.scene)
        ) {
            return true
        }
        ICastService.instance.getViewModel()?.showComponentId()?.postValue("")
        return false
    }

    override fun createComponent(context: IFeedContext): IFeedComponent {
        return CastControlComponent(feedContext = context)
    }

    private fun isTogether(scene: String?): Boolean {
        if (scene == null) return false
        if (scene == FeedShareConstants.FEED_SHARE_SCENE_NAME) {
            return true
        }
        return false
    }
}解释下上面代码，并分析代码中问题