package com.hh.arome.ui.setting;

import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.common.lib_base.utils.ui.ToastUtils;
import com.common.lib_base.utils.ui.UIUtils;
import com.hh.arome.R;
import com.hh.arome.bean.DeviceConfigResultBean;
import com.hh.arome.bean.SimpleResultBean;
import com.hh.arome.common.ConfigData;
import com.hh.arome.mvp.contract.DeviceConfigUpdateContract;
import com.hh.arome.mvp.presenter.DeviceConfigUpdatePresenter;
import com.hh.arome.ui.base.BaseHHActivity;
import com.hh.arome.utlis.log.AppLogUtils;
import com.hh.arome.utlis.log.LogType;

import static com.hh.arome.common.Constants.LogcatValue.LINE;
import static com.hh.arome.common.Constants.LogcatValue.NODE;
import static com.hh.arome.common.Constants.LogcatValue.WARP;

public class ZoloActivity extends BaseHHActivity implements View.OnClickListener, DeviceConfigUpdateContract.View {
    private ImageButton iv_title_left;
    private Toolbar tb_center;
    private TextView tv_camera_display_angle_value;
    private SeekBar seekbar_camera_display_angle;
    private ImageView iv_preview_image_isMirror;
    private RelativeLayout item_preview_image_isMirror;
    private ImageView iv_algorithm_isMirror;
    private RelativeLayout item_algorithm_isMirror;
    private TextView tv_confirm;

    DeviceConfigUpdatePresenter mDeviceConfigUpdatePresenter;
    private TextView tv_ir_algorithm_angle_value;
    private SeekBar seekbar_ir_algorithm_angle;
    private TextView tv_algorithm_image_angle_value;
    private SeekBar seekbar_algorithm_image_angle;

    @Override
    public int setLayout() {
        return R.layout.activity_zolo;
    }

    TextView tv_title_center;

    @Override
    protected void initView() {
        super.initView();

        tv_title_center = findViewById(R.id.tv_title_center);

        iv_title_left = findViewById(R.id.iv_title_left);
        tb_center = findViewById(R.id.tb_center);
        tv_camera_display_angle_value = findViewById(R.id.tv_camera_display_angle_value);
        seekbar_camera_display_angle = findViewById(R.id.seekbar_camera_display_angle);
        iv_preview_image_isMirror = findViewById(R.id.iv_preview_image_isMirror);
        item_preview_image_isMirror = findViewById(R.id.item_preview_image_isMirror);
        iv_algorithm_isMirror = findViewById(R.id.iv_algorithm_isMirror);
        item_algorithm_isMirror = findViewById(R.id.item_algorithm_isMirror);
        tv_confirm = findViewById(R.id.tv_confirm);
        iv_title_left.setOnClickListener(this);
        item_preview_image_isMirror.setOnClickListener(this);
        item_algorithm_isMirror.setOnClickListener(this);
        tv_confirm.setOnClickListener(this);
        tv_ir_algorithm_angle_value = findViewById(R.id.tv_ir_algorithm_angle_value);
        seekbar_ir_algorithm_angle = findViewById(R.id.seekbar_ir_algorithm_angle);
        tv_algorithm_image_angle_value = findViewById(R.id.tv_algorithm_image_angle_value);
        seekbar_algorithm_image_angle = findViewById(R.id.seekbar_algorithm_image_angle);
    }

    DeviceConfigResultBean deviceConfigBean;

    private int displayAngle = 90;//预览角度
    private int algorithmImageAngle = 270;//送算法的图像角度
    private int irAlgorithmAngle = 270;//用于ir摄像头算法角度
    private boolean isDisplayMirror = true;//预览图像是否需要镜像
    private boolean isAlgorithmMirror = false;//送算法图像是否镜像

    @Override
    protected void initData() {

        tv_title_center.setText("zoloConfig摄像头配置");

        mDeviceConfigUpdatePresenter = new DeviceConfigUpdatePresenter(this);

        deviceConfigBean = ConfigData.getDeviceConfigResultBean();

        if (deviceConfigBean == null) {
            AppLogUtils.e("deviceConfigBean == null");
            deviceConfigBean = new DeviceConfigResultBean();
        }

        // 预览角度
        displayAngle = deviceConfigBean.getDisplayAngle();
        // 送算法的图像角度
        algorithmImageAngle = deviceConfigBean.getAlgorithmAngle();
        // 用于ir摄像头算法角度
        irAlgorithmAngle = deviceConfigBean.getSupAlgorithmAngle();

        // 预览图像是否需要镜像
        isDisplayMirror = deviceConfigBean.isDisplayMirror();
        // 送算法图像是否镜像
        isAlgorithmMirror = deviceConfigBean.isMirror();


        seekbar_camera_display_angle.setProgress(displayAngle);
        tv_camera_display_angle_value.setText(displayAngle + "");
        seekbar_algorithm_image_angle.setProgress(algorithmImageAngle);
        tv_algorithm_image_angle_value.setText(algorithmImageAngle + "");
        seekbar_ir_algorithm_angle.setProgress(irAlgorithmAngle);
        tv_ir_algorithm_angle_value.setText(irAlgorithmAngle + "");

        setSwitchView(iv_preview_image_isMirror, isDisplayMirror);
        setSwitchView(iv_algorithm_isMirror, isAlgorithmMirror);

    }


    @Override
    protected void initListener() {

        seekbar_camera_display_angle.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override  //当滑块进度改变时，会执行该方法下的代码
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                displayAngle = i;
            }

            @Override  //当开始滑动滑块时，会执行该方法下的代码
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override   //当结束滑动滑块时，会执行该方法下的代码
            public void onStopTrackingTouch(SeekBar seekBar) {
                displayAngle = refreshView(displayAngle);
                seekbar_camera_display_angle.setProgress(displayAngle);
                tv_camera_display_angle_value.setText(displayAngle + "");

                AppLogUtils.e("displayAngle = " + displayAngle);

            }
        });


        seekbar_algorithm_image_angle.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override  //当滑块进度改变时，会执行该方法下的代码
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                algorithmImageAngle = i;
            }

            @Override  //当开始滑动滑块时，会执行该方法下的代码
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override   //当结束滑动滑块时，会执行该方法下的代码
            public void onStopTrackingTouch(SeekBar seekBar) {
                algorithmImageAngle = refreshView(algorithmImageAngle);
                seekbar_algorithm_image_angle.setProgress(algorithmImageAngle);
                tv_algorithm_image_angle_value.setText(algorithmImageAngle + "");

                AppLogUtils.e("algorithmImageAngle = " + algorithmImageAngle);
            }
        });


        seekbar_ir_algorithm_angle.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override  //当滑块进度改变时，会执行该方法下的代码
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                irAlgorithmAngle = i;
            }

            @Override  //当开始滑动滑块时，会执行该方法下的代码
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override   //当结束滑动滑块时，会执行该方法下的代码
            public void onStopTrackingTouch(SeekBar seekBar) {
                irAlgorithmAngle = refreshView(irAlgorithmAngle);
                seekbar_ir_algorithm_angle.setProgress(irAlgorithmAngle);
                tv_ir_algorithm_angle_value.setText(irAlgorithmAngle + "");

                AppLogUtils.e("irAlgorithmAngle = " + irAlgorithmAngle);
            }
        });


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.item_preview_image_isMirror:

                isDisplayMirror = !isDisplayMirror;

                setSwitchView(iv_preview_image_isMirror, isDisplayMirror);

                break;
            case R.id.item_algorithm_isMirror:

                isAlgorithmMirror = !isAlgorithmMirror;

                setSwitchView(iv_algorithm_isMirror, isAlgorithmMirror);

                break;
            case R.id.tv_confirm:

                handleDataAndRequest();

                break;
        }
    }

    private void handleDataAndRequest() {


        deviceConfigBean.setDisplayAngle(displayAngle);
        deviceConfigBean.setAlgorithmAngle(algorithmImageAngle);
        deviceConfigBean.setSupAlgorithmAngle(irAlgorithmAngle);

        deviceConfigBean.setIsDisplayMirror(isDisplayMirror);
        deviceConfigBean.setIsMirror(isAlgorithmMirror);

//        ToastUtils.showLongToast("本地配置已生效");
        ConfigData.setDeviceConfigResultBean(deviceConfigBean);

        // 开始请求接口
        mDeviceConfigUpdatePresenter.requestDeviceConfigUpdate(deviceConfigBean);

    }


    private void setSwitchView(ImageView iv, boolean flag) {
        if (flag) {
            UIUtils.setImageResource(iv, R.mipmap.iv_swich_open);
        } else {
            UIUtils.setImageResource(iv, R.mipmap.iv_swich_close);
        }
    }

    private int default_unit = 90;//滑动停留单位

    private int refreshView(int quota) {
        if (quota % default_unit > 0 && quota != 270 && quota != 0) {
            int remainder = quota % default_unit;
            if (remainder > default_unit / 2 && default_unit * (((int) (quota / default_unit)) + 1) <= 270) {
                quota = default_unit * (((int) (quota / default_unit)) + 1);
            } else {
                quota = default_unit * (int) (quota / default_unit);
            }
            return quota;
        } else {
            return 0;
        }
    }

    @Override
    public void showDeviceConfigUpdateData(SimpleResultBean resultBean) {
        ToastUtils.showLongToast("在线配置已更新");

        delayFinish();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void dismissLoading() {

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void showError(String url, String msg) {
        // 开始保存日志
        AppLogUtils.saveLog(LogType.Logcat, new StringBuilder()
                .append(LINE)
                .append((new Throwable().getStackTrace())[0]).append(WARP)
                .append(NODE)
                .append("网络请求结果异常").append(WARP)
                .append("url = ").append(url).append(WARP)
                .append("msg = ").append(msg)
                .toString()
        );

        ToastUtils.showLongToast(msg + "");

        delayFinish();
    }
}
