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.model.Bridge;
import org.iotivity.bridge.homeserver.BridgeServerActivity;
import org.iotivity.bridge.homeserver.model.VirtualOCFDevice;
import org.iotivity.oc.OcCborEncoder;
import org.iotivity.oc.OcUtils;

public class GetBridgeRequestHandler implements OCRequestHandler {

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

    private BridgeServerActivity activity;
    private Bridge bridge;

    public GetBridgeRequestHandler(BridgeServerActivity activity, Bridge bridge) {
        this.activity = activity;
        this.bridge = bridge;
    }

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

        activity.msg("Get Bridge :" + request.getResource().getUri());
        activity.msg("\t" + bridge.getName() + ", " + bridge.getEcoName() + ", " + bridge.isSecureMode());
        activity.printLine();

        if(request.getResource().getUri().equals(Bridge.SECUREMODE_URI)) {

            OcCborEncoder root = OcCborEncoder.createOcCborEncoder(OcCborEncoder.EncoderType.ROOT);
            switch (interfaces) {
                case OCInterfaceMask.BASELINE: {
                    root.processBaselineInterface(request.getResource());
                    break;
                }
                case OCInterfaceMask.RW: {
                    root.setBoolean(Bridge.SECUREMODE_RESOURCE_NAME, bridge.isSecureMode());
                    break;
                }
                default:
                    break;
            }
            root.done();
            OcUtils.sendResponse(request, OCStatus.OC_STATUS_OK);
        }
        else if(request.getResource().getUri().equals(Bridge.VODLIST_URI)) {

            OcCborEncoder root = OcCborEncoder.createOcCborEncoder(OcCborEncoder.EncoderType.ROOT);
            switch (interfaces) {
                case OCInterfaceMask.BASELINE: {
                    root.processBaselineInterface(request.getResource());
                    break;
                }
                case OCInterfaceMask.R: {
                    root.setStringArray("rt", new String[]{ Bridge.VODLIST_RT });
                    OcCborEncoder vodList = OcCborEncoder.createOcCborEncoder(OcCborEncoder.EncoderType.ARRAY, root, Bridge.VODS);
                    for (VirtualOCFDevice vod : bridge.getVirtualOCFDeviceList()) {
                        OcCborEncoder mediaObject = OcCborEncoder.createOcCborEncoder(OcCborEncoder.EncoderType.ARRAY_ITEM, vodList);
                        mediaObject.setTextString(VirtualOCFDevice.NAME_KEY, vod.getDeviceName());
                        mediaObject.setTextString(VirtualOCFDevice.ECONAME_KEY, vod.getEcoName());
                        mediaObject.setTextString(VirtualOCFDevice.DI_KEY, vod.getVirtualDeviceId());
                        mediaObject.done();

                        activity.msg("\t\t" + vod.getDeviceName() + ", " + vod.getEcoName() + "," + vod.getVirtualDeviceId());
                    }
                    vodList.done();
                    break;
                }
                default:
                    break;
            }
            root.done();
            OcUtils.sendResponse(request, OCStatus.OC_STATUS_OK);
        }

    }
}
