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

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;

import androidx.annotation.Nullable;

import com.pingwang.bluetoothlib.bean.SupportUnitBean;
import com.pingwang.bluetoothlib.config.CmdConfig;
import com.pingwang.bluetoothlib.device.BleDevice;
import com.pingwang.bluetoothlib.listener.OnBleSettingListener;

import java.math.BigDecimal;
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.BleBaseActivity;
import aicare.net.cn.sdk.ailinksdkdemoandroid.utils.TimeUtils;
import cn.net.aicare.modulelibrary.module.BleNutrition.BleNutritionData;
import cn.net.aicare.modulelibrary.module.BleNutrition.BleNutritionHistoryBean;

public class BleNutritionActivity extends BleBaseActivity implements View.OnClickListener, BleNutritionData.BleNutritionCallback, OnBleSettingListener {

    private static final String TAG = "Tag1";

    private Button btn_clear;
    private ListView list_view;
    private Button btn_set_unit;
    private Button btn_set_zero;
    private List<RadioButton> rb_list;

    private List<String> mList;
    private ArrayAdapter mListAdapter;

    private String mMac;
    private BleDevice mBleDevice;
    private boolean mBuzzOpen = false;
    private BleNutritionData mBleNutritionData;

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

        btn_clear = findViewById(R.id.btn_clear);
        list_view = findViewById(R.id.list_view);
        btn_set_unit = findViewById(R.id.btn_set_unit);
        btn_set_zero = findViewById(R.id.btn_set_zero);

        btn_clear.setOnClickListener(this);
        btn_set_unit.setOnClickListener(this);
        btn_set_zero.setOnClickListener(this);
        findViewById(R.id.btnVoice).setOnClickListener(this);
        findViewById(R.id.btnReadHistory).setOnClickListener(this);
        findViewById(R.id.btnClearHistory).setOnClickListener(this);
        findViewById(R.id.btnSynTime).setOnClickListener(this);

        rb_list = new ArrayList<>();
        rb_list.add(findViewById(R.id.rb_g));
        rb_list.add(findViewById(R.id.rb_ml));
        rb_list.add(findViewById(R.id.rb_lb_oz));
        rb_list.add(findViewById(R.id.rb_oz));
        rb_list.add(findViewById(R.id.rb_kg));
        rb_list.add(findViewById(R.id.rb_jin));
        rb_list.add(findViewById(R.id.rb_milk_ml));
        rb_list.add(findViewById(R.id.rb_water_ml));
        rb_list.add(findViewById(R.id.rb_milk_fl_oz));
        rb_list.add(findViewById(R.id.rb_water_fl_oz));
        rb_list.add(findViewById(R.id.rb_lb));

        // 单位只能单选
        for (RadioButton radioButton : rb_list) {
            radioButton.setOnClickListener(v -> {
                for (RadioButton rb : rb_list) {
                    rb.setChecked(rb == v);
                }
            });
        }

        // 获取Mac
        mMac = getIntent().getStringExtra("mac");

        // 初始化列表
        mList = new ArrayList<>();
        mListAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mList);
        list_view.setAdapter(mListAdapter);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_clear) {
            // 清空文本框
            clearText();
        } else if (id == R.id.btn_set_unit) {
            // 设置单位
            setUnit();
        } else if (id == R.id.btn_set_zero) {
            // 去皮指令
            setZero();
        } else if (id == R.id.btnVoice) {
            //蜂鸣器开关切换
            mBuzzOpen = !mBuzzOpen;
            setBuzz(mBuzzOpen);
        } else if (id == R.id.btnReadHistory) {
            // 获取历史数据
            getHistoryData();
        } else if (id == R.id.btnClearHistory) {
            // 清空历史数据
            clearHistoryData();
        } else if (id == R.id.btnSynTime) {
            // 同步时间
            appSynTime();
        }
    }

    @Override
    protected void onDestroy() {
        if (mAILinkBleManager != null) {
            mAILinkBleManager.disconnectAll();
        }
        super.onDestroy();
    }

    @Override
    public void onServiceSuccess() {
        mBleDevice = mAILinkBleManager.getBleDevice(mMac);
        if (mBleDevice != null) {
            mBleDevice.setOnBleSettingListener(this);
            mBleNutritionData = new BleNutritionData(mBleDevice);
            mBleNutritionData.setBleNutritionCallback(this);
            addText("连接成功：" + mMac);
        }
    }

    @Override
    public void onServiceErr() {

    }

    @Override
    public void unbindServices() {

    }

    @Override
    public void mcuBmVersion(String version) {
        addText("MCU上发BM版本：" + version);
    }

    @Override
    public void mcuSupportUnit(List<SupportUnitBean> list) {
        String str = "";
        for (SupportUnitBean supportUnitBean : list) {
            str += supportUnitBean.toString() + "；";
            if (supportUnitBean.getType() != null && supportUnitBean.getType().equals("8")) {
                // 遍历所有单位，有这个支持单位就显示
                for (int i = 0; i < rb_list.size(); i++) {
                    boolean hasUnit = false;
                    for (Integer integer : supportUnitBean.getSupportUnit()) {
                        if (integer != null && integer == i) {
                            hasUnit = true;
                            break;
                        }
                    }
                    rb_list.get(i).setEnabled(hasUnit);
                }
                // 再次遍历，如果选中了不支持的单位，就重新选择
                boolean unitDisable = false;
                for (int i = 0; i < rb_list.size(); i++) {
                    if (!rb_list.get(i).isEnabled() && rb_list.get(i).isChecked()) {
                        unitDisable = true;
                        rb_list.get(i).setChecked(false);
                    }
                    if (rb_list.get(i).isEnabled() && unitDisable) {
                        unitDisable = false;
                        rb_list.get(i).setChecked(true);
                    }
                }
            }
        }
        addText("MCU上发支持单位列表：\n" + str);
    }

    @Override
    public void mcuWeight(int no, int weight, int unit, int decimal, int symbol, int type) {

        float w = weight;
        // 正负
        if (symbol == 1) {
            w *= -1;
        }
        // 小数点
        w = (float) (w / Math.pow(10, decimal));
        // 保留小数位

        String weightStr = "";
        if (unit == 2) {
            int lb = (int) (w / 16);
            float oz = w - (lb * 16);
            weightStr = lb + ":" + getPreFloatStr(oz, decimal);
        } else {
            weightStr = getPreFloatStr(w, decimal);
        }

        String str = "MCU上发重量：" + weightStr +" "+ getUnitStr(unit) + "\n流水号：" + no + "，原始重量：" + weight + "，单位：" + unit + "，小数点：" + decimal + "，符号：" + symbol + "，重量类型：" + type;
        addText(str);
    }

    @Override
    public void mcuUnitResult(int status) {
        String statusStr = "";
        switch (status) {
            case 0:
                statusStr = "成功";
                break;
            case 1:
                statusStr = "失败";
                break;
            case 2:
                statusStr = "不支持";
                break;
        }
        String str = "MCU上发设置单位结果：" + statusStr;
        addText(str);
    }

    @Override
    public void mcuErr(int weightErr, int batteryErr) {
        String weightErrStr = "";
        switch (weightErr) {
            case 0:
                weightErrStr = "正常";
                break;
            case 1:
                weightErrStr = "超重";
                break;
        }
        String batteryErrStr = "";
        switch (batteryErr) {
            case 0:
                batteryErrStr = "正常";
                break;
            case 1:
                batteryErrStr = "低电";
                break;
        }
        String str = "MCU上发异常报警：\n重量状态：" + weightErrStr + "\n电池状态：" + batteryErrStr;
        addText(str);
    }

    @Override
    public void onHistoryRecord(int schedule, List<BleNutritionHistoryBean> list) {
        if (schedule < 99) {
            addText("获取历史数据：进度" + schedule + "%");
            return;
        }
        addText("获取历史数据：进度" + schedule + "%");
        addText("获取历史数据：" + list.size() + "条");
        StringBuilder str = new StringBuilder();
        for (BleNutritionHistoryBean bean : list) {
            str.append("时间：").append(TimeUtils.getTimeNoNewline(bean.getTime() * 1000L)).append(";");
            str.append("重量：").append(bean.getSymbol() == 1 ? "-" : "").append(bean.getWeight()).append(";");
            str.append("单位：").append(getUnitStr(bean.getUnit())).append(";");
            str.append("小数点：").append(bean.getDecimal()).append(";");
            str.append("\n");
        }
        addText("历史数据：\n" + str.toString());
        addText("APP获取历史数据完成");
    }

    @Override
    public void onClearHistoryRecord(int status) {
        addText("清除历史数据结果：" + (status == 0x00 ? "成功" : "失败"));
    }

    private SimpleDateFormat sdf;

    // 添加一条文本
    private void addText(String text) {
        if (sdf == null) {
            sdf = new SimpleDateFormat("HH:mm:ss", Locale.US);
        }
        mList.add(sdf.format(System.currentTimeMillis()) + "：\n" + text);
        mListAdapter.notifyDataSetChanged();
        list_view.smoothScrollToPosition(mList.size() - 1);
    }

    // 清空文本
    private void clearText() {
        mList.clear();
        mListAdapter.notifyDataSetChanged();
    }

    // 保留小数位
    private String getPreFloatStr(float f, int decimal) {
        BigDecimal bigDecimal = new BigDecimal(f);
        return bigDecimal.setScale(decimal, BigDecimal.ROUND_HALF_UP).toString();
    }

    /**
     * APP 发送归零指令
     */
    private void setZero() {
        if (mBleNutritionData != null) {
            mBleNutritionData.setZero();
            addText("APP下发去皮指令");
        }
    }

    /**
     * 设置蜂鸣器
     *
     * @param open 开放
     */
    private void setBuzz(boolean open) {
        if (mBleNutritionData != null) {
            mBleNutritionData.setBuzz(open);
            addText("APP下发蜂鸣器指令:" + open);
        }
    }


    /**
     * APP 下发单位
     */
    private void setUnit() {
        if (mBleNutritionData != null) {
            int unit = -1;
            for (int i = 0; i < rb_list.size(); i++) {
                if (rb_list.get(i).isChecked()) {
                    unit = i;
                    break;
                }
            }
            if (unit == -1) {
                addText("APP下发单位：失败：未选择单位");
                return;
            }
            mBleNutritionData.setUnit(unit);
            addText("APP下发单位：" + unit + "：" + getUnitStr(unit));
        }
    }

    /**
     * 获取单位字符串
     *
     * @param unit 0
     * @return g
     */
    private String getUnitStr(int unit) {
        String unitStr = "";
        switch (unit) {
            case 0x00:
                unitStr = "g";
                break;
            case 0x01:
                unitStr = "ml";
                break;
            case 0x02:
                unitStr = "lb:oz";
                break;
            case 0x03:
                unitStr = "oz";
                break;
            case 0x04:
                unitStr = "kg";
                break;
            case 0x05:
                unitStr = "斤";
                break;
            case 0x06:
                unitStr = "牛奶ml";
                break;
            case 0x07:
                unitStr = "水ml";
                break;
            case 0x08:
                unitStr = "牛奶floz";
                break;
            case 0x09:
                unitStr = "水floz";
                break;
            case 0x0A:
                unitStr = "lb";
                break;
        }
        return unitStr;
    }

    /**
     * 获取历史数据
     */
    public void getHistoryData() {
        if (mBleNutritionData != null) {
            mBleNutritionData.getHistoryData(0);
            addText("APP下发获取历史数据指令");
        }
    }

    public void clearHistoryData() {
        if (mBleNutritionData != null) {
            mBleNutritionData.clearHistoryData();
        }

    }

    /**
     * 应用同步时间
     */
    public void appSynTime() {
        if (mBleNutritionData != null) {
            mBleNutritionData.appSynTime();
            addText("APP下发同步时间指令");
        }

    }

    @Override
    public void OnSettingReturn(int cmdType, int cmdData) {
        if (cmdType == CmdConfig.SET_DEVICE_TIME_UNIX) {
            addText("MCU上发同步时间结果：" + (cmdData == 0 ? "成功" : "失败"));
        }
    }
}
