package com.baijiayun.live.ui

import android.view.View
import android.widget.FrameLayout
import androidx.lifecycle.Observer
import com.baijiayun.bjyutils.kt.removeViewFromParent
import com.baijiayun.live.ui.pptpanel.PPTFragment
import com.baijiayun.live.ui.speakpanel.SingleSpeakFragment
import com.baijiayun.live.ui.topmenu.TopMenuFragment
import com.baijiayun.liveuibase.chat.ChatPadFragment
import com.baijiayun.liveuibase.panel.BaseMainVideoFragment
import kotlinx.android.synthetic.main.activity_live_room_pad_single.activity_live_room_pad_room_chat_container
import kotlinx.android.synthetic.main.activity_live_room_pad_single.activity_live_room_pad_room_interaction_container
import kotlinx.android.synthetic.main.layout_chat_single.tv_chat_name

/**
 * 三分屏1v1模板
 * class_type=6
 */

class LiveRoomSingleActivity : LiveRoomTripleActivity() {

    override fun getContentResId() = R.layout.activity_live_room_pad_single
    override fun initView() {
        pptContainer.id = View.generateViewId()
        removeViewFromParent(pptContainer)
        toolboxLayer = findViewById(R.id.activity_live_toolbox)
        topMenuLayout = findViewById(R.id.activity_live_room_pad_room_top_container)
        pptContainerViewGroup = findViewById(R.id.activity_live_room_pad_room_ppt_container)
        pptContainerViewGroup?.addView(pptContainer, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
        responderButtonContainer = toolboxLayer
        supportFragmentManager.beginTransaction()
                .replace(R.id.activity_live_room_pad_room_top_container, TopMenuFragment.newInstance())
                .replace(R.id.activity_live_room_pad_room_main_video_container, BaseMainVideoFragment.newInstance())
                .replace(R.id.activity_live_room_pad_room_interaction_container, ChatPadFragment.newInstance())
                .replace(pptContainer.id, PPTFragment.newInstance())
                .replace(R.id.activity_live_room_pad_room_speaker_video_container, SingleSpeakFragment.newInstance())
                .commitNowAllowingStateLoss()
        routerViewModel.isShowEyeCare.value = findViewById<View>(R.id.activity_class_eye_care_layer).visibility == View.VISIBLE
    }

    override fun afterNavigateToMain() {
        super.afterNavigateToMain()
        activity_live_room_pad_room_chat_container?.setOnClickListener {
            //打开聊天界面(仅手机)
            routerViewModel.action2Chat.value = true
            routerViewModel.redPointNumber.value = 0
        }
        //1v1 课程视频比例9:16
        val videoParams = findViewById<View>(R.id.activity_live_room_pad_room_main_video_container).layoutParams
        val speakParams = findViewById<View>(R.id.activity_live_room_pad_room_speaker_video_container).layoutParams
        videoParams.height = resources.getDimensionPixelSize(R.dimen.main_video_single_width) * 9 / 16
        speakParams.height = videoParams.height
    }

    override fun afterObserveSuccess() {
        super.afterObserveSuccess()
        routerViewModel.redPointNumber.observe(this, Observer {
            it?.let {
                var chatName = getString(R.string.chat)
                if (it > 0) {
                    chatName += "(${it})"
                }
                tv_chat_name?.text = chatName
            }
        })
        routerViewModel.action2Chat.observe(this@LiveRoomSingleActivity, Observer {
            it?.let {
                if (it) {
                    activity_live_room_pad_room_interaction_container.visibility = View.VISIBLE
                } else {
                    activity_live_room_pad_room_interaction_container.visibility = View.GONE
                }
            }
        })
        routerViewModel.isShowEyeCare.observe(this@LiveRoomSingleActivity, Observer {
            it?.let {
                findViewById<View>(R.id.activity_class_eye_care_layer).visibility = if (it) View.VISIBLE else View.GONE
            }
        })
    }
}


