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

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

public class PostLightResponseHandler implements OCResponseHandler {

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

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

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

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

        activity.printLine();
        activity.showResultDialog("POST "+resource.getUri()+" Response Handler Result",
                "Result Code = " + response.getCode().toString());

        /*
        Post2LightResponseHandler postLight = new Post2LightResponseHandler(activity, light);
        if (OcUtils.initPut(light.getServerUri(), light.getServerEndpoint(), null, postLight, OCQos.LOW_QOS)) {
            OcCborEncoder root = OcCborEncoder.createOcCborEncoder(OcCborEncoder.EncoderType.ROOT);
            root.setBoolean(Light.SWITCH_KEY, true);
            root.setLong(Light.DIMMING_SETTING_KEY, 55);
            root.done();

            if (OcUtils.doPost()) {
                activity.msg("\tSent POST2 request");
            } else {
                activity.msg("\tCould not send POST2 request");
            }
        } else {
            activity.msg("\tCould not init POST2 request");
        }
        activity.printLine();

         */
    }
}
