package com.hdl.sdk.hdl_sdk.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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.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.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 CtrlRGBLightActivity extends BaseActivity {
    /**
     * Topbar
     */
    private RelativeLayout topBarBack;
    private TextView topBarTitle;
    private Button btnOpen, btnClose, btnRGB;
    private EditText etR, etG, etB;
    private SeekBar seekBar;
    private TextView switchText;
    private AppliancesInfo appliancesInfo;
    private int settingBrightness = 0;
    private RGBBackInfo mRGBBackInfo;

//    private RGBLightBean mRGBLightBean;


    /**
     * 复写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_r_g_b_light);

        initToolbar();
        initcurState();
        initView();
        initOnClick();
        displayStateView();
        mRGBBackInfo = new RGBBackInfo(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);
        btnRGB = findViewById(R.id.btnRGB);
        seekBar = findViewById(R.id.sb_brightness);
        etR = findViewById(R.id.et_r);
        etG = findViewById(R.id.et_g);
        etB = findViewById(R.id.et_b);

        switchText = findViewById(R.id.switchText);
    }

    private void initOnClick() {
        btnRGB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(etR.getText().toString()) || TextUtils.isEmpty(etG.getText().toString()) || TextUtils.isEmpty(etB.getText().toString())) {
                    showToast("RGB值 不能为空");
                } else {
                    int rStatus = Integer.parseInt(etR.getText().toString());
                    int gStatus = Integer.parseInt(etG.getText().toString());
                    int bStatus = Integer.parseInt(etB.getText().toString());
                    if (rStatus < 0 || rStatus > 255 || gStatus < 0 || gStatus > 255 || bStatus < 0 || bStatus > 255) {
                        showToast("RGB值范围 0~255");
                        return;
                    }
                    int brightness = settingBrightness == 0 ? 100 : settingBrightness;
                    HDLCommand.lightRGBCtrl(appliancesInfo, brightness, rStatus, gStatus, bStatus);
                }
            }
        });

        btnOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int brightness = settingBrightness == 0 ? 100 : settingBrightness;
                HDLCommand.lightRGBCtrl(appliancesInfo, brightness, mRGBBackInfo.getrStatus(), mRGBBackInfo.getgStatus(), mRGBBackInfo.getbStatus());
            }
        });

        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HDLCommand.lightRGBCtrl(appliancesInfo, 0, mRGBBackInfo.getrStatus(), mRGBBackInfo.getgStatus(), mRGBBackInfo.getbStatus());
            }
        });

        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();
                }

                HDLCommand.lightRGBCtrl(appliancesInfo, seekBar.getProgress(), mRGBBackInfo.getrStatus(), mRGBBackInfo.getgStatus(), mRGBBackInfo.getbStatus());
            }
        });
    }

    private void displayStateView() {

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

    }


    /**
     * showRGBBackInfoInfo
     *
     * @param mRGBBackInfo
     */
    private void showRGBBackInfoInfo(RGBBackInfo mRGBBackInfo) {
        String message = "";
        if (mRGBBackInfo.getBrightness() > 0) {
            //如果大于0才记住当前设置的亮度值,下次打开时继续这个亮度值
            settingBrightness = mRGBBackInfo.getBrightness();
            message = "当前状态:开";
            message += "\n" + "亮度:" + mRGBBackInfo.getBrightness();
            message += "\n" + "R:" + mRGBBackInfo.getrStatus();
            message += "\n" + "G:" + mRGBBackInfo.getgStatus();
            message += "\n" + "B:" + mRGBBackInfo.getbStatus();
        } else {
            message = "当前状态:关";
        }


        seekBar.setProgress(mRGBBackInfo.getBrightness());
        etR.setText(String.valueOf(mRGBBackInfo.getrStatus()));
        etG.setText(String.valueOf(mRGBBackInfo.getgStatus()));
        etB.setText(String.valueOf(mRGBBackInfo.getbStatus()));
        switchText.setText(message);
        showToast(message);
        HDLLog.I(message);
    }

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

//            showToast("RGB灯控制成功");
            mRGBBackInfo = event.getRGBBackInfo();
            showRGBBackInfoInfo(mRGBBackInfo);
        }
    }


    @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_RGB:
                    if (appliancesInfo.getChannelNum() == event.getAppliancesInfo().getChannelNum()) {
                        if (!event.isSuccess()) {
                            showToast("获取RGB状态失败,请重新再试");
                            return;
                        }
                        String message = "";
                        RGBBackInfo rgbBackInfo = new RGBBackInfo(event.appliancesInfo);
                        if (rgbBackInfo == null) {
                            showToast("获取RGB状态失败,请重新再试");
                            return;
                        }
                        mRGBBackInfo = rgbBackInfo;
                        showRGBBackInfoInfo(mRGBBackInfo);
                    }
                    break;
            }
        }
    }
}