package com.videogo.ui.others;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.videogo.global.GlobalKit;
import com.videogo.openapi.EZConstants;
import com.videogo.widget.TitleBar;
import com.videogo.widget.bottommenu.BottomMenuFragment;

import java.util.Arrays;
import java.util.List;

import ezviz.ezopensdk.R;
import ezviz.ezopensdkcommon.common.RootActivity;

public class EZStreamConfigSettingActivity extends RootActivity implements View.OnClickListener,
        CompoundButton.OnCheckedChangeListener {

    protected static final String TAG = EZStreamConfigSettingActivity.class.getSimpleName();

    // 播放器P2P能力禁用开关
    private Switch playerDisableP2PSwitch;

    // 性能/画质优先/默认
    private LinearLayout videoQualityLL;
    // 当前设置的视频质量
    private TextView videoQualityTv;

    // 输出解析模式
    private LinearLayout parseOutputModeLL;
    // 当前设置的输出解析模式
    private TextView parseOutputModeTv;

    // 纯音频播放开关
    private Switch pureAudioSwitch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ezstream_config_setting);

        // 播放器P2P能力禁用
        playerDisableP2PSwitch = findViewById(R.id.switch_player_disable_p2p);
        playerDisableP2PSwitch.setChecked(GlobalKit.getInstance().isPlayerP2PDisable);
        playerDisableP2PSwitch.setOnCheckedChangeListener(this);

        // 视频质量
        videoQualityLL = findViewById(R.id.ll_video_quality);
        videoQualityTv = findViewById(R.id.tv_video_quality);
        videoQualityLL.setOnClickListener(this);

        // 输出解析模式
        parseOutputModeLL = findViewById(R.id.ll_parse_output_mode);
        parseOutputModeTv = findViewById(R.id.tv_parse_output_mode);
        parseOutputModeLL.setOnClickListener(this);

        // 纯音频播放
        pureAudioSwitch = findViewById(R.id.switch_pure_audio);
        pureAudioSwitch.setChecked(GlobalKit.getInstance().enablePureAudio);
        pureAudioSwitch.setOnCheckedChangeListener(this);

        initTitleBar();
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ll_video_quality) {
            openVideoQualityPopupWindow();
        } else if (id == R.id.ll_parse_output_mode) {
            openParseOutputModePopupWindow();
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        int id = buttonView.getId();
        if (id == R.id.switch_pure_audio) {
            GlobalKit.getInstance().enablePureAudio = isChecked;
        } else if (id == R.id.switch_player_disable_p2p) {
            GlobalKit.getInstance().isPlayerP2PDisable = isChecked;
        }
    }

    /**
     * 打开视频质量设置弹出框
     */
    private void openVideoQualityPopupWindow() {
        List<String> videoQualityModes = Arrays.asList("性能优先", "画质优先", "默认");
        new BottomMenuFragment(this).setHeadTitle("请选择画面质量模式").addMenuItems(videoQualityModes).setOnItemClickListener(position -> {
            if (position == 0) {
                GlobalKit.getInstance().videoQuality = EZConstants.EZVideoQuality.VIDEO_PERFORMANCE_PRIORITY;
            } else if (position == 1) {
                GlobalKit.getInstance().videoQuality = EZConstants.EZVideoQuality.VIDEO_QUALITY_PRIORITY;
            } else {
                GlobalKit.getInstance().videoQuality = EZConstants.EZVideoQuality.VIDEO_PRIORITY_NONE;
            }
            videoQualityTv.setText(videoQualityModes.get(position));
        }).show();
    }

    private void openParseOutputModePopupWindow() {
        List<String> parseOutputModes = Arrays.asList("默认，设置封装层断帧", "设置编码层断帧", "设置I帧前输出", "同时设置编码层断帧和I帧前输出");
        new BottomMenuFragment(this).setHeadTitle("请选择解析输出模式").addMenuItems(parseOutputModes).setOnItemClickListener(position -> {

        }).show();
    }

    /***************** UI *******************/

    private void initTitleBar() {
        TitleBar mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.addBackButton(view -> finish());
        mTitleBar.setTitle("取流特殊配置");
    }

}