package com.phlx.wool.ui.setting;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.phlx.wool.App;
import com.phlx.wool.Configs;
import com.phlx.wool.R;
import com.phlx.wool.rfid.ModuleManager;
import com.phlx.wool.ui.base.BackBarVM;
import com.phlx.wool.util.TextUtil;
import com.uhf.structures.Rfid_Value;

import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.ToastUtils;


/**
 * <pre>
 *     author : lei
 *     e-mail : 18600395998@163.com
 *     time   : 2019/05/23
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class SettingVM extends BackBarVM {

    public ObservableField<String> power = new ObservableField<>("");

    public ObservableField<String> settingStatusText = new ObservableField<>("");
    public ObservableField<String> settingButtonText = new ObservableField<>("");

    public SingleLiveEvent<Boolean> saveIpEvent = new SingleLiveEvent<>();
    public SingleLiveEvent<Boolean> bluetoothEvent = new SingleLiveEvent<>();
    public SingleLiveEvent<Boolean> disBluetoothEvent = new SingleLiveEvent<>();

    public SettingVM(@NonNull Application application) {
        super(application);
    }

    //初始化ToolBar
    public void initToolbar(int settingStatusInt) {
        setBackTitleText("设置");
        setBackIconVisibleObservable(View.GONE);

        setBlueStatus(Configs.isConnected ? 0 : 1);

        if (settingStatusInt == 1)
            bluetoothEvent.call();
    }


    //保存ip
    public BindingCommand saveIpOnClick = new BindingCommand(() -> {
        saveIpEvent.call();
    });
    //获取功率
    public BindingCommand getPowerOnClick = new BindingCommand(this::getAntennaPower);
    //设置功率
    public BindingCommand setPowerOnClick = new BindingCommand(this::setAntennaPower);
    //连接蓝牙
    public BindingCommand connectBlueOnClick = new BindingCommand(this::connectBlule);

    public void setBlueStatus(int status) {
        switch (status) {
            case 0://已连接
                settingStatusText.set(getApplication().getResources().getString(R.string.blue_status_connect));
                settingButtonText.set(getApplication().getResources().getString(R.string.disconnect_blue));
                break;
            case 1://未连接
                settingStatusText.set(getApplication().getResources().getString(R.string.blue_status_unconnect));
                settingButtonText.set(getApplication().getResources().getString(R.string.connect_blue));
                break;
            case 2://已断开
                settingStatusText.set(getApplication().getResources().getString(R.string.blue_status_disconnect));
                settingButtonText.set(getApplication().getResources().getString(R.string.connect_blue));
                break;
        }

    }

    /**
     * 获取天线功率
     */
    private void getAntennaPower() {
        if (ModuleManager.powerOn() != 0) {
            ToastUtils.showShort("模块连接失败");
            ModuleManager.powerOff();
            return;
        }
        Rfid_Value power_value = new Rfid_Value();
        int powerStatue =
                App.getInstance().getLinkage().Radio_GetAntennaPower(power_value);
        if (powerStatue == 0) {
            String value = String.valueOf((power_value.value) / 10);
            power.set(value);
            ToastUtils.showShort("获取天线功率成功");
        } else {
            ToastUtils.showShort("获取天线功率失败");
        }

    }

    /**
     * 设置天线功率
     */
    private void setAntennaPower() {
        if (ModuleManager.powerOn() != 0) {
            ToastUtils.showShort("模块连接失败");
            ModuleManager.powerOff();
            return;
        }
        if (TextUtil.isSpace(power.get())) {
            ToastUtils.showShort("功率不能为空");
            return;
        }
        int status_power;
        int iu32 = (int) (Double.parseDouble(power.get()) * 10);
        if (iu32 < 0) {
            ToastUtils.showShort("参数不可用");
            return;
        }
        status_power = App.getInstance().getLinkage().Radio_SetAntennaPower(iu32);

        if (status_power == 0) {
            ToastUtils.showShort("功率修改成功");

        } else if (status_power == -1000) {
            ToastUtils.showShort("正在盘点");
        } else {
            ToastUtils.showShort("设置功率失败");
        }
    }

    public void connectBlule() {

        if (Configs.isConnected)
            disBluetoothEvent.call();
        else
            bluetoothEvent.call();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ModuleManager.powerOff();
    }
}
