//
//  tank
//
//  Import and Playlist Daemon for autoradio project
//
//
//  Copyright (C) 2017-2019 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 (
	"io/ioutil"
	"log"
	// "net/http"

	"github.com/gin-gonic/gin"
	"gitlab.servus.at/autoradio/tank/auth"
	"gitlab.servus.at/autoradio/tank/importer"
	"gitlab.servus.at/autoradio/tank/store"
)

type API struct {
	store    *store.Store
	importer *importer.Importer
	infoLog  *log.Logger
	errLog   *log.Logger
	dbgLog   *log.Logger
}

func NewAPI(st *store.Store, im *importer.Importer, 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
	return
}

func InstallHTTPHandler(r *gin.RouterGroup, st *store.Store, im *importer.Importer, infoLog, errLog, dbgLog *log.Logger) {
	r.Use(auth.Middleware())
	api := NewAPI(st, im, infoLog, errLog, dbgLog)

	// Shows
	r.GET("/shows", api.ListShows())
	r.POST("/shows/:show-id", api.CreateShow())

	// Files
	r.GET("/shows/:show-id/files", api.ListFilesOfShow())
	r.POST("/shows/:show-id/files", api.CreateFileForShow())

	r.GET("/shows/:show-id/files/:file-id", api.ReadFileOfShow())
	r.PATCH("/shows/:show-id/files/:file-id", api.PatchFileOfShow())
	r.DELETE("/shows/:show-id/files/:file-id", api.DeleteFileOfShow())

	r.GET("/shows/:show-id/files/:file-id/usage", api.ReadUsageOfFile())

	r.GET("/shows/:show-id/files/:file-id/import", api.ReadImportOfFile())
	r.DELETE("/shows/:show-id/files/:file-id/import", api.CancelImportOfFile())

	// // TODO: distignuish between flow.js and simple upload using the content type?!?
	r.PUT("/shows/:show-id/files/:file-id/upload", api.UploadFileSimple())
	r.POST("/shows/:show-id/files/:file-id/upload", api.UploadFileFlowJS())
	r.GET("/shows/:show-id/files/:file-id/upload", api.TestFileFlowJS())

	// Imports
	r.GET("/shows/:show-id/imports", api.ListImportsOfShow())

	// // Playlists
	// playlistsHandler := make(handlers.MethodHandler)
	// playlistsHandler[http.MethodGet] = api.ListPlaylistsOfShow()
	// playlistsHandler[http.MethodPost] = api.CreatePlaylistForShow()
	// r.Handle("/shows/{show-id}/playlists", playlistsHandler)

	// playlistHandler := make(handlers.MethodHandler)
	// playlistHandler[http.MethodGet] = api.ReadPlaylistOfShow()
	// playlistHandler[http.MethodPut] = api.UpdatePlaylistOfShow()
	// playlistHandler[http.MethodDelete] = api.DeletePlaylistOfShow()
	// r.Handle("/shows/{show-id}/playlists/{playlist-id}", playlistHandler)

	// r.Handle("/", indexHandler)
}