package org.iotivity.bridge.homeserver.handler.client;

import android.util.Log;
import android.util.Pair;

import org.iotivity.OCClientResponse;
import org.iotivity.OCQos;
import org.iotivity.OCRepresentation;
import org.iotivity.OCResponseHandler;
import org.iotivity.OCStatus;
import org.iotivity.bridge.homeserver.BridgeServerActivity;
import org.iotivity.bridge.homeserver.model.Light;
import org.iotivity.bridge.homeserver.utils.Utils;
import org.iotivity.oc.OcRemoteResource;
import org.iotivity.oc.OcUtils;

import java.util.ArrayList;

public class GetLight2ResponseHandler implements OCResponseHandler {

    private static final String TAG = GetLight2ResponseHandler.class.getSimpleName();

    private BridgeServerActivity activity;
    private Light light;
    private OcRemoteResource resource ;

    public GetLight2ResponseHandler(BridgeServerActivity activity, OcRemoteResource resource, Light light) {
        this.activity = activity;
        this.light = light;
        this.resource = resource ;
    }

    @Override
    public void handler(OCClientResponse response) {
        activity.msg("Get "+resource.getUri()+" Response Handler: Code = "+response.getCode().toString());
        if(response.getCode() != OCStatus.OC_STATUS_OK) {
            activity.showErrorDialog("Get "+resource.getUri()+" Response Handler Error",
                    "Result Code = " + response.getCode().toString());
            activity.printLine();
            return;
        }

        final ArrayList<Pair<String, Object>> props = new ArrayList<>();

        OCRepresentation rep = response.getPayload();
        while (rep != null) {
            switch (rep.getType()) {
                case OC_REP_BOOL:
                    activity.msg("\t" + rep.getName() + " = " + rep.getValue().getBool());
                    props.add(new Pair(rep.getName(), rep.getValue().getBool()));
                    if(Light.SWITCH_KEY.equals(rep.getName())) {
                        light.setSwitchOn(rep.getValue().getBool());
                    }
                    break;
                case OC_REP_INT:
                    activity.msg("\t" + rep.getName() + "= " + rep.getValue().getInteger());
                    props.add(new Pair(rep.getName(), rep.getValue().getInteger()));
                    if(Light.DIMMING_SETTING_KEY.equals(rep.getName())) {
                        light.setDimmingLevel((int)rep.getValue().getInteger());
                    } else if(Light.DIMMING_RANGE_KEY.equals(rep.getName())) {
                        light.setDimmingRange((int) rep.getValue().getInteger());
                    } else if(Light.DIMMING_STEP_KEY.equals(rep.getName())) {
                        light.setDimmingStep((int) rep.getValue().getInteger());
                    }
                        break;
                case OC_REP_STRING:
                    activity.msg("\t" + rep.getName() + " = " + rep.getValue().getString());
                    props.add(new Pair(rep.getName(), rep.getValue().getString()));
                    if(Light.URI_LIGHT.equalsIgnoreCase(resource.getUri())) {
                        light.setLightResourceName(rep.getValue().getString());
                    } else if(Light.URI_DIMMING.equalsIgnoreCase(resource.getUri())) {
                        light.setDimmingResourceName(rep.getValue().getString());
                    }
                    break;
                default:
                    break;
            }
            rep = rep.getNext();
        }

        activity.printLine();
        activity.showPropertiesDialog( "Properties of " + this.resource.getUri(), props, resource);
        Log.d(TAG, "Properties : " + Utils.propertiesToString(props));


        // Observe resource
        ObserveLightResponseHandler observerLight = new ObserveLightResponseHandler(activity, resource, light);
        OcUtils.doObserve(resource.getUri(), Utils.getCoapsEndpoint(resource.getEndpoints()), null, observerLight, OCQos.LOW_QOS);
        activity.msg("Sent OBSERVE request");
        activity.printLine();

        // Create Stop Observe trigger
        //StopObserveTriggerHandler stopObserve = new StopObserveTriggerHandler(activity, resource);
        //OcUtils.setDelayedHandler(stopObserve, 30);

        // Stop Observing
        //OcUtils.stopObserve(resource.getUri(), Utils.getCoapsEndpoint(resource.getEndpoints()));

    }
}
