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

import android.util.Log;

import org.iotivity.OCCoreRes;
import org.iotivity.OCMain;
import org.iotivity.OCRepresentation;
import org.iotivity.OCRequest;
import org.iotivity.OCRequestHandler;
import org.iotivity.OCStatus;
import org.iotivity.OCType;
import org.iotivity.OCUuid;
import org.iotivity.OCUuidUtil;
import org.iotivity.bridge.homeserver.BridgeService;
import org.iotivity.bridge.homeserver.model.Light;
import org.iotivity.bridge.homeserver.model.OCFDevice;
import org.iotivity.oc.OcCborException;
import org.iotivity.oc.OcRepresentation;
import org.iotivity.oc.OcUtils;
import org.json.JSONException;
import org.json.JSONObject;

public class PostRequestHandler implements OCRequestHandler {

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

    private BridgeService service;
    private OCFDevice device;

    public PostRequestHandler(BridgeService service, OCFDevice device) {
        this.service = service;
        this.device = device;
    }

    @Override
    public void handler(OCRequest request, int interfaces) {
        Log.d(TAG, "inside Post Request Handler");
        long device_index = request.getResource().getDevice();
        OCUuid uuid = OCCoreRes.getDeviceId(device_index);
        String device_id = OCUuidUtil.uuidToString(uuid);
        String resource_uri = request.getResource().getUri();

        Log.d(TAG, "Post "+resource_uri+" of "+device_id+"("+device.getDeviceName()+") Request Received.");
        try {

            OcRepresentation rep = new OcRepresentation(request.getRequestPayload());
            JSONObject obj = new JSONObject();
            while (rep != null) {
                if(rep.getType() == OCType.OC_REP_BOOL) {
                    obj.put(rep.getKey(), rep.getBoolean());
                    Log.d(TAG, "\t\t" + rep.getKey() + ": " + rep.getBoolean());
                } else if(rep.getType() == OCType.OC_REP_STRING) {
                    obj.put(rep.getKey(), rep.getString());
                    Log.d(TAG, "\t\t" + rep.getKey() + ": " + rep.getString());
                } else if(rep.getType() == OCType.OC_REP_INT) {
                    obj.put(rep.getKey(), rep.getLong());
                    Log.d(TAG, "\t\t" + rep.getKey() + ": " + rep.getLong());
                } else if(rep.getType() == OCType.OC_REP_DOUBLE) {
                    obj.put(rep.getKey(), rep.getDouble());
                    Log.d(TAG, "\t\t" + rep.getKey() + ": " + rep.getDouble());
                } else if(rep.getType() == OCType.OC_REP_ARRAY) {
                    obj.put(rep.getKey(), rep.getArray());
                    Log.d(TAG, "\t\t" + rep.getKey() + ": " + rep.getArray().toString());
                }

                rep = rep.getNext();
            }
            // TODO : doPostProperties() with Light Control Service
            service.doPostLightControlResource(device_index, device_id, resource_uri, obj.toString());

        } catch (JSONException e) {
            Log.e(TAG, "JSON Error", e);
        } catch (OcCborException e) {
            Log.e(TAG, "CBOR Error", e);
        }

        //service.printLine();
        OcUtils.sendResponse(request, OCStatus.OC_STATUS_CHANGED);
    }
}
