/*
 * Copyright (c) 2020  InterDigital Communications, Inc
 *
 * Licensed under the Apache License, Version 2.0 (the \"License\");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an \"AS IS\" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * MEEP Demo App API
 *
 * No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
 *
 * API version: 0.0.1
 * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
 */

package server

import (
	"fmt"
	"net/http"
	"strings"

	"github.com/gorilla/mux"
)

type Route struct {
	Name        string
	Method      string
	Pattern     string
	HandlerFunc http.HandlerFunc
}

type Routes []Route

func NewRouter() *mux.Router {
	router := mux.NewRouter().StrictSlash(true)
	for _, route := range routes {
		var handler http.Handler = route.HandlerFunc
		handler = Logger(handler, route.Name)

		router.
			Methods(route.Method).
			Path(route.Pattern).
			Name(route.Name).
			Handler(handler)
	}

	router.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("./static/"))))

	return router
}

func Index(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello World!")
}

var routes = Routes{
	Route{
		"Index",
		"GET",
		"/v1/",
		Index,
	},

	Route{
		"GetEdgeInfo",
		strings.ToUpper("Get"),
		"/v1/edge-app",
		GetEdgeInfo,
	},

	Route{
		"PostTrackingNotification",
		strings.ToUpper("Post"),
		"/v1/location_notifications/{subscriptionId}",
		PostTrackingNotification,
	},

	Route{
		"HandleEvent",
		strings.ToUpper("Post"),
		"/v1/mg/event",
		HandleEvent,
	},

	Route{
		"GetUeLocation",
		strings.ToUpper("Get"),
		"/v1/location/{ueId}",
		GetUeLocation,
	},

	Route{
		"CreateUeState",
		strings.ToUpper("Post"),
		"/v1/ue/{ueId}",
		CreateUeState,
	},

	Route{
		"DeleteUeState",
		strings.ToUpper("Delete"),
		"/v1/ue/{ueId}",
		DeleteUeState,
	},

	Route{
		"GetUeState",
		strings.ToUpper("Get"),
		"/v1/ue/{ueId}",
		GetUeState,
	},

	Route{
		"UpdateUeState",
		strings.ToUpper("Put"),
		"/v1/ue/{ueId}",
		UpdateUeState,
	},
}
