/*
 * Copyright (c) 2019  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.
 *
 * AdvantEDGE Platform Controller REST API
 *
 * This API is the main platform API and mainly used by the AdvantEDGE frontend to interact with scenarios <p>**Micro-service**<br>[meep-ctrl-engine](https://github.com/InterDigitalInc/AdvantEDGE/tree/master/go-apps/meep-ctrl-engine) <p>**Type & Usage**<br>Platform main interface used by controller software that want to interact with the AdvantEDGE platform <p>**Details**<br>API details available at _your-AdvantEDGE-ip-address:30000/api_ <p>**Default Port**<br>`30000`
 *
 * API version: 1.0.0
 * Contact: AdvantEDGE@InterDigital.com
 * 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 = Logger(route.HandlerFunc, 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{
		"CreateReplayFile",
		strings.ToUpper("Post"),
		"/v1/replay/{name}",
		CreateReplayFile,
	},

	Route{
		"CreateReplayFileFromScenarioExec",
		strings.ToUpper("Post"),
		"/v1/replay/{name}/generate",
		CreateReplayFileFromScenarioExec,
	},

	Route{
		"DeleteReplayFile",
		strings.ToUpper("Delete"),
		"/v1/replay/{name}",
		DeleteReplayFile,
	},

	Route{
		"DeleteReplayFileList",
		strings.ToUpper("Delete"),
		"/v1/replay",
		DeleteReplayFileList,
	},

	Route{
		"GetReplayStatus",
		strings.ToUpper("Get"),
		"/v1/replaystatus",
		GetReplayStatus,
	},

	Route{
		"GetReplayFile",
		strings.ToUpper("Get"),
		"/v1/replay/{name}",
		GetReplayFile,
	},

	Route{
		"GetReplayFileList",
		strings.ToUpper("Get"),
		"/v1/replay",
		GetReplayFileList,
	},

	Route{
		"LoopReplay",
		strings.ToUpper("Post"),
		"/v1/replay/{name}/loop",
		LoopReplay,
	},

	Route{
		"PlayReplayFile",
		strings.ToUpper("Post"),
		"/v1/replay/{name}/play",
		PlayReplayFile,
	},

	Route{
		"StopReplayFile",
		strings.ToUpper("Post"),
		"/v1/replay/{name}/stop",
		StopReplayFile,
	},

	Route{
		"GetStates",
		strings.ToUpper("Get"),
		"/v1/states",
		GetStates,
	},

	Route{
		"CreateScenario",
		strings.ToUpper("Post"),
		"/v1/scenarios/{name}",
		CreateScenario,
	},

	Route{
		"DeleteScenario",
		strings.ToUpper("Delete"),
		"/v1/scenarios/{name}",
		DeleteScenario,
	},

	Route{
		"DeleteScenarioList",
		strings.ToUpper("Delete"),
		"/v1/scenarios",
		DeleteScenarioList,
	},

	Route{
		"GetScenario",
		strings.ToUpper("Get"),
		"/v1/scenarios/{name}",
		GetScenario,
	},

	Route{
		"GetScenarioList",
		strings.ToUpper("Get"),
		"/v1/scenarios",
		GetScenarioList,
	},

	Route{
		"SetScenario",
		strings.ToUpper("Put"),
		"/v1/scenarios/{name}",
		SetScenario,
	},

	Route{
		"ActivateScenario",
		strings.ToUpper("Post"),
		"/v1/active/{name}",
		ActivateScenario,
	},

	Route{
		"GetActiveNodeServiceMaps",
		strings.ToUpper("Get"),
		"/v1/active/serviceMaps",
		GetActiveNodeServiceMaps,
	},

	Route{
		"GetActiveScenario",
		strings.ToUpper("Get"),
		"/v1/active",
		GetActiveScenario,
	},

	Route{
		"SendEvent",
		strings.ToUpper("Post"),
		"/v1/events/{type}",
		SendEvent,
	},

	Route{
		"TerminateScenario",
		strings.ToUpper("Delete"),
		"/v1/active",
		TerminateScenario,
	},
}
