package main

import (
	"encoding/json"
	"io"
	"net/http"

	log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"
	gisClient "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-gis-engine-client"
)

func main() {
        log.MeepTextLogInit("system_test_http_stub")

	// Set routing rules
	http.HandleFunc("/sandbox-ctrl/v1/events/MOBILITY", sandbox_ctrl_mobility)
	http.HandleFunc("/gis/v1/geodata/cellularPower", gis_geodata_cellular_power)

	//Use the default DefaultServeMux.
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		log.Fatal(err)
	}
}

/*func convertPredictedQostoJson(predictedQos *gisClient.PredictedQos) string {
	jsonInfo, err := json.Marshal(*predictedQos)
	if err != nil {
		log.Error(err.Error())
		return ""
	}
	return string(jsonInfo)
}*/

func sandbox_ctrl_mobility(w http.ResponseWriter, r *http.Request) {
	log.Debug(">>> sandbox_ctrl_mobility: ", r)

        w.Header().Set("Content-Type", "application/json")
	io.WriteString(w, "")

        log.Debug("<<< sandbox_ctrl_mobility: ", w)
}

func gis_geodata_cellular_power(w http.ResponseWriter, r *http.Request) {
	log.Debug(">>> gis_geodata_cellular_power: ", r)

        w.Header().Set("Content-Type", "application/json")
	var requestData gisClient.GeoCoordinateList
	decoder := json.NewDecoder(r.Body)
	err := decoder.Decode(&requestData)
	if err != nil {
		log.Error(err.Error())
                w.WriteHeader(http.StatusBadRequest)
		return
	}

	log.Debug("gis_geodata_cellular_power: ", requestData)
	// TODO Use Tables instead of hardcoded values {43.733505 7.413917} {43.733517 7.413916}
	io.WriteString(w, "{\"coordinatesPower\": [{\"latitude\": 43.733505,\"longitude\": 7.413917,\"rsrq\": 63,\"rsrp\": 21,\"poaName\": \"4g-macro-cell-5\"},{\"latitude\": 43.733517,\"longitude\": 7.413916,\"rsrq\": 60,\"rsrp\": 58,\"poaName\": \"4g-macro-cell-5\"}]}")
	log.Debug("<<< gis_geodata_cellular_power: ", w)
}
