package aicare.net.cn.sdk.ailinksdkdemoandroid.modules.gasDetector;

import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import androidx.appcompat.app.ActionBar;

import com.pingwang.bluetoothlib.AILinkBleManager;
import com.pingwang.bluetoothlib.device.BleDevice;
import com.pingwang.bluetoothlib.listener.OnCallbackBle;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import aicare.net.cn.sdk.ailinksdkdemoandroid.R;
import aicare.net.cn.sdk.ailinksdkdemoandroid.base.BleAppNewBaseActivity;
import cn.net.aicare.modulelibrary.module.gasDetector.GasDetectorCurrentInfoBean;
import cn.net.aicare.modulelibrary.module.gasDetector.GasDetectorDevice;
import cn.net.aicare.modulelibrary.module.gasDetector.GasDetectorHistoryInfoBean;
import cn.net.aicare.modulelibrary.module.gasDetector.OnGasDetectorInfoListener;

/**
 * 气体探测器界面
 *
 * @author xing
 * @date 2024/09/05
 */
public class GasDetectorActivity extends BleAppNewBaseActivity implements View.OnClickListener, OnCallbackBle, OnGasDetectorInfoListener {

    private String mMac = "";
    private EditText etInterval, etDensity, etHpa, etInHg;
    private GasDetectorDevice mGasDetectorDevice;
    private ListView list_view;
    private List<String> mList;
    private ArrayAdapter mListAdapter;

    @Override
    protected void uiHandlerMessage(Message msg) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_gas_detector;
    }

    @Override
    protected void initListener() {
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setTitle("气体检测仪");
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btnClearLog) {//清除日志
            mList.clear();
            addText("清除日志");
        } else if (id == R.id.btnPauseLog) {//暂停日志
            isPause = !isPause;
            addText("暂停日志:" + isPause);
        } else if (id == R.id.btnGetCurInfo) {//实时信息
            mGasDetectorDevice.getCurrentInfo();
            addText("获取实时信息");
        } else if (id == R.id.btnSyncTime) {//同步时间
            mGasDetectorDevice.setSyncTime();
            addText("同步时间");
        } else if (id == R.id.btnGetCO2Density) {//CO2报警浓度
            mGasDetectorDevice.getCo2Alarm();
            addText("获取CO2报警浓度");
        } else if (id == R.id.btnGetCODensity) {//CO报警浓度
            mGasDetectorDevice.getCoAlarm();
            addText("获取CO报警浓度");
        } else if (id == R.id.btnGetO2Density) {//O2报警浓度
            mGasDetectorDevice.getO2Alarm();
            addText("获取O2报警浓度");
        } else if (id == R.id.btnGetCO2Interval) {//CO2间隔
            mGasDetectorDevice.getCo2Interval();
            addText("获取CO2间隔");
        } else if (id == R.id.btnGetCOInterval) {//CO间隔
            mGasDetectorDevice.getCoInterval();
            addText("获取CO间隔");
        } else if (id == R.id.btnGetHistoryInterval) {//历史记录间隔
            mGasDetectorDevice.getOfflineHistoryInterval();
            addText("获取历史记录间隔");
        } else if (id == R.id.btnGetCO2Switch) {//CO2开关
            mGasDetectorDevice.getCo2Switch();
            addText("获取CO2开关");
        } else if (id == R.id.btnGetCOSwitch) {//CO开关
            mGasDetectorDevice.getCoSwitch();
            addText("获取CO开关");
        } else if (id == R.id.btnGetSoundSwitch) {//声音开关
            mGasDetectorDevice.getSoundSwitch();
            addText("获取声音开关");
        } else if (id == R.id.btnGetShakeSwitch) {//震动开关
            mGasDetectorDevice.getShockSwitch();
            addText("获取震动开关");
        } else if (id == R.id.btnGetTempUnit) {//温度单位
            mGasDetectorDevice.getTempUnit();
            addText("获取温度单位");
        } else if (id == R.id.btnGetPressureUnit) {//气压单位
            mGasDetectorDevice.getAirPressureUnit();
            addText("获取气压单位");
        } else if (id == R.id.btnGetScreenOffTime) {//息屏时间
            mGasDetectorDevice.getScreenOffTime();
            addText("获取息屏时间");
        } else if (id == R.id.btnGetShutdownTime) {//关机时间
            mGasDetectorDevice.getShutdownTime();
            addText("获取关机时间");
        } else if (id == R.id.btnGetHistory) {//历史记录
            mGasDetectorDevice.getOfflineHistory(0);
            addText("获取历史记录");
        } else if (id == R.id.btnClearHistory) {//清空历史记录
            mGasDetectorDevice.setClearOfflineHistory();
            addText("清空历史记录");
        } else if (id == R.id.btnSetCo2Density) {//设置Co2浓度
            try {
                int densityCo2 = Integer.parseInt(etDensity.getText().toString());
                mGasDetectorDevice.setCo2Alarm(densityCo2);
                addText("设置CO2报警浓度:" + densityCo2);
            } catch (NumberFormatException e) {
                addText("请输入正确的数字");
            }
        } else if (id == R.id.btnSetO2Density) {//设置O2浓度
            try {
                int densityO2 = Integer.parseInt(etDensity.getText().toString());
                mGasDetectorDevice.setO2Alarm(densityO2);
                addText("设置O2报警浓度:" + densityO2);
            } catch (NumberFormatException e) {
                addText("请输入正确的数字");
            }
        } else if (id == R.id.btnSetCo2Interval) {//设置CO2间隔
            try {
                int intervalCo2 = Integer.parseInt(etInterval.getText().toString());
                mGasDetectorDevice.setCo2Interval(intervalCo2);
                addText("设置CO2间隔:" + intervalCo2);
            } catch (NumberFormatException e) {
                addText("请输入正确的数字");
            }
        } else if (id == R.id.btnSetCoInterval) {//设置CO间隔
            try {
                int intervalCo = Integer.parseInt(etInterval.getText().toString());
                mGasDetectorDevice.setCoInterval(intervalCo);
                addText("设置CO间隔:" + intervalCo);
            } catch (NumberFormatException e) {
                addText("请输入正确的数字");
            }
        } else if (id == R.id.btnSetPressureCali) {//气压校准
            try {
                int hpa = Integer.parseInt(etHpa.getText().toString());
                int inHg = Integer.parseInt(etInHg.getText().toString());
                mGasDetectorDevice.setAirPressureCalibration(hpa, inHg);
                addText("设置气压校准:hpa=" + hpa + "   inHg=" + inHg);
            } catch (NumberFormatException e) {
                addText("请输入正确的数字");
            }
        }
    }

    @Override
    protected void initData() {
        mMac = getIntent().getStringExtra("mac");
        mList = new ArrayList<>();
        mListAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mList);
        list_view.setAdapter(mListAdapter);
        addText("连接成功:" + mMac);
        onServiceSuccess();
    }

    @Override
    protected void initView() {
        etDensity = (EditText) findViewById(R.id.etDensity);
        etInterval = (EditText) findViewById(R.id.etInterval);
        etHpa = (EditText) findViewById(R.id.etHpa);
        etInHg = (EditText) findViewById(R.id.etInHg);
        list_view = findViewById(R.id.list_view);
    }

    @Override
    public void onServiceSuccess() {
        if (TextUtils.isEmpty(mMac)) {
            return;
        }
        if (mGasDetectorDevice != null) {
            return;
        }
        AILinkBleManager.getInstance().setOnCallbackBle(this);
        BleDevice bleDevice = AILinkBleManager.getInstance().getBleDevice(mMac);
        mGasDetectorDevice = GasDetectorDevice.init(bleDevice);
        mGasDetectorDevice.addOnGasDetectorInfoListener(this);
        mGasDetectorDevice.setOnShowData(new GasDetectorDevice.OnShowData() {
            @Override
            public void showData(String data) {
                addText("收到的vlaue数据:" + data);
            }
        });
    }

    @Override
    public void onServiceErr() {

    }

    @Override
    public void unbindServices() {

    }


    @Override
    public void onDisConnected(String mac, int code) {
        if (mac.equals(this.mMac)) {
            if (mGasDetectorDevice != null) {
                mGasDetectorDevice.removeOnGasDetectorInfoListener(this);
                mGasDetectorDevice = null;
            }
            addText("断开连接:" + mac + "  code:" + code);
        }
    }


    @Override
    public void onCurrentInfo(GasDetectorCurrentInfoBean currentInfoBean) {
        addText("当前信息:" + currentInfoBean.toString());
    }

    @Override
    public void onCo2Density(int density) {
        addText("CO2报警浓度(单位ppm):" + density);
    }

    @Override
    public void onCo2Interval(int interval) {
        addText("CO2间隔(单位min)0代表一直:" + interval);
    }

    @Override
    public void onCo2Switch(int status) {
        addText("CO2开关:" + (status == 0 ? "打开" : "关闭"));
    }

    @Override
    public void onCoDensity(int density) {
        addText("CO报警浓度(单位ppm):" + density);
    }

    @Override
    public void onCoInterval(int interval) {
        addText("CO间隔(单位min)0代表一直:" + interval);
    }

    @Override
    public void onCoSwitch(int status) {
        addText("CO开关:" + (status == 0 ? "打开" : "关闭"));
    }

    @Override
    public void onO2Density(int density) {
        addText("O2报警浓度(单位%):" + density);
    }

    @Override
    public void onHistorySaveInterval(int interval) {
        addText("历史记录保存间隔(单位:分钟,0代表一直):" + interval);
    }

    @Override
    public void onTempUnit(int unit) {
        addText("温度单位:" + (unit == 0 ? "℃" : "℉"));
    }

    @Override
    public void onPressureUnit(int unit) {
        addText("气压单位:" + (unit == 0 ? "hPa" : "inHg"));
    }

    @Override
    public void onPressureCalibration(int hpa, int inHg) {
        addText("气压校准:hpa=" + hpa + "   inHg=" + inHg);
    }

    @Override
    public void onBrightness(int brightness) {
        addText("亮度:" + brightness);
    }

    @Override
    public void onSoundSwitch(int status) {
        addText("声音开关:" + (status == 0 ? "打开" : "关闭"));
    }

    @Override
    public void onShockSwitch(int status) {
        addText("震动开关:" + (status == 0 ? "打开" : "关闭"));
    }

    @Override
    public void onSyncTime(long time) {
        addText("同步时间成功:time=" + time + "  格式化:" + sdf.format(time * 1000));
    }

    @Override
    public void onScreenOffTime(int time) {
        addText("息屏时间(单位,秒)0代表从不:" + time);
    }

    @Override
    public void onShutdownTime(int time) {
        addText("关机时间(单位,小时)0代表从不:" + time);
    }

    @Override
    public void onHistoryInfo(GasDetectorHistoryInfoBean historyInfoBean) {
        addText("历史记录:" + historyInfoBean.toString());
        if (historyInfoBean.getAllCount() != historyInfoBean.getCount()) {
            mGasDetectorDevice.getOfflineHistory(historyInfoBean.getCount());
        }
    }

    @Override
    public void onClearHistory() {
        addText("清空历史记录成功");
    }

    private SimpleDateFormat sdf;
    private String mOldText = "";
    private boolean isPause = false;

    private void addText(String text) {
        if (isPause && !mList.isEmpty()) {
            return;
        }
        if (mOldText.equals(text)) {
            return;
        }
        if (sdf == null) {
            sdf = new SimpleDateFormat("HH:mm:ss", Locale.US);
        }
        mList.add(0, sdf.format(System.currentTimeMillis()) + "：\n" + text);
        mListAdapter.notifyDataSetChanged();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        AILinkBleManager.getInstance().removeOnCallbackBle(this);
        if (mGasDetectorDevice != null) {
            mGasDetectorDevice.clear();
        }
    }
}
