package com.baijiayun.live.ui.preview

import android.Manifest
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.baijiahulian.common.permission.AppPermissions
import com.baijiayun.bjyutils.reactivex.RxUtils
import com.baijiayun.live.ui.R
import com.baijiayun.live.ui.databinding.BjyFragmentVideoPreviewBinding
import com.baijiayun.livecore.wrapper.LPRecorder
import com.baijiayun.liveuibase.base.BasePadFragment
import com.baijiayun.liveuibase.utils.PermissionTipBuilderFactory
import com.baijiayun.liveuibase.utils.disposeUntilDestroy
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.android.synthetic.main.bjy_layout_ppt_menu.view.*
import java.util.concurrent.TimeUnit

/**
 * @author        pengwenjian
 * @date          2023/2/23
 * @description：开播前本地预览
 */
class VideoPreviewFragment : BasePadFragment() {
    private var _binding: BjyFragmentVideoPreviewBinding? = null
    private val binding get() = _binding!!
    private val settingBeautyDialogFragment by lazy {
        SettingBeautyDialogFragment.newInstance()
    }
    private val settingCameraDialogFragment by lazy {
        SettingCameraDialogFragment.newInstance()
    }


    override fun getLayoutId() = R.layout.bjy_fragment_video_preview

    @SuppressLint("ClickableViewAccessibility")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        _binding = BjyFragmentVideoPreviewBinding.bind(view)
        view.setOnTouchListener { _, _ -> true }

        RxUtils.clicks(binding.startClassBtn)
            .throttleFirst(1, TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .disposeUntilDestroy(this@VideoPreviewFragment)
            .subscribe {
                routerViewModel.liveRoom.requestClassStart()
            }
        RxUtils.clicks(binding.beautyTv)
            .throttleFirst(1, TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .disposeUntilDestroy(this@VideoPreviewFragment)
            .subscribe {
                showDialogFragment(settingBeautyDialogFragment)
            }
        RxUtils.clicks(binding.cameraSettingTv).throttleFirst(1, TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .disposeUntilDestroy(this@VideoPreviewFragment)
            .subscribe {
                showDialogFragment(settingCameraDialogFragment)
            }
        routerViewModel.liveRoom.speakQueueVM.observableOfActiveUsers
            .disposeUntilDestroy(this@VideoPreviewFragment)
            .subscribe {
                startLocalPreView()
            }
        routerViewModel.shouldAttachVideo.observe(this@VideoPreviewFragment) {
            binding.cameraView.visibility = if (it) View.VISIBLE else View.GONE
        }
        binding.beautyTv.visibility = View.VISIBLE
    }

    private fun startLocalPreView() {
        AppPermissions.newPermissions(activity)
            .request(
                PermissionTipBuilderFactory.createPermissionTipsView(
                    context,
                    Manifest.permission.CAMERA
                ),
                Manifest.permission.CAMERA
            )
            .disposeUntilDestroy(this@VideoPreviewFragment)
            .subscribe {
                if (it) {
                    routerViewModel.shouldAttachVideo.value = true
                    routerViewModel.liveRoom.recorder.startLocalPreview(binding.cameraView)
                } else {
                    Toast.makeText(context, context?.resources?.getString(R.string.live_no_camera_mic_permission), Toast.LENGTH_LONG).show()
                }
            }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    companion object {
        fun newInstance(): VideoPreviewFragment {
            val args = Bundle()
            val fragment = VideoPreviewFragment()
            fragment.arguments = args
            return fragment
        }
    }
}