package com.miya.hardware;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.miya.weighing.Weighing;
import com.miya.weighing.WeighingManager;

public class WeighingActivity extends Activity implements View.OnClickListener {

    final static int MSG_UPDATE_RESULT = 0x01;

    EditText etGravity, etTare;

    TextView tvResult;

    TextView tvResultNow;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_UPDATE_RESULT:
                    tvResult.setText("当前称重量(g):" + msg.getData().getString("weight") + "  ;是否稳定：" + msg.getData().getBoolean("isStable"));
                    break;
            }
        }
    };

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

        tvResult = findViewById(R.id.tvResult);
        tvResultNow = findViewById(R.id.tvResultNow);

        etGravity = findViewById(R.id.etGravity);
        etTare = findViewById(R.id.etTare);

        findViewById(R.id.btnInit).setOnClickListener(this);
        findViewById(R.id.btnRead).setOnClickListener(this);
        findViewById(R.id.btnZero).setOnClickListener(this);
        findViewById(R.id.btnTare).setOnClickListener(this);
        findViewById(R.id.btnGravity).setOnClickListener(this);
        findViewById(R.id.btnRelease).setOnClickListener(this);
        findViewById(R.id.btnReadNow).setOnClickListener(this);
        findViewById(R.id.ivBack).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnInit:
                boolean isSuccess = WeighingManager.getInstance().init(this.getApplicationContext());
                if (isSuccess == true) {
                    tvResult.setText("初始化成功：硬件为：" + WeighingManager.getInstance().getWeighing().getWeighingName());
                } else {
                    tvResult.setText("没找到称重硬件！");
                }
                break;
            case R.id.btnRead:
                if (WeighingManager.getInstance().isConnected()) {
                    WeighingManager.getInstance().getWeighing().setWeighingCallback(new Weighing.WeighingCallback() {
                        @Override
                        public void returnWeight(int weight, boolean isStable) {
                            //抛至主线程
                            Bundle bundle = new Bundle();
                            bundle.putString("weight", weight + "");
                            bundle.putBoolean("isStable", isStable);
                            Message msg = new Message();
                            msg.what = MSG_UPDATE_RESULT;
                            msg.setData(bundle);
                            handler.sendMessage(msg);
                        }
                    });
                }
                break;
            case R.id.btnZero:
                if (WeighingManager.getInstance().isConnected()) {
                    WeighingManager.getInstance().getWeighing().resetZero();
                }
                break;
            case R.id.btnTare:
                if (WeighingManager.getInstance().isConnected()) {
                    try {
                        WeighingManager.getInstance().getWeighing().setTare(Integer.parseInt(etTare.getText().toString()));
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
                break;
            case R.id.btnGravity:
                if (WeighingManager.getInstance().isConnected()) {
                    try {
                        WeighingManager.getInstance().getWeighing().setGravity(Integer.parseInt(etGravity.getText().toString()));
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
                break;
            case R.id.btnRelease:
                if (WeighingManager.getInstance().isConnected()) {
                    if (WeighingManager.getInstance().getWeighing().release() == true) {
                        tvResultNow.setText("断开连接成功!");
                    }
                }
                break;
            case R.id.btnReadNow:
                if (WeighingManager.getInstance().isConnected()) {
                    tvResultNow.setText(WeighingManager.getInstance().getWeighing().getWeight() + "");
                }
                break;
            case R.id.ivBack:
                finish();
                break;
        }
        if (WeighingManager.getInstance().isConnected() == false) {
            tvResult.setText("当前没有称重设备，试试初始化!");
            return;
        }
    }
}
