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

import android.util.Log;

import org.iotivity.OCInterfaceMask;
import org.iotivity.OCRequest;
import org.iotivity.OCRequestHandler;
import org.iotivity.OCStatus;
import org.iotivity.bridge.homeserver.BridgeService;
import org.iotivity.bridge.homeserver.model.Light;
import org.iotivity.bridge.homeserver.BridgeServerActivity;
import org.iotivity.oc.OcCborEncoder;
import org.iotivity.oc.OcUtils;

public class GetLightRequestHandler implements OCRequestHandler {

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

    private BridgeService service;
    private Light light;

    public GetLightRequestHandler(BridgeService service, Light light) {
        this.service = service;
        this.light = light;
    }

    @Override
    public void handler(OCRequest request, int interfaces) {
        Log.d(TAG, "inside Get Light Request Handler");

        service.msg("Get "+request.getResource().getUri()+" Request Received.");

        OcCborEncoder root = OcCborEncoder.createOcCborEncoder(OcCborEncoder.EncoderType.ROOT);
        switch (interfaces) {
            case OCInterfaceMask.BASELINE: {
                root.processBaselineInterface(request.getResource());
                break;
            }
            case OCInterfaceMask.RW:
            case OCInterfaceMask.A:
                if(Light.URI_LIGHT.equalsIgnoreCase(request.getResource().getUri())) {
                    // Light Switch Optional Resource (read only)
                    root.setTextString(Light.RESOURCE_NAME_KEY, light.getLightResourceName());
                    // Light Switch (Switch Binary) Required Resource (read write)
                    root.setBoolean(Light.SWITCH_KEY, light.isSwitchOn());

                    service.msg("\t\t" + Light.RESOURCE_NAME_KEY + ": " + light.getLightResourceName());
                    service.msg("\t\t" + Light.SWITCH_KEY + ": " + light.isSwitchOn());
                } else if (Light.URI_DIMMING.equalsIgnoreCase(request.getResource().getUri())) {
                    // Dimming Optional Resource (read only)
                    root.setTextString(Light.RESOURCE_NAME_KEY, light.getDimmingResourceName());
                    // Dimming Required Resource (read write)
                    root.setLong(Light.DIMMING_SETTING_KEY, (long)light.getDimmingLevel());
                    // Dimming Optional Resources (read only)
                    root.setLong(Light.DIMMING_RANGE_KEY, (long)light.getDimmingRange());
                    root.setLong(Light.DIMMING_STEP_KEY, (long)light.getDimmingStep());

                    service.msg("\t\t" + Light.RESOURCE_NAME_KEY + ": " + light.getDimmingResourceName());
                    service.msg("\t\t" + Light.DIMMING_SETTING_KEY + ": " + light.getDimmingLevel());
                    service.msg("\t\t" + Light.DIMMING_RANGE_KEY + ": " + light.getDimmingRange());
                    service.msg("\t\t" + Light.DIMMING_STEP_KEY + ": " + light.getDimmingStep());
                }
                break;
            default:
                break;
        }
        root.done();
        service.msg("Get "+request.getResource().getUri()+" Response Sent.");
        service.printLine();

        OcUtils.sendResponse(request, OCStatus.OC_STATUS_OK);

    }
}
