package com.hdl.sdk.hdl_sdk.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.hdl.sdk.hdl_core.HDLAppliances.Config.HDLApConfig;
import com.hdl.sdk.hdl_core.HDLAppliances.HDLLight.CCTBackInfo;
import com.hdl.sdk.hdl_core.HDLAppliances.HDLLight.RGBBackInfo;
import com.hdl.sdk.hdl_core.HDLDeviceManger.Bean.AppliancesInfo;
import com.hdl.sdk.hdl_core.HDLDeviceManger.Core.HDLCommand;
import com.hdl.sdk.hdl_core.HDLDeviceManger.EventBusEvent.DeviceStateEvent;
import com.hdl.sdk.hdl_core.HDLDeviceManger.EventBusEvent.LightCCTCtrlBackEvent;
import com.hdl.sdk.hdl_core.HDLDeviceManger.EventBusEvent.LightRGBCtrlBackEvent;
import com.hdl.sdk.hdl_sdk.R;
import com.hdl.sdk.hdl_sdk.base.BaseActivity;
import com.hdl.sdk.hdl_sdk.utlis.HDLLog;

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

public class CtrlCCTLightActivity extends BaseActivity {
    /**
     * Topbar
     */
    private RelativeLayout topBarBack;
    private TextView topBarTitle;
    private Button btnOpen, btnClose;
    //    private EditText etR,etG,etB;
    private SeekBar seekBar, seekBarCCT;
    private TextView switchText;
    private AppliancesInfo appliancesInfo;
    private int settingBrightness = 0;
    private int settingCCT = 2000;
    private CCTBackInfo mCCTBackInfo;

    //默认色温范围值 2000K 到7000K,不一定所有设备都是这个范围
    private int MIN_CCT = 2000;
    private int MAX_CCT = 7000;


    /**
     * 复写isRegisterEventBus()  要注册使用EventBus,这里要设置返回true
     *
     * @return true
     */
    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

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

        initToolbar();
        initcurState();
        initView();
        initOnClick();
        displayStateView();
        mCCTBackInfo = new CCTBackInfo(appliancesInfo);

        //读取状态
        HDLCommand.getRgbOrCctStateFromNetwork(appliancesInfo);

    }

    /**
     * 初始化Toolbar
     */
    private void initToolbar() {
//        topBarBack = findViewById(R.id.ll_top_b_left);
//        setViewVisible(topBarBack);
//        topBarTitle = findViewById(R.id.tv_top_b_header_title);
//        topBarBack.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                finish();
//            }
//        });
    }

    private void initcurState() {
        appliancesInfo = (AppliancesInfo) getIntent().getSerializableExtra("hdl");

    }

    private void initView() {
        btnOpen = findViewById(R.id.btnOpen);
        btnClose = findViewById(R.id.btnClose);
        seekBar = findViewById(R.id.sb_brightness);
        seekBarCCT = findViewById(R.id.sb_cct);
        switchText = findViewById(R.id.switchText);
    }

    private void initOnClick() {


        btnOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int brightness = settingBrightness == 0 ? 100 : settingBrightness;
                if (settingCCT > MAX_CCT) {
                    settingCCT = MAX_CCT;
                } else if (settingCCT < MIN_CCT) {
                    settingCCT = MIN_CCT;
                }
                HDLCommand.lightCCTCtrl(appliancesInfo, brightness, settingCCT);
            }
        });

        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (settingCCT > MAX_CCT) {
                    settingCCT = MAX_CCT;
                } else if (settingCCT < MIN_CCT) {
                    settingCCT = MIN_CCT;
                }
                HDLCommand.lightCCTCtrl(appliancesInfo, 0, settingCCT);
            }
        });

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (seekBar.getProgress() > 0) {
                    settingBrightness = seekBar.getProgress();
                }
                if (settingCCT > MAX_CCT) {
                    settingCCT = MAX_CCT;
                } else if (settingCCT < MIN_CCT) {
                    settingCCT = MIN_CCT;
                }
                HDLCommand.lightCCTCtrl(appliancesInfo, seekBar.getProgress(), settingCCT);
            }
        });

        seekBarCCT.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                settingCCT = seekBarProgressToValue(MIN_CCT, MAX_CCT, seekBar.getProgress());
                if (settingCCT > MAX_CCT) {
                    settingCCT = MAX_CCT;
                } else if (settingCCT < MIN_CCT) {
                    settingCCT = MIN_CCT;
                }

                HDLLog.I("色温设置:" + settingCCT + "K");
                int brightness = settingBrightness == 0 ? 100 : settingBrightness;
                HDLCommand.lightCCTCtrl(appliancesInfo, brightness, settingCCT);
            }
        });

    }

    private void displayStateView() {

        switch (appliancesInfo.getDeviceType()) {
            case HDLApConfig.TYPE_LIGHT_CCT:
                break;
            default:
                finish();//设备类型不对结束页面
                break;
        }
        /**根据需求是否发送一次获取刷新状态请求*/

    }


    /**
     * showCCTBackInfo
     *
     * @param mCCTBackInfo
     */
    private void showCCTBackInfo(CCTBackInfo mCCTBackInfo) {
        String message = "";
        if (mCCTBackInfo.getBrightness() > 0) {
            //如果大于0才记住当前设置的亮度值,下次打开时继续这个亮度值
            settingBrightness = mCCTBackInfo.getBrightness();

            message = "当前状态:开";
            message += "\n" + "亮度:" + mCCTBackInfo.getBrightness();
            message += "\n" + "色温值:" + mCCTBackInfo.getCctValue() + "K";

        } else {
            message = "当前状态:关";
        }

        settingCCT = mCCTBackInfo.getCctValue();
        seekBar.setProgress(mCCTBackInfo.getBrightness());
        int cctProgress = seekBarValueToProgress(MIN_CCT, MAX_CCT, mCCTBackInfo.getCctValue());
        seekBarCCT.setProgress(cctProgress);
        switchText.setText(message);
        showToast(message);
        HDLLog.I(message);
    }

    /**
     * RGB控制回调Event
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLightCCTCtrlBackEventMain(LightCCTCtrlBackEvent event) {
        if (event.getCCTBackInfo().getAppliancesInfo().getDeviceDeviceID() == appliancesInfo.getDeviceDeviceID()
                && event.getCCTBackInfo().getAppliancesInfo().getDeviceSubnetID() == appliancesInfo.getDeviceSubnetID()
                && event.getCCTBackInfo().getAppliancesInfo().getChannelNum() == appliancesInfo.getChannelNum()
        ) {
            if (!event.isSuccess()) {
                showToast("RGB灯控制超时,请重新再试");
                return;
            }

//            showToast("RGB灯控制成功");
            mCCTBackInfo = event.getCCTBackInfo();
            showCCTBackInfo(mCCTBackInfo);
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDeviceStateEventMain(DeviceStateEvent event) {
//        proDialog.dismiss();
        if (event.getAppliancesInfo().getDeviceSubnetID() == appliancesInfo.getDeviceSubnetID()
                && event.getAppliancesInfo().getDeviceDeviceID() == appliancesInfo.getDeviceDeviceID()
        ) {
            //这个返回的信息是当前状态的
            switch (event.getAppliancesInfo().getDeviceType()) {
                case HDLApConfig.TYPE_LIGHT_CCT:
                    if (appliancesInfo.getChannelNum() == event.getAppliancesInfo().getChannelNum()) {
                        if (!event.isSuccess()) {
                            showToast("获取CCT灯状态失败,请重新再试");
                            return;
                        }
                        String message = "";
                        CCTBackInfo cctBackInfo = new CCTBackInfo(event.appliancesInfo);
                        if (cctBackInfo == null) {
                            showToast("获取CCT灯状态失败,请重新再试");
                            return;
                        }
                        mCCTBackInfo = cctBackInfo;
                        showCCTBackInfo(mCCTBackInfo);
                    }
                    break;
            }
        }
    }


    /**
     * 根据滑条刻度值,给定最大最小值 计算出对应显示值
     * 滑条刻度值范围0~100
     *
     * @param maxValue  最大值
     * @param minValue  最小值
     * @param mProgress 滑条值
     * @return 计算后的显示值
     */
    private int seekBarProgressToValue(int minValue, int maxValue, int mProgress) {
        int progress = mProgress;

        if (progress < 0) {
            progress = 0;
        } else if (progress > 100) {
            progress = 100;
        }
        int intValue = 0;
        intValue = (maxValue - minValue) * progress / 100 + minValue;
        return intValue;
    }

    /**
     * 根据显示值,给定最大最小值 计算出对滑条刻度
     * 滑条刻度值范围0~100
     *
     * @param maxValue  最大值
     * @param minValue  最小值
     * @param mIntValue 显示值
     * @return 计算后的滑条值
     */
    private int seekBarValueToProgress(int minValue, int maxValue, int mIntValue) {
        int intValue = mIntValue;

        if (intValue < minValue) {
            intValue = minValue;
        } else if (intValue > maxValue) {
            intValue = maxValue;
        }
        int progress = 0;
        progress = (intValue - minValue) * 100 / (maxValue - minValue);
        return progress;


    }
}