package com.ez.ertc.meeting.ui

import android.app.Activity
import android.content.Intent
import com.ez.basertc.ConstantKeys
import com.ez.basertc.RTCConstant.ERTCVideoEncParam
import com.ez.basertc.RTCConstant.ERTCVideoResolution
import com.ez.ertc.meeting.R
import com.ez.ertc.meeting.base.YsBaseActivity
import com.ez.ertc.meeting.constant.MtSets
import com.ez.ertc.meeting.widget.VideoParamDialog
import kotlinx.android.synthetic.main.activity_debug_test.*

class DebugParamTestActivity : YsBaseActivity() {
    var videoParamDialogSmall: VideoParamDialog? = null
    var videoParamDialogBig: VideoParamDialog? = null
    var videoParamDialogShare: VideoParamDialog? = null

    companion object {
        fun start(activity: Activity) {
            val intent = Intent(activity, DebugParamTestActivity::class.java)
            activity.startActivity(intent)
        }
    }

    override fun provideLayoutId(): Int  = R.layout.activity_debug_test

    override fun initView() {
        videoParamDialogBig = VideoParamDialog(this)
        videoParamDialogBig?.itemClickListener = object :
            VideoParamDialog.ParamRecyclerViewAdapter.OnItemClickListener {
            override fun selectParam(param: ERTCVideoResolution) {
                ConstantKeys.resolutionBig = param
                MtSets.saveResolution(param.width, param.height)
                etResolutionBig.text = param.width.toString() + "x" + param.height
                etBitrateBig.text = (param.recommendBitrate/1024).toString()
                videoParamDialogBig?.dismiss()
            }
        }

        videoParamDialogSmall = VideoParamDialog(this)
        videoParamDialogSmall?.itemClickListener = object :
            VideoParamDialog.ParamRecyclerViewAdapter.OnItemClickListener {
            override fun selectParam(param: ERTCVideoResolution) {
                val smallParam = ERTCVideoEncParam()
                smallParam.videoResolution = param
                smallParam.videoBitrate = param.recommendBitrate
                smallParam.videoFps = ConstantKeys.videoFpsSmall
                ConstantKeys.resolutionSmall = smallParam
                etResolutionSmall.text = param.width.toString() + "x" + param.height
                etBitrateSmall.text = (param.recommendBitrate/1024).toString()
                videoParamDialogSmall?.dismiss()
            }
        }

        videoParamDialogShare = VideoParamDialog(this)
        videoParamDialogShare?.itemClickListener = object :
            VideoParamDialog.ParamRecyclerViewAdapter.OnItemClickListener {
            override fun selectParam(param: ERTCVideoResolution) {
                ConstantKeys.resolutionShare = param
                etResolutionShare.text = param.width.toString() + "x" + param.height
                etBitrateShare.text = (param.recommendBitrate/1024).toString()
                videoParamDialogShare?.dismiss()
            }
        }

        etResolutionBig.setOnClickListener {
            videoParamDialogBig?.show()
        }

        etResolutionSmall.setOnClickListener {
            videoParamDialogSmall?.show()
        }

        etResolutionShare.setOnClickListener {
            videoParamDialogShare?.show()
        }
    }

    override fun initData() {
        etFpsBig.setText(ConstantKeys.videoFpsBig.toString())
        etResolutionBig.text = ConstantKeys.resolutionBig.width.toString() + "x" + ConstantKeys.resolutionBig.height
        etBitrateBig.text = (ConstantKeys.resolutionBig.recommendBitrate/1024).toString()
        etFpsSmall.setText(ConstantKeys.videoFpsSmall.toString())
        etResolutionSmall.text = ConstantKeys.resolutionSmall.videoResolution.width.toString() + "x" +ConstantKeys.resolutionSmall.videoResolution.height
        etBitrateSmall.text = (ConstantKeys.resolutionSmall.videoResolution.recommendBitrate/1024).toString()
        etFpsShare.setText(ConstantKeys.videoFpsShare.toString())
        etResolutionShare.text = ConstantKeys.resolutionShare.width.toString() + "x" + ConstantKeys.resolutionShare.height
        etBitrateShare.text = (ConstantKeys.resolutionShare.recommendBitrate/1024).toString()
    }

    override fun onDestroy() {
        ConstantKeys.videoFpsBig = etFpsBig.text.toString().toInt()
        ConstantKeys.videoFpsSmall = etFpsSmall.text.toString().toInt()
        ConstantKeys.videoFpsShare = etFpsShare.text.toString().toInt()
        var resolutionSmall = ConstantKeys.resolutionSmall
        resolutionSmall.videoFps = ConstantKeys.videoFpsSmall
        ConstantKeys.resolutionSmall = resolutionSmall
        super.onDestroy()
    }
}