//
//  tank
//
//  Import and Playlist Daemon for autoradio project
//
//
//  Copyright (C) 2017 Christian Pointner <equinox@helsinki.at>
//
//  This file is part of tank.
//
//  tank is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  any later version.
//
//  tank is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with tank. If not, see <http://www.gnu.org/licenses/>.
//

package v1

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"

	"github.com/gorilla/mux"
	"gitlab.servus.at/autoradio/tank/importer"
	"gitlab.servus.at/autoradio/tank/store"
)

func sendWebResponse(w http.ResponseWriter, status int, respdata interface{}) {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(status)
	json.NewEncoder(w).Encode(respdata) // TODO: Error Handling?
}

func notFound(w http.ResponseWriter, r *http.Request) {
	sendWebResponse(w, http.StatusNotFound, ErrorResponse{"not found"})
}

func methodNotAllowed(w http.ResponseWriter, r *http.Request) {
	sendWebResponse(w, http.StatusMethodNotAllowed, ErrorResponse{fmt.Sprintf("method %s is not allowed here", r.Method)})
}

type API struct {
	store    *store.Store
	importer *importer.SessionStore
	infoLog  *log.Logger
	errLog   *log.Logger
	dbgLog   *log.Logger
	listing  APIListing
}

func NewAPI(st *store.Store, im *importer.SessionStore, infoLog, errLog, dbgLog *log.Logger) (api *API) {
	if infoLog == nil {
		infoLog = log.New(ioutil.Discard, "", 0)
	}
	if errLog == nil {
		errLog = log.New(ioutil.Discard, "", 0)
	}
	if dbgLog == nil {
		dbgLog = log.New(ioutil.Discard, "", 0)
	}

	api = &API{}
	api.store = st
	api.importer = im
	api.infoLog = infoLog
	api.errLog = errLog
	api.dbgLog = dbgLog
	api.listing.Endpoints = make(map[string]*APIEndpoint)
	return
}

func (api *API) ListEndpoints() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		sendWebResponse(w, http.StatusOK, api.listing)
	})
}

func InstallHandler(r *mux.Router, st *store.Store, im *importer.SessionStore, infoLog, errLog, dbgLog *log.Logger) {
	r.NotFoundHandler = http.HandlerFunc(notFound)
	r.MethodNotAllowedHandler = http.HandlerFunc(methodNotAllowed)

	api := NewAPI(st, im, infoLog, errLog, dbgLog)

	// Groups
	r.Handle("/groups", api.ListGroups()).Methods("GET")
	r.HandleFunc("/groups", methodNotAllowed)

	// Imports
	r.Handle("/groups/{group-id}/imports", api.ListImportsOfGroup()).Methods("GET")
	r.Handle("/groups/{group-id}/imports", api.CreateImportForGroup()).Methods("POST")
	r.HandleFunc("/groups/{group-id}/imports", methodNotAllowed)

	r.Handle("/groups/{group-id}/imports/{import-id}", api.ReadImportOfGroup()).Methods("GET")
	r.Handle("/groups/{group-id}/imports/{import-id}", api.UploadFileToImportOfGroup()).Methods("PUT")
	r.Handle("/groups/{group-id}/imports/{import-id}", api.DeleteImportOfGroup()).Methods("DELETE")
	r.HandleFunc("/groups/{group-id}/imports/{import-id}", methodNotAllowed)

	// Files
	r.Handle("/groups/{group-id}/files", api.ListFilesOfGroup()).Methods("GET")
	r.HandleFunc("/groups/{group-id}/files", methodNotAllowed)

	r.Handle("/groups/{group-id}/files/{file-id}", api.ReadFileOfGroup()).Methods("GET")
	r.Handle("/groups/{group-id}/files/{file-id}", api.UpdateFileOfGroup()).Methods("PUT")
	r.Handle("/groups/{group-id}/files/{file-id}", api.DeleteFileOfGroup()).Methods("DELETE")
	r.HandleFunc("/groups/{group-id}/files/{file-id}", methodNotAllowed)

	// Playlists
	r.Handle("/groups/{group-id}/playlists", api.ListPlaylistsOfGroup()).Methods("GET")
	r.Handle("/groups/{group-id}/playlists", api.CreatePlaylistForGroup()).Methods("POST")
	r.HandleFunc("/groups/{group-id}/playlists", methodNotAllowed)

	r.Handle("/groups/{group-id}/playlists/{playlist-id}", api.ReadPlaylistOfGroup()).Methods("GET")
	r.Handle("/groups/{group-id}/playlists/{playlist-id}", api.UpdatePlaylistOfGroup()).Methods("PUT")
	r.Handle("/groups/{group-id}/playlists/{playlist-id}", api.DeletePlaylistOfGroup()).Methods("DELETE")
	r.HandleFunc("/groups/{group-id}/playlists/{playlist-id}", methodNotAllowed)

	// Index
	r.Handle("/", api.ListEndpoints()).Methods("GET")
	r.HandleFunc("/", methodNotAllowed)

	r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
		p, err := route.GetPathTemplate()
		if err != nil {
			return nil
		}

		m, err := route.GetMethods()
		if err != nil || len(m) == 0 {
			return err
		}

		if e, exists := api.listing.Endpoints[p]; exists {
			e.Methods = append(e.Methods, m...)
		} else {
			api.listing.Endpoints[p] = &APIEndpoint{m}
		}
		return nil
	})
}