package com.bokecc.livemodule.live.multirtc;

import android.content.Context;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.IntDef;
import androidx.annotation.StringRes;

import com.bokecc.dwlivedemo.R;
import com.bokecc.livemodule.live.DWLiveRTCStatusListener;
import com.bokecc.livemodule.utils.CCViewUtils;
import com.bokecc.livemodule.utils.DensityUtil;
import com.bokecc.livemodule.view.BaseRelativeLayout;
import com.bokecc.sdk.mobile.live.common.log.ELog;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Timer;
import java.util.TimerTask;

/**
 * CC 多人连麦控制组件 父级元素必须是RelativeLayout
 *
 */
public class MultiRTCControlLayout extends BaseRelativeLayout implements DWLiveRTCStatusListener {
    private final String TAG = "MultiRTCControlLayout";

    public static final int IDLE = 0;   //绿色按钮、申请视频连麦
    public static final int CALLING = 1; //红色按钮、申请视频/音频连麦中...
    public static final int CALLING_DISABLE = 2; //红色按钮、申请视频/音频连麦中... 置灰状态
    public static final int ANSWERING_VIDEO = 3;  //红色按钮、静音按钮、禁用视频按钮、摄像头切换按钮，视频连麦通话中
    public static final int ANSWERING_AUDIO = 4;  //红色按钮、静音按钮，音频连麦通话中
    public static final int RECEIVE_CALL = 5; //绿色按钮-接受或拒绝-红色按钮，接收到连麦请求
    public static final int CONNECTING = 6; //中间态，连线中，什么按钮都没有
    private LinearLayout mContainer;
    private ImageView mGreenButton;
    private ImageView mRedButton;
    private TextView mTvCallTips;
    private ImageView mMicButton;
    private ImageView mCameraButton;
    private ImageView mCameraSwitchButton;
    private ImageView mArrowRightButton;

    //收缩计时器
    private Timer mFoldTimer;
    private TimerTask mFoldTimerTask;
    //发起通话5秒后才可以挂断计时器
    private Timer mCallDisableTimer;
    private TimerTask mCallDisableTimerTask;

    private OnRTCControlListener mOnRTCControlListener;
    private ImageView mMoreButton;
    private TextView mReceiveTips;
    private View rootView;
    private boolean mLand = false;
    private float topMarginPercent = 0.3f;//在父view的位置


    @IntDef({IDLE, CALLING,CALLING_DISABLE,ANSWERING_VIDEO,ANSWERING_AUDIO,RECEIVE_CALL,CONNECTING})
    @Retention(RetentionPolicy.SOURCE)
    public @interface RTCControlState {}

    @RTCControlState int currentState = IDLE;

    private boolean isExpanded = true;  //当前面板展开状态
    private boolean isAllowVideo = true;  //是否支持视频连麦
    private boolean isMicOn = true;
    private boolean isCameraOn = true;
    private boolean isCameraFront = true;  //是否是前置摄像头  false 后置


    public MultiRTCControlLayout(Context context) {
        super(context);
        refreshState();
    }

    public MultiRTCControlLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        refreshState();
    }

    public MultiRTCControlLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        refreshState();
    }

    public void setCurrentState(@RTCControlState int currentState) {
        this.currentState = currentState;
        //取消5s计时器
        if (currentState!=CALLING_DISABLE){
            releaseCallDisableTimer();
        }
        //联通状态，横屏需要进行判断
        if((currentState==ANSWERING_VIDEO)&&mContext.getResources().getConfiguration().orientation== Configuration.ORIENTATION_LANDSCAPE&&isAllowVideo){
            setLandScape(true);
        }else{
            isExpanded = true;
            refreshState();
        }

    }

    public int getCurrentState() {
        return currentState;
    }

    public void setLandScape(boolean land) {
        ELog.d(TAG, "[demo_bokecc.MultiRTCControlLayout.setLandScape]  [land="+land+"]  ");
        this.mLand = land;
        //lan为true代表横屏接通状态，只显示answering布局，并且调成竖向布局
        if (land){
            if (rootView!=null){
                rootView.setBackgroundResource(R.drawable.shape_bg_rtc_control_land);
                mContainer.setOrientation(LinearLayout.VERTICAL);
                //禁用滑动，居中显示
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) this.getLayoutParams();
                layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
                layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_END);
                layoutParams.setMarginStart(DensityUtil.dp2px(mContext,15f));
                this.setLayoutParams(layoutParams);
                calculateTopMargin();
            }
        }else {
            if (rootView!=null){
                rootView.setBackgroundResource(R.drawable.shape_bg_rtc_control);
                mContainer.setOrientation(LinearLayout.HORIZONTAL);
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) this.getLayoutParams();
                layoutParams.removeRule(RelativeLayout.CENTER_VERTICAL);
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END);
                layoutParams.setMarginStart(DensityUtil.dp2px(mContext,0f));
                this.setLayoutParams(layoutParams);
                calculateTopMargin();
            }

        }
        isExpanded = true;
        refreshState();

    }


    public void setMicOn(final boolean micOn) {
        post(new Runnable() {
            @Override
            public void run() {
                isMicOn = micOn;
                refreshState();
            }
        });
    }

    public void setCameraOn(final boolean cameraOn) {
        post(new Runnable() {
            @Override
            public void run() {
                isCameraOn = cameraOn;
                refreshState();
            }
        });
    }

    @Override
    public void initViews() {
        inflateViews();
    }

    private void inflateViews() {
        rootView = LayoutInflater.from(mContext).inflate(R.layout.live_multi_rtc_control_layout, null);

        //布局
        mContainer = rootView.findViewById(R.id.ll_live_module_multi_rtc_control_container);
        //绿色接听按钮
        mGreenButton = rootView.findViewById(R.id.iv_live_module_multi_rtc_control_green_icon);
        //红色挂断按钮
        mRedButton = rootView.findViewById(R.id.iv_live_module_multi_rtc_control_red_icon);
        //文字描述-正在通话中等
        mTvCallTips = rootView.findViewById(R.id.tv_live_module_multi_rtc_control_call_tips);
        //文字描述-讲师邀请连麦
        mReceiveTips = rootView.findViewById(R.id.tv_live_module_multi_rtc_control_receive_call);


        //麦克风禁用按钮
        mMicButton = rootView.findViewById(R.id.iv_live_module_multi_rtc_control_mic);
        //摄像头禁用按钮
        mCameraButton = rootView.findViewById(R.id.iv_live_module_multi_rtc_control_camera);
        //摄像头切换按钮
        mCameraSwitchButton = rootView.findViewById(R.id.iv_live_module_multi_rtc_control_camera_change);
        //收缩面板按钮
        mArrowRightButton = rootView.findViewById(R.id.iv_live_module_multi_rtc_control_arrow_right);

        //更多按钮
        mMoreButton = rootView.findViewById(R.id.iv_live_module_multi_rtc_control_more);

        CCViewUtils.applyPressedViewScale(mGreenButton,mRedButton,mMicButton,mCameraButton,mCameraSwitchButton,mArrowRightButton);

        addView(rootView,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        post(new Runnable() {
            @Override
            public void run() {
                calculateTopMargin();
            }
        });

    }

    public void calculateTopMargin(){
        RelativeLayout.LayoutParams layoutParams = (LayoutParams) getLayoutParams();
        if(layoutParams!=null&&getParent()!=null){
            float top= ((ViewGroup)getParent()).getMeasuredHeight()*topMarginPercent;
            layoutParams.topMargin = (int) Math.max(0,top);
            this.setLayoutParams(layoutParams);
        }
    }


    private void refreshState(){
        resetView();
        switch (currentState){
            case IDLE:
                if (isExpanded){
                    mContainer.setVisibility(View.VISIBLE);
                    mGreenButton.setVisibility(View.VISIBLE);
                    mTvCallTips.setVisibility(View.VISIBLE);
                    if (isAllowVideo){//视频连麦
                        mTvCallTips.setText(getString(R.string.module_live_rtc_idle_video));
                    }else {//音频连麦
                        mTvCallTips.setText(getString(R.string.module_live_rtc_idle_audio));
                    }
                    this.setClickable(true);//允许拖拽
                    mGreenButton.setClickable(true);
                    CCViewUtils.applySingleDebouncing(mGreenButton, new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mOnRTCControlListener!=null){
                                mOnRTCControlListener.onCallClick();
                            }
                        }
                    });

                }else {
                    mContainer.setVisibility(View.VISIBLE);
                    mGreenButton.setVisibility(View.VISIBLE);
                    this.setClickable(true);
                    this.setOnClickListener(rootClickListener);
                }
                break;
            case CALLING:
                if (isExpanded){
                    mContainer.setVisibility(View.VISIBLE);
                    mRedButton.setVisibility(View.VISIBLE);
                    mTvCallTips.setVisibility(View.VISIBLE);
                    mTvCallTips.setText(getString(R.string.module_live_rtc_apply));
                    this.setClickable(true);
                    mRedButton.setClickable(true);
                    CCViewUtils.applySingleDebouncing(mRedButton, new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mOnRTCControlListener!=null){
                                mOnRTCControlListener.onBreakClick();
                            }
                        }
                    });
                }else {
                    mContainer.setVisibility(View.VISIBLE);
                    mRedButton.setVisibility(View.VISIBLE);
                    this.setClickable(true);
                    this.setOnClickListener(rootClickListener);
                }

                break;
            case CALLING_DISABLE:
                if (isExpanded){
                    mContainer.setVisibility(View.VISIBLE);
                    mRedButton.setVisibility(View.VISIBLE);
                    mTvCallTips.setVisibility(View.VISIBLE);
                    mTvCallTips.setText(getString(R.string.module_live_rtc_apply));
                    this.setClickable(true);
                    mRedButton.setClickable(true);
                    mRedButton.setAlpha(0.5f);
                    CCViewUtils.applySingleDebouncing(mRedButton, new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mOnRTCControlListener!=null){
                                mOnRTCControlListener.onBreakDisableClick();
                            }
                        }
                    });
                }else {
                    mContainer.setVisibility(View.VISIBLE);
                    mRedButton.setVisibility(View.VISIBLE);
                    this.setClickable(true);
                    this.setOnClickListener(rootClickListener);
                }
                break;
            case ANSWERING_VIDEO:
                if (isExpanded){
                    mRedButton.setVisibility(View.VISIBLE);
                    mMicButton.setVisibility(View.VISIBLE);
                    mMicButton.setImageResource(isMicOn?R.drawable.icon_live_multirtc_mic_on:R.drawable.icon_live_multirtc_mic_off);
                    mCameraButton.setVisibility(View.VISIBLE);
                    mCameraButton.setImageResource(isCameraOn?R.drawable.icon_live_multirtc_camera_on:R.drawable.icon_live_multirtc_camera_off);
                    mCameraSwitchButton.setVisibility(View.VISIBLE);
                    if (!mLand){//横屏模式下不展示arrow
                        mArrowRightButton.setVisibility(View.VISIBLE);
                    }else {
                        mArrowRightButton.setVisibility(View.GONE);
                    }
                    mRedButton.setClickable(true);
                    mMicButton.setClickable(true);
                    mCameraButton.setClickable(true);
                    mCameraSwitchButton.setClickable(true);
                    mArrowRightButton.setClickable(true);
                    this.setClickable(true);//允许拖拽

                    CCViewUtils.applySingleDebouncing(mArrowRightButton, new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            fold();
                        }
                    });
                    CCViewUtils.applySingleDebouncing(mRedButton, new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mOnRTCControlListener!=null){
                                mOnRTCControlListener.onBreakClick();
                            }
                        }
                    });
                    CCViewUtils.applySingleDebouncing(mMicButton, new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mOnRTCControlListener!=null){
                                mOnRTCControlListener.onMicStateChange(!isMicOn);
                            }

                        }
                    });

                    CCViewUtils.applySingleDebouncing(mCameraButton, new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mOnRTCControlListener!=null){
                                mOnRTCControlListener.onCameraStateChange(!isCameraOn);
                            }
                        }
                    });


                    CCViewUtils.applySingleDebouncing(mCameraSwitchButton, new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mOnRTCControlListener!=null){
                                mOnRTCControlListener.onCameraSwitch(isCameraOn,!isCameraFront);
                                if (isCameraOn){
                                    isCameraFront = !isCameraFront;
                                }
                            }
                        }
                    });

                }else {
                    mMoreButton.setVisibility(View.VISIBLE);
                    this.setClickable(true);
                    this.setOnClickListener(rootClickListener);
                }

                break;
            case ANSWERING_AUDIO:
                if (isExpanded){
                    mRedButton.setVisibility(View.VISIBLE);
                    mMicButton.setVisibility(View.VISIBLE);
                    mMicButton.setImageResource(isMicOn?R.drawable.icon_live_multirtc_mic_on:R.drawable.icon_live_multirtc_mic_off);
//                    if (!mLand){//横屏模式下不展示arrow
                    mArrowRightButton.setVisibility(View.VISIBLE);
//                    }else {
//                        mArrowRightButton.setVisibility(View.GONE);
//                    }
                    mRedButton.setClickable(true);
                    mMicButton.setClickable(true);
                    mArrowRightButton.setClickable(true);
                    this.setClickable(true);//允许拖拽

                    CCViewUtils.applySingleDebouncing(mArrowRightButton, new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            fold();
                        }
                    });
                    CCViewUtils.applySingleDebouncing(mRedButton, new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mOnRTCControlListener!=null){
                                mOnRTCControlListener.onBreakClick();
                            }
                        }
                    });
                    CCViewUtils.applySingleDebouncing(mMicButton, new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mOnRTCControlListener!=null){
                                mOnRTCControlListener.onMicStateChange(!isMicOn);
                            }

                        }
                    });

                }else {
                    mMoreButton.setVisibility(View.VISIBLE);
                    this.setClickable(true);
                    this.setOnClickListener(rootClickListener);
                }

                break;
            case RECEIVE_CALL:
                isExpanded = true;
                mGreenButton.setVisibility(View.VISIBLE);
                mReceiveTips.setVisibility(View.VISIBLE);
                mReceiveTips.setText(isAllowVideo?"讲师邀请你\n视频连麦":"讲师邀请你\n音频连麦");
                mRedButton.setVisibility(View.VISIBLE);
                mGreenButton.setClickable(true);
                mRedButton.setClickable(true);
                this.setClickable(true);//允许拖拽

                CCViewUtils.applySingleDebouncing(mGreenButton, new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnRTCControlListener!=null){
                            mOnRTCControlListener.onCallClick();
                        }
                    }
                });
                CCViewUtils.applySingleDebouncing(mRedButton, new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnRTCControlListener!=null){
                            mOnRTCControlListener.onBreakClick();
                        }
                    }
                });
                break;
            case CONNECTING:
                isExpanded = true;
                mRedButton.setVisibility(View.VISIBLE);
                mRedButton.setClickable(false);
                mTvCallTips.setVisibility(View.VISIBLE);
                mTvCallTips.setText(getString(R.string.module_live_rtc_lining));
                this.setClickable(true);//允许拖拽
                break;
            default:

                break;
        }

        setDimenByState();

        if (isExpanded){
            createFoldTimer();
        }

    }

    private void resetView() {
        for (int x=0;x<mContainer.getChildCount();x++){
            View v = mContainer.getChildAt(x);
            v.setVisibility(View.GONE);
            //清除所有点击事件
            v.setOnClickListener(null);
            v.setClickable(false);
        }
        mMoreButton.setVisibility(View.GONE);
        mRedButton.setAlpha(1f);
        this.setOnClickListener(null);
        this.setClickable(false);
    }

    public void expand(){
        post(new Runnable() {
            @Override
            public void run() {
                if (!isExpanded){
                    isExpanded = true;
                    refreshState();
                    startOpenAnim();
                }
            }
        });

    }

    //收起动画
    private void startCloseAnim() {
    }
    //展开动画
    private void startOpenAnim() {
    }

    private void setDimenByState(){
        if (!mLand){
            //容器上下padding是5，左右是0
            rootView.setPadding(0,DensityUtil.dp2px(mContext,5),0,DensityUtil.dp2px(mContext,5));
            //按钮间距7.5，
            setMargin(mGreenButton,DensityUtil.dp2px(mContext,7.5f),0,0,0);
            setMargin(mRedButton,DensityUtil.dp2px(mContext,7.5f),0,0,0);
            setMargin(mMicButton,DensityUtil.dp2px(mContext,7.5f),0,0,0);
            setMargin(mCameraButton,DensityUtil.dp2px(mContext,7.5f),0,0,0);
            setMargin(mCameraSwitchButton,DensityUtil.dp2px(mContext,7.5f),0,0,0);
            setMargin(mArrowRightButton,DensityUtil.dp2px(mContext,7.5f),0,0,0);
            //文字间距5  后间距15
            setMargin(mTvCallTips,DensityUtil.dp2px(mContext,5f),0,DensityUtil.dp2px(mContext,15f),0);
            setMargin(mReceiveTips,DensityUtil.dp2px(mContext,5f),0,DensityUtil.dp2px(mContext,15f),0);
            setMargin(mMoreButton,DensityUtil.dp2px(mContext,7.5f),0,DensityUtil.dp2px(mContext,15f),0);

        }else {
            //容器左右padding是5，左右是0
            rootView.setPadding(DensityUtil.dp2px(mContext,10),DensityUtil.dp2px(mContext,10),DensityUtil.dp2px(mContext,10),DensityUtil.dp2px(mContext,10));
            //按钮间距7.5，
            setMargin(mGreenButton,0,0,0,0);
            setMargin(mRedButton,0,0,0,0);
            setMargin(mMicButton,0,DensityUtil.dp2px(mContext,12.5f),0,0);
            setMargin(mCameraButton,0,DensityUtil.dp2px(mContext,12.5f),0,0);
            setMargin(mCameraSwitchButton,0,DensityUtil.dp2px(mContext,12.5f),0,0);
            setMargin(mArrowRightButton,DensityUtil.dp2px(mContext,7.5f),0,0,0);
            //文字间距5  后间距15
            setMargin(mTvCallTips,DensityUtil.dp2px(mContext,5f),0,DensityUtil.dp2px(mContext,15f),0);
            setMargin(mReceiveTips,DensityUtil.dp2px(mContext,5f),0,DensityUtil.dp2px(mContext,15f),0);
            setMargin(mMoreButton,DensityUtil.dp2px(mContext,7.5f),0,DensityUtil.dp2px(mContext,15f),0);
        }
    }

    private void setMargin(View view,int left,int top,int right,int bottom){
        if (view.getLayoutParams()!=null){
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            if (layoutParams instanceof RelativeLayout.LayoutParams){
                ((RelativeLayout.LayoutParams)layoutParams).setMargins(left,top,right,bottom);
                view.setLayoutParams(layoutParams);
            }else if (layoutParams instanceof LinearLayout.LayoutParams){
                ((LinearLayout.LayoutParams)layoutParams).setMargins(left,top,right,bottom);
                view.setLayoutParams(layoutParams);
            }
        }
    }


    public void fold(){
        post(new Runnable() {
            @Override
            public void run() {
                if (isExpanded&&!mLand){
                    isExpanded = false;
                    refreshState();
                    startCloseAnim();
                }
            }
        });

    }

    private String getString(@StringRes int id){
        if (mContext!=null){
            return mContext.getString(id);
        }
        return "";
    }


    private OnClickListener rootClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            expand();
        }
    };


    @Override
    protected void onDetachedFromWindow() {
        ELog.d(TAG, "[demo_bokecc.MultiRTCControlLayout.onDetachedFromWindow]");
        releaseFoldTimer();
        releaseCallDisableTimer();
        super.onDetachedFromWindow();
    }

    private void createFoldTimer(){
        if (mFoldTimer !=null){
            releaseFoldTimer();
        }
        mFoldTimer = new Timer();
        mFoldTimerTask = new TimerTask() {
            @Override
            public void run() {
                fold();
            }
        };
        //10s之后，收缩view
        mFoldTimer.schedule(mFoldTimerTask,10000L);
    }

    /**
     * destory上次使用的 Timer
     */
    private void releaseFoldTimer() {
        try{
            if (mFoldTimer != null) {
                mFoldTimer.cancel();
                mFoldTimer = null;
            }
            if (mFoldTimerTask != null) {
                mFoldTimerTask.cancel();
                mFoldTimerTask = null;
            }
//            ELog.d(TAG, "[demo_bokecc.MultiRTCControlLayout.releaseFoldTimer] release success");
        }catch (Exception e){
            ELog.e(TAG,"timer cancel failed");
        }
    }

    private float downY = 0f;
    private boolean isDrag = false;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        //拖拽操作下拦截事件
        return isDrag || super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float y = event.getY();
        //纵向移动超过5个像素，视为拖拽
        if (event.getAction()==MotionEvent.ACTION_DOWN){
            isDrag = false;
            downY = y;
        }else if (event.getAction()==MotionEvent.ACTION_MOVE){
            if (Math.abs(y-downY)>5){
                isDrag = true;
                handleDragEvent(event);
            }
        }else if (event.getAction()==MotionEvent.ACTION_UP){
            downY = 0f;
            //重置10s定时器
            if (isExpanded){//展开状态下10s后折叠
                createFoldTimer();
            }
        }


        return isDrag || super.onTouchEvent(event);
    }

    private void handleDragEvent(MotionEvent event) {
        if (mLand) {
            return;
        }
        float y = event.getY();
        float dy = y-downY;
        try{
            RelativeLayout.LayoutParams layoutParams = (LayoutParams) getLayoutParams();
            layoutParams.topMargin = Math.min(Math.max(0,(int) (layoutParams.topMargin+dy)),((ViewGroup)getParent()).getMeasuredHeight()-DensityUtil.dp2px(mContext,100));
            this.setLayoutParams(layoutParams);
        }catch (Exception e){
            ELog.e(TAG,"this layoutParams is not RelativeLayout.LayoutParams");
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    public void setOnRTCControlListener(OnRTCControlListener mOnRTCControlListener) {
        this.mOnRTCControlListener = mOnRTCControlListener;
    }

    public interface OnRTCControlListener{
        /**
         * 发起连麦按钮点击
         */
        void onCallClick();

        /**
         * 挂断按钮点击
         */
        void onBreakClick();

        void onBreakDisableClick();

        /**
         * 麦克风按钮点击
         * @param on 返回当前按钮状态，需要调用者监听到回调之后，操作禁用麦克风并设置回view，true 开启麦克风  off 关闭麦克风
         */
        void onMicStateChange(boolean on);

        /**
         * 摄像头按钮点击
         * @param on  true 摄像头启用  false  摄像头禁用
         */
        void onCameraStateChange(boolean on);

        /**
         * 前后摄像头切换
         */
        void onCameraSwitch(boolean isCameraOn, boolean isCameraFront);
    }




    // region -------------------- 状态改变回调 -------------------------

    @Override
    public void onEnterRTC(final boolean isVideoRtc) {
        post(new Runnable() {
            @Override
            public void run() {
                setCurrentState(isVideoRtc?ANSWERING_VIDEO:ANSWERING_AUDIO);
            }
        });
    }

    @Override
    public void onExitRTC() {
        post(new Runnable() {
            @Override
            public void run() {
                isMicOn = true;
                isCameraOn = true;
                setCurrentState(IDLE);
            }
        });
    }

    @Override
    public void onCloseSpeak() {
        post(new Runnable() {
            @Override
            public void run() {
                setCurrentState(IDLE);
            }
        });
    }

    @Override
    public void onCalling() {
        post(new Runnable() {
            @Override
            public void run() {
                setCurrentState(CALLING_DISABLE);
                //启动计时器，5秒后置为可用
                startCallDisableTimer();
            }
        });
    }

    private void startCallDisableTimer() {
        if (mCallDisableTimer !=null){
            releaseCallDisableTimer();
        }
        mCallDisableTimer = new Timer();
        mCallDisableTimerTask = new TimerTask() {
            @Override
            public void run() {
                post(new Runnable() {
                    @Override
                    public void run() {
                        setCurrentState(CALLING);
                    }
                });
            }
        };
        //10s之后，收缩view
        mCallDisableTimer.schedule(mCallDisableTimerTask,5000L);
    }

    private void releaseCallDisableTimer() {
        try{
            if (mCallDisableTimer != null) {
                mCallDisableTimer.cancel();
                mCallDisableTimer = null;
            }
            if (mCallDisableTimerTask != null) {
                mCallDisableTimerTask.cancel();
                mCallDisableTimerTask = null;
            }
//            ELog.d(TAG, "[demo_bokecc.MultiRTCControlLayout.releaseCallDisableTimer]  release success");
        }catch (Exception e){
            ELog.e(TAG,"timer cancel failed");
        }
    }


    @Override
    public void onReceiveCall() {
        post(new Runnable() {
            @Override
            public void run() {
                setCurrentState(RECEIVE_CALL);
            }
        });
    }

    @Override
    public void onReceiveCancelCall() {
        post(new Runnable() {
            @Override
            public void run() {
                setCurrentState(IDLE);
            }
        });
    }

    @Override
    public void onRTCAvailableTypeChange(boolean isAllowRtc,boolean isVideoRTC) {
        isAllowVideo = isVideoRTC;
    }
}
