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.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 SingleLiveEvent<Boolean> saveIpEvent = new SingleLiveEvent<>();

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

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


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


    /**
     * 获取天线功率
     */
    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("设置功率失败");
        }
    }

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