package com.aemreunal.controller.api;

import net.minidev.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.aemreunal.config.GlobalSettings;
import com.aemreunal.domain.Scenario;
import com.aemreunal.exception.MalformedRequestException;
import com.aemreunal.service.ScenarioService;

/*
 ***************************
 * Copyright (c) 2014      *
 *                         *
 * This code belongs to:   *
 *                         *
 * @author Ahmet Emre Ünal *
 * S001974                 *
 *                         *
 * aemreunal@gmail.com     *
 * emre.unal@ozu.edu.tr    *
 *                         *
 * aemreunal.com           *
 ***************************
 */

@Controller
@RequestMapping(GlobalSettings.API_PATH_MAPPING)
public class APIController {
    @Autowired
    private ScenarioService scenarioService;

    /*
     * Beacon query JSON example:
     * {
     *      "uuid":   <beacon UUID>
     *      "major":  <beacon Major>
     *      "minor":  <beacon Minor>
     *      "secret": <project Secret>
     * }
     */
    @RequestMapping(method = RequestMethod.POST, value = GlobalSettings.API_BEACON_QUERY_PATH_MAPPING, produces = "application/json; charset=UTF-8")
    public ResponseEntity<Scenario> queryForScenario(@RequestBody JSONObject beaconQueryJson) {
        verifyQueryRequest(beaconQueryJson);
        String uuid = beaconQueryJson.get("uuid").toString().toUpperCase();
        String major = beaconQueryJson.get("major").toString().toUpperCase();
        String minor = beaconQueryJson.get("minor").toString().toUpperCase();
        String secret = beaconQueryJson.get("secret").toString().toUpperCase();
        Scenario scenario = scenarioService.queryForScenario(uuid, major, minor, secret);
        // TODO do something intelligent instead of just returning the scenario
        return new ResponseEntity<Scenario>(scenario, HttpStatus.OK);
    }

    private void verifyQueryRequest(JSONObject beaconQueryJson) {
        if (!beaconQueryJson.containsKey("uuid") ||
            !beaconQueryJson.containsKey("major") ||
            !beaconQueryJson.containsKey("minor") ||
            !beaconQueryJson.containsKey("secret")) {
            throw new MalformedRequestException();
        }
    }
}

@@ -1573,24 +1573,26 @@
 eEntity%3C
-Scenario
+JSONObject
 %3E queryF
@@ -1703,593 +1703,538 @@
    S
-tring uuid = beaconQueryJson.get(%22uuid%22).toString().toUpperCase(
+cenario scenario = getScenario(beaconQueryJson);%0A        return new ResponseEntity%3CJSONObject%3E(scenario.generateQueryResponse(), HttpStatus.OK
 );%0A    
+%7D%0A%0A
     
-String major = beaconQueryJson.get(%22major%22).toString().toUpperCase();%0A        String minor = beaconQueryJson.get(%22minor%22).toString().toUpperCase();%0A        String secret = beaconQueryJson.get(%22secret%22).toString().toUpperCase();%0A        Scenario scenario = scenarioService.queryForScenario(uuid, major, minor, secret);%0A        // TODO do something intelligent instead of just returning the scenario%0A        return new ResponseEntity%3CScenario%3E(scenario, HttpStatus.OK);%0A    %7D%0A%0A    private void verifyQueryRequest
+private void verifyQueryRequest(JSONObject beaconQueryJson) %7B%0A        if (!beaconQueryJson.containsKey(%22uuid%22) %7C%7C%0A            !beaconQueryJson.containsKey(%22major%22) %7C%7C%0A            !beaconQueryJson.containsKey(%22minor%22) %7C%7C%0A            !beaconQueryJson.containsKey(%22secret%22)) %7B%0A            throw new MalformedRequestException();%0A        %7D%0A    %7D%0A%0A    private Scenario getScenario
 (JSO
@@ -2260,37 +2260,46 @@
 Json) %7B%0A        
-if (!
+String uuid = 
 beaconQueryJson.
@@ -2302,251 +2302,358 @@
 son.
-containsKey(%22uuid%22) %7C%7C%0A            !beaconQueryJson.containsKey(%22major%22) %7C%7C%0A            !beaconQueryJson.containsKey(%22minor%22) %7C%7C%0A            !beaconQueryJson.containsKey(%22secret%22)) %7B%0A            throw new MalformedRequestException();%0A        %7D
+get(%22uuid%22).toString().toUpperCase();%0A        String major = beaconQueryJson.get(%22major%22).toString().toUpperCase();%0A        String minor = beaconQueryJson.get(%22minor%22).toString().toUpperCase();%0A        String secret = beaconQueryJson.get(%22secret%22).toString().toUpperCase();%0A        return scenarioService.queryForScenario(uuid, major, minor, secret);
 %0A   

Return scenario fields upon query