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

import android.util.Log;

import org.iotivity.OCClientResponse;
import org.iotivity.OCQos;
import org.iotivity.OCRep;
import org.iotivity.OCRepresentation;
import org.iotivity.OCResponseHandler;
import org.iotivity.OCStatus;
import org.iotivity.bridge.homeserver.BridgeServerActivity;
import org.iotivity.bridge.homeserver.BridgeService;
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 org.json.JSONException;
import org.json.JSONObject;

public class PostResponseHandler implements OCResponseHandler {

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

    private BridgeService service ;
    private OcRemoteResource resource ;

    public PostResponseHandler(BridgeService service, OcRemoteResource resource) {
        this.service = service;
        this.resource = resource ;
    }

    @Override
    public void handler(OCClientResponse response) {

        Log.d(TAG, "POST "+resource.getUri()+" Response Handler: Code = "+response.getCode().toString());
        if(response.getCode() != OCStatus.OC_STATUS_CHANGED && response.getCode() != OCStatus.OC_STATUS_CREATED) {
            return;
        }

        // Observe the response of Post Request
        ObserveResponseHandler observerLight = new ObserveResponseHandler(service, resource, BridgeService.ResponseType.POST);
        OcUtils.doObserve(resource.getUri(), Utils.getCoapsEndpoint(resource.getEndpoints()), null, observerLight, OCQos.LOW_QOS);

        // Stop Observe right after Observe
        // OcUtils.stopObserve(resource.getUri(), Utils.getCoapsEndpoint(resource.getEndpoints()));

        // Stop trigger register. It will be stop Observe after some secs
        //StopObserveTriggerHandler stopObserve = new StopObserveTriggerHandler(resource);
        //OcUtils.setDelayedHandler(stopObserve, 10);
    }
}
