package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble;

import android.util.Pair;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.UUID;

import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes;
import nodomain.freeyourgadget.gadgetbridge.model.Weather;
import ru.gelin.android.weather.notification.ParcelableWeather2;

public class AppMessageHandlerMarioTime extends AppMessageHandler {

    public static final int KEY_WEATHER_ICON_ID = 10;
    public static final int KEY_WEATHER_TEMPERATURE = 11;
    public static final int KEY_WEATHER_REQUEST = 12;

    private static final Logger LOG = LoggerFactory.getLogger(AppMessageHandlerMarioTime.class);

    public AppMessageHandlerMarioTime(UUID uuid, PebbleProtocol pebbleProtocol) {
        super(uuid, pebbleProtocol);
    }

    private byte[] encodeWeatherMessage(int temperature, int condition) {
        ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>(2);
        pairs.add(new Pair<>(KEY_WEATHER_ICON_ID, (Object) (byte) condition));
        pairs.add(new Pair<>(KEY_WEATHER_TEMPERATURE, (Object) (byte) temperature));
        byte[] weatherMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs);

        ByteBuffer buf = ByteBuffer.allocate(weatherMessage.length);

        // encode ack and put in front of push message (hack for acknowledging the last message)
        buf.put(weatherMessage);

        return buf.array();
    }

    @Override
    public GBDeviceEvent[] handleMessage(ArrayList<Pair<Integer, Object>> pairs) {
        boolean weatherRequested = false;
        for (Pair<Integer, Object> pair : pairs) {
            switch (pair.first) {
                case KEY_WEATHER_REQUEST:
                    LOG.info("got weather request");
                    weatherRequested = true;
                    break;
                default:
                    LOG.info("unknown key " + pair.first);
            }
        }
        if (!weatherRequested) {
            return new GBDeviceEvent[]{null};
        }
        ParcelableWeather2 weather = Weather.getInstance().getWeather2();

        GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
        sendBytes.encodedBytes = encodeWeatherMessage(weather.currentConditionCode, weather.currentTemp - 273);

        GBDeviceEventSendBytes sendBytesAck = new GBDeviceEventSendBytes();
        sendBytesAck.encodedBytes = mPebbleProtocol.encodeApplicationMessageAck(mUUID, mPebbleProtocol.last_id);

        return new GBDeviceEvent[]{sendBytesAck, sendBytes};
    }
}

@@ -506,23 +506,16 @@
 ther2;%0A%0A
-public 
 class Ap
@@ -569,29 +569,30 @@
 ler %7B%0A%0A    p
-ublic
+rivate
  static fina
@@ -624,29 +624,30 @@
  = 10;%0A    p
-ublic
+rivate
  static fina
@@ -687,21 +687,22 @@
 1;%0A    p
-ublic
+rivate
  static 
@@ -840,23 +840,16 @@
 );%0A%0A    
-public 
 AppMessa
@@ -1473,105 +1473,8 @@
 );%0A%0A
-        // encode ack and put in front of push message (hack for acknowledging the last message)%0A
     
@@ -2138,74 +2138,255 @@
     
-ParcelableWeather2 weather = Weather.getInstance().getWeather2();%0A
+return pushMessage();%0A    %7D%0A%0A    @Override%0A    public GBDeviceEvent%5B%5D pushMessage() %7B%0A        ParcelableWeather2 weather = Weather.getInstance().getWeather2();%0A%0A        if (weather == null) %7B%0A            return new GBDeviceEvent%5B%5D%7Bnull%7D;%0A        %7D
 %0A   
@@ -2528,50 +2528,48 @@
 rent
-ConditionCode, weather.currentTemp - 273);
+Temp - 273, 1); // FIXME: condition code
 %0A%0A  
@@ -2817,16 +2817,17 @@
 Bytes%7D;%0A
+%0A
     %7D%0A%7D%0A

fix mario time appmessage handler (weather condition icon hardcoded to sunny for now)