package com.uxxu.konashi.lib;

import android.bluetooth.BluetoothGattCharacteristic;

import com.uxxu.konashi.lib.events.KonashiAnalogEvent;
import com.uxxu.konashi.lib.events.KonashiDeviceInfoEvent;
import com.uxxu.konashi.lib.events.KonashiDigitalEvent;
import com.uxxu.konashi.lib.events.KonashiEvent;
import com.uxxu.konashi.lib.events.KonashiUartEvent;

import java.util.UUID;

/**
 * Created by izumin on 8/4/15.
 */
public enum KonashiCharacteristic {
    ANALOG_READ0(KonashiUUID.ANALOG_READ0_UUID, KonashiAnalogEvent.UPDATE_ANALOG_VALUE_AIO0) {
        @Override
        public void handle(BluetoothGattCharacteristic characteristic, KonashiNotifier notifier) {
            int value = KonashiUtils.getAnalogValue(Konashi.AIO0, characteristic);
            notifyKonashiEvent(notifier, value, null);
        }
    },
    ANALOG_READ1(KonashiUUID.ANALOG_READ1_UUID, KonashiAnalogEvent.UPDATE_ANALOG_VALUE_AIO1) {
        @Override
        public void handle(BluetoothGattCharacteristic characteristic, KonashiNotifier notifier) {
            int value = KonashiUtils.getAnalogValue(Konashi.AIO1, characteristic);
            notifyKonashiEvent(notifier, value, null);
        }
    },
    ANALOG_READ2(KonashiUUID.ANALOG_READ2_UUID, KonashiAnalogEvent.UPDATE_ANALOG_VALUE_AIO2) {
        @Override
        public void handle(BluetoothGattCharacteristic characteristic, KonashiNotifier notifier) {
            int value = KonashiUtils.getAnalogValue(Konashi.AIO2, characteristic);
            notifyKonashiEvent(notifier, value, null);
        }
    },
    BATTERY_LEVEL(KonashiUUID.BATTERY_LEVEL_UUID, KonashiDeviceInfoEvent.UPDATE_BATTERY_LEVEL) {
        @Override
        public void handle(BluetoothGattCharacteristic characteristic, KonashiNotifier notifier) {
            int value = KonashiUtils.getBatteryLevel(characteristic);
            notifyKonashiEvent(notifier, value, null);
        }
    },
    PIO_SETTING(KonashiUUID.PIO_SETTING_UUID, KonashiDigitalEvent.UPDATE_PIO_SETTING) {
        @Override
        public void handle(BluetoothGattCharacteristic characteristic, KonashiNotifier notifier) {
            notifyKonashiEvent(notifier, characteristic.getValue()[0], null);
        }
    },
    PIO_PULLUP(KonashiUUID.PIO_PULLUP_UUID, KonashiDigitalEvent.UPDATE_PIO_PULLUP) {
        @Override
        public void handle(BluetoothGattCharacteristic characteristic, KonashiNotifier notifier) {
            notifyKonashiEvent(notifier, characteristic.getValue()[0], null);
        }
    },
    PIO_INPUT(KonashiUUID.PIO_INPUT_NOTIFICATION_UUID, KonashiDigitalEvent.UPDATE_PIO_INPUT) {
        @Override
        public void handle(BluetoothGattCharacteristic characteristic, KonashiNotifier notifier) {
            notifyKonashiEvent(notifier, characteristic.getValue()[0], null);
        }
    },
    UART_RX(KonashiUUID.UART_RX_NOTIFICATION_UUID, KonashiUartEvent.UART_RX_COMPLETE) {
        @Override
        public void handle(BluetoothGattCharacteristic characteristic, KonashiNotifier notifier) {
            notifyKonashiEvent(notifier, characteristic.getValue(), null);
        }
    },
    UNKNOWN(null, null) {
        @Override
        public void handle(BluetoothGattCharacteristic characteristic, KonashiNotifier notifier) {
            // Do nothing...
        }
    };

    private UUID mUuid;
    private KonashiEvent mEvent;

    KonashiCharacteristic(UUID uuid, KonashiEvent event) {
        mUuid = uuid;
        mEvent = event;
    }

    public static KonashiCharacteristic valueOf(UUID uuid) {
        if (uuid != null) {
            for (KonashiCharacteristic handler : values()) {
                if (uuid.equals(handler.getUuid())) {
                    return handler;
                }
            }
        }
        return UNKNOWN;
    }

    protected void notifyKonashiEvent(KonashiNotifier notifier, Object param1, Object param2) {
        notifier.notifyKonashiEvent(mEvent, param1, param2);
    }

    public UUID getUuid() {
        return mUuid;
    }

    public KonashiEvent getKonashiEvent() {
        return mEvent;
    }

    abstract public void handle(BluetoothGattCharacteristic characteristic, KonashiNotifier notifier);
}

@@ -452,16 +452,105 @@
 istic %7B%0A
+%0A    /* ==== Analog ================================================================ */%0A%0A
     ANAL
@@ -1646,363 +1646,93 @@
  %7D,%0A
-    BATTERY_LEVEL(KonashiUUID.BATTERY_LEVEL_UUID, KonashiDeviceInfoEvent.UPDATE_BATTERY_LEVEL) %7B%0A        @Override%0A        public void handle(BluetoothGattCharacteristic characteristic, KonashiNotifier notifier) %7B%0A            int value = KonashiUtils.getBatteryLevel(characteristic);%0A            notifyKonashiEvent(notifier, value, null);%0A        %7D%0A    %7D,
+%0A    /* ==== PIO ================================================================ */%0A
 %0A   
@@ -2628,24 +2628,111 @@
    %7D%0A    %7D,%0A
+%0A    /* ==== UART ================================================================ */%0A%0A
     UART_RX(
@@ -3005,31 +3005,571 @@
 ;%0A        %7D%0A
-
     %7D,%0A
+%0A    /* ==== Device info ================================================================ */%0A%0A    BATTERY_LEVEL(KonashiUUID.BATTERY_LEVEL_UUID, KonashiDeviceInfoEvent.UPDATE_BATTERY_LEVEL) %7B%0A        @Override%0A        public void handle(BluetoothGattCharacteristic characteristic, KonashiNotifier notifier) %7B%0A            int value = KonashiUtils.getBatteryLevel(characteristic);%0A            notifyKonashiEvent(notifier, value, null);%0A        %7D%0A    %7D,%0A%0A    /* ==== UNKNOWN ================================================================ */%0A%0A
     UNKNOWN(

Add comments