package com.uti.sensors.bleshow;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.StringDef;
import android.util.Log;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;

import com.polidea.rxandroidble.RxBleConnection;
import com.uti.Utils.GenericTabRow;

import java.util.UUID;

import rx.Observable;

import static java.lang.StrictMath.pow;

/**
 * Created by kunyi on 2017/5/14.
 */

public class TempertureProfile extends GenericProfile {
    private final boolean DBG = false;
    private final String TAG = "TempertureProfile";
    private final static String GattServ = "F000AA00-0451-4000-B000-0000000000000";
    private final static String GattData = "F000AA01-0451-4000-B000-0000000000000";
    private final static String GattConf = "F000AA02-0451-4000-B000-0000000000000";
    private final static String GattPeri = "F000AA03-0451-4000-B000-0000000000000";
    private final static byte[] Bconf =  new byte[] {(byte)0x01 };
    private double ambient;
    private double target;
    protected GenericTabRow tr;
    protected GenericTabRow am;
    protected Context context;

    public TempertureProfile(@NonNull Observable<RxBleConnection> conn) {
        super(conn,
                UUID.fromString(GattServ),
                UUID.fromString(GattConf),
                UUID.fromString(GattData),
                UUID.fromString(GattPeri),
                Bconf);
    }

    @Override
    public boolean registerNotification(Context con, View parenet, TableLayout tabLayout) {
        am = new GenericTabRow(con);
        am.sl1.autoScale = true;
        am.sl1.autoScale = true;
        am.sl1.autoScaleBounceBack = true;
        am.setIcon("sensortag2", "temperature");
        am.title.setText("Ambient Temperature Data");
        am.uuidLabel.setText(GattData);
        am.value.setText("0.0'C");
        am.periodMinVal = 200;
        am.periodBar.setMax(255 - (am.periodMinVal/10));
        am.periodBar.setProgress(100);
        am.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        tr = new GenericTabRow(con);
        tr.sl1.autoScale = true;
        tr.sl1.autoScaleBounceBack = true;
        tr.setIcon("sensortag2", "irtemperature");
        tr.title.setText("IR Temperature Data");
        tr.uuidLabel.setText(GattData);
        tr.value.setText("0.0'C");
        tr.periodMinVal = 200;
        tr.periodBar.setMax(255 - (tr.periodMinVal/10));
        tr.periodBar.setProgress(100);
        tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        tabLayout.addView(am, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
        tabLayout.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));


        super.registerNotificationImp(bytes -> {
            convertRaw(bytes);
            if (DBG)
                Log.d(TAG, "Ambient value:" + ambient + ", Target:" + target);
            am.value.setText(String.format("%.1fC", ambient));
            tr.value.setText(String.format("%.1fC", target));

        });
        return true;
    }

    @Override
    public boolean configuration() {
        super.configurationImp(bytes -> {
            if (DBG)
                Log.d(TAG, "configuration complete");
        });
        return true;
    }

    @Override
    protected void convertRaw(byte[] bytes) {
        final double SCALE_LSB = (0.03125f);
        // TI -- TMP007 sensor algorithm
        target = (int16AtOffset(bytes, 0)>>2)*SCALE_LSB;
        ambient = (int16AtOffset(bytes, 2)>>2)*SCALE_LSB;
    }
}

@@ -1045,22 +1045,21 @@
 private 
-double
+float
  ambient
@@ -1072,22 +1072,21 @@
 private 
-double
+float
  target;
@@ -2437,33 +2437,33 @@
 lue.setText(%220.0
-'
+%C2%B0
 C%22);%0A        tr.
@@ -3209,24 +3209,25 @@
 format(%22%25.1f
+%C2%B0
 C%22, ambient)
@@ -3225,24 +3225,62 @@
  ambient));%0A
+            am.sl1.addValue(ambient);%0A
             
@@ -3315,16 +3315,17 @@
 at(%22%25.1f
+%C2%B0
 C%22, targ
@@ -3330,16 +3330,54 @@
 rget));%0A
+            tr.sl1.addValue(target);%0A%0A
 %0A       
@@ -3695,14 +3695,13 @@
 nal 
-double
+float
  SCA

update TermertureProfile