package com.ez.ertc.meeting.ui;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.ez.ertc.meeting.BuildConfig;
import com.ez.ertc.meeting.R;
import com.ez.ertc.meeting.R2;
import com.ez.ertc.meeting.base.YsBaseActivity;
import com.ez.ertc.meeting.constant.MtNavigator;
import com.ez.ertc.meeting.constant.MtSets;
import com.ez.ertc.meeting.event.MtAccountNotifyEvent;
import com.ez.ertc.meeting.event.MtAudioModeEvent;
import com.ez.ertc.meeting.event.MtSwitchAudioDisplayEvent;
import com.ez.ertc.meeting.event.MtSwitchMemberToastEvent;
import com.ez.ertc.meeting.event.MtSwitchStimulationEvent;
import com.ez.ertc.meeting.event.MtSwitchWaterMarkEvent;
import com.ez.ertc.meeting.event.MtToTestEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author xiezuoyuan
 * @time 2020/5/8 20:22
 */
public class MeetingSetsActivity extends YsBaseActivity {
    @BindView(R2.id.swVoice)
    Switch swVoice;
    @BindView(R2.id.swToast)
    Switch swToast;
    @BindView(R2.id.swWaterMark)
    Switch swWaterMark;
    @BindView(R2.id.swSaveAVData)
    Switch swSaveAVData;
    @BindView(R2.id.swAudioMode)
    Switch swAudioMode;
    @BindView(R2.id.swAudioDisplay)
    Switch swAudioDisplay;
    @BindView(R2.id.llMtSets)
    LinearLayout llMtSets;
    @BindView(R2.id.tvSdkVersion)
    TextView tvSdkVersion;
    @BindView(R2.id.tvAutoMute)
    TextView tvAutoMute;
    float deltaX,deltaY;
    private boolean isWaterMarkOn;

    public static void start(Activity activity,boolean isWaterMarkOn,boolean isHost,String roomId, boolean isAudioMode){
        Intent intent = new Intent(activity, MeetingSetsActivity.class);
        intent.putExtra(MtNavigator.Extras.EXTRA_WATERMARK_ON,isWaterMarkOn);
        intent.putExtra(MtNavigator.Extras.EXTRA_ROOM_IS_MANAGER,isHost);
        intent.putExtra(MtNavigator.Extras.EXTRA_ROOM_ID,roomId);
        intent.putExtra(MtNavigator.Extras.EXTRA_AUDIO_MODE,isAudioMode);
        activity.startActivity(intent);
    }

    @Override
    protected boolean registerEventBus() {
        return true;
    }

    @Override
    protected int provideLayoutId() {
        return R.layout.ys_mt_activity_meeting_sets;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initData() {
        boolean isHost = getIntent().getBooleanExtra(MtNavigator.Extras.EXTRA_ROOM_IS_MANAGER,false);
        if (isHost){
            llMtSets.setVisibility(View.VISIBLE);
            isWaterMarkOn = getIntent().getBooleanExtra(MtNavigator.Extras.EXTRA_WATERMARK_ON,true);
            swWaterMark.setChecked(isWaterMarkOn);
        } else {
            llMtSets.setVisibility(View.GONE);
        }
        if (!getPackageName().equals("com.ez.ertc")){
            tvSdkVersion.setText(BuildConfig.SDK_VERSION);
            tvSdkVersion.setVisibility(View.VISIBLE);
        } else {
            tvSdkVersion.setVisibility(View.INVISIBLE);
        }
        swAudioMode.setChecked(getIntent().getBooleanExtra(MtNavigator.Extras.EXTRA_AUDIO_MODE,false));
        //屏蔽滑动导致不触发onClick的问题
        View.OnTouchListener onTouchListener = new View.OnTouchListener() {
            @SuppressLint("NewApi")
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        deltaX = event.getX();
                        deltaY = event.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        float x = event.getX();
                        float y = event.getY();
                        if (Math.abs(x - deltaX) < 50 && Math.abs(y - deltaY) < 50){
                            v.performClick();
                        }
                        break;
                }
                return true;
            }
        };
        swWaterMark.setOnTouchListener(onTouchListener);
        swToast.setOnTouchListener(onTouchListener);
        swVoice.setOnTouchListener(onTouchListener);
        swAudioMode.setOnTouchListener(onTouchListener);
        swAudioDisplay.setOnTouchListener(onTouchListener);
    }

    @OnClick({R2.id.swWaterMark,R2.id.swToast,R2.id.swVoice
    ,R2.id.swAudioMode,R2.id.swAudioDisplay,R2.id.llAutoMute,R2.id.llNetwork})
    public void onClick(View view){
        int id = view.getId();
        if (id == R.id.swWaterMark){
            isWaterMarkOn = swWaterMark.isChecked();
            EventBus.getDefault().post(new MtSwitchWaterMarkEvent(swWaterMark.isChecked()));
        } else if (id == R.id.swSaveAVData){
            MtSets.setSaveAVData(swSaveAVData.isChecked());
        } else if (id == R.id.swToast){
            MtSets.setShowMemberToast(swToast.isChecked());
            EventBus.getDefault().post(new MtSwitchMemberToastEvent(swToast.isChecked()));
        } else if (id == R.id.swVoice){
            MtSets.setVoiceStimulation(swVoice.isChecked());
            EventBus.getDefault().post(new MtSwitchStimulationEvent(swVoice.isChecked()));
        } else if (id == R.id.swAudioMode){
            EventBus.getDefault().post(new MtAudioModeEvent(swAudioMode.isChecked()));
        } else if (id == R.id.llAutoMute){
            showToast("待实现");
        } else if (id == R.id.llNetwork){
            //2021.11.20
            //关于APP和PC端网络检测的最终方案，先统一保留在会议外的入口，隐藏会议中的入口
            //，以从界面入口上先规避掉底层接口的隐性影响。
            EventBus.getDefault().post(new MtToTestEvent());
        } else if (id == R.id.swAudioDisplay){
            MtSets.setAudioDisplay(swAudioDisplay.isChecked());
            EventBus.getDefault().post(new MtSwitchAudioDisplayEvent(swAudioDisplay.isChecked()));
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MtAccountNotifyEvent event) {
        swWaterMark.setChecked(isWaterMarkOn);
        llMtSets.setVisibility(View.VISIBLE);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MtSwitchWaterMarkEvent event) {
        isWaterMarkOn = event.enable;
        swWaterMark.setChecked(event.enable);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MtAudioModeEvent event) {
        swAudioMode.setChecked(event.enable);
    }

    @Override
    protected void onResume() {
        super.onResume();
        swVoice.setChecked(MtSets.isVoiceStimulation());
        swToast.setChecked(MtSets.isShowMemberToast());
        swAudioDisplay.setChecked(MtSets.isAudioDisplay());
    }

}
