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

import org.iotivity.OCClientResponse;
import org.iotivity.OCRepresentation;
import org.iotivity.OCResponseHandler;
import org.iotivity.bridge.homeserver.BridgeServer;
import org.iotivity.bridge.homeserver.BridgeServerActivity;
import org.iotivity.bridge.homeserver.model.Light;
import org.iotivity.oc.OcRemoteResource;

public class ObserveLightResponseHandler implements OCResponseHandler {

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

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

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

    @Override
    public void handler(OCClientResponse response) {
        activity.msg("OBSERVER Light:");
        OCRepresentation rep = response.getPayload();
        while (rep != null) {
            switch (rep.getType()) {
                case OC_REP_BOOL:
                    activity.msg("\t" + 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());
                    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());
                    if(Light.URI_LIGHT.equals(resource.getUri())) {
                        light.setLightResourceName(rep.getValue().getString());
                    } else if(Light.URI_DIMMING.equals(resource.getUri())) {
                        light.setDimmingResourceName(rep.getValue().getString());
                    }
                    break;
                default:
                    break;
            }
            rep = rep.getNext();
        }
        activity.printLine();
    }
}
