//
//  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 (
	"context"
	"encoding/json"
	"net/http"
	"time"

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

func (api *API) ListFilesOfShow(c *gin.Context) {
	showID := c.Param("show-id")
	if authorized, _ := authorizeRequest(c, showID); !authorized {
		return
	}

	// TODO: implement pagination
	files, err := api.store.ListFiles(showID)
	if err != nil {
		sendError(c, err)
		return
	}
	c.JSON(http.StatusOK, FilesListing{files})
}

func (api *API) CreateFileForShow(c *gin.Context) {
	showID := c.Param("show-id")
	authorized, sess := authorizeRequest(c, showID)
	if !authorized {
		return
	}

	request := &FileCreateRequest{}
	err := json.NewDecoder(c.Request.Body).Decode(request)
	if err != nil {
		c.JSON(http.StatusBadRequest, ErrorResponse{Error: "error decoding request: " + err.Error()})
		return
	}
	if request.SourceURI == "" {
		c.JSON(http.StatusBadRequest, ErrorResponse{Error: "source-uri is mandatory"})
		return
	}
	srcURI, err := api.importer.ParseAndVerifySourceURI(request.SourceURI)
	if err != nil {
		c.JSON(http.StatusBadRequest, ErrorResponse{Error: "source-uri is invalid: " + err.Error()})
		return
	}
	waitFor := c.Query("wait-for")
	switch waitFor {
	case "running":
	case "done":
	case "":
	default:
		c.JSON(http.StatusBadRequest, ErrorResponse{Error: "unable to wait for unknown state: " + waitFor})
		return
	}

	file := &store.File{}
	file.Source.URI = request.SourceURI
	if file, err = api.store.CreateFile(showID, *file); err != nil {
		sendError(c, err)
		return
	}

	// From here on, in case of an error we should either delete the newly created
	// file or at least return the id of it so the API user can deal with the problem.
	status := http.StatusCreated
	refID := "" // TODO: get this from query paremeter
	job, err := api.importer.CreateJob(showID, file.ID, srcURI, sess.Username, refID)
	if err != nil {
		status, _ = statusCodeFromError(err)
		goto create_file_response
	}

	if err = job.Start(context.Background(), 3*time.Hour); err != nil { // TODO: hardcoded value
		status, _ = statusCodeFromError(err)
		goto create_file_response
	}

	switch waitFor {
	case "running":
		<-job.Running()
	case "done":
		<-job.Done()
	}

create_file_response:
	if file, err = api.store.GetFile(showID, file.ID); err != nil {
		// GetFile only fails if the file does not exist or there is
		// a problem with the database connection. The former probably
		// means that the file got deleted by somebody else... all other
		// errors most likely mean that the file still exists. In this
		// case it's better to return the file so the API user can use
		// the file ID for further inspection.
		if err == store.ErrNotFound {
			sendError(c, err)
			return
		}
		status, _ = statusCodeFromError(err)
	}
	c.JSON(status, file)
}

func (api *API) ReadFileOfShow(c *gin.Context) {
	showID := c.Param("show-id")
	if authorized, _ := authorizeRequest(c, showID); !authorized {
		return
	}

	id, err := idFromString(c.Param("file-id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, ErrorResponse{Error: "invalid file-id: " + err.Error()})
		return
	}
	file, err := api.store.GetFile(showID, id)
	if err != nil {
		sendError(c, err)
		return
	}
	c.JSON(http.StatusOK, file)
}

func (api *API) PatchFileOfShow(c *gin.Context) {
	showID := c.Param("show-id")
	if authorized, _ := authorizeRequest(c, showID); !authorized {
		return
	}

	id, err := idFromString(c.Param("file-id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, ErrorResponse{Error: "invalid file-id: " + err.Error()})
		return
	}
	data := make(map[string]string)
	if err = json.NewDecoder(c.Request.Body).Decode(&data); err != nil {
		c.JSON(http.StatusBadRequest, ErrorResponse{Error: "error decoding request: " + err.Error()})
		return
	}
	file, err := api.store.UpdateFileMetadata(showID, id, data)
	if err != nil {
		sendError(c, err)
		return
	}
	c.JSON(http.StatusOK, file)
}

func (api *API) DeleteFileOfShow(c *gin.Context) {
	showID := c.Param("show-id")
	if authorized, _ := authorizeRequest(c, showID); !authorized {
		return
	}

	id, err := idFromString(c.Param("file-id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, ErrorResponse{Error: "invalid file-id: " + err.Error()})
		return
	}
	if job, err := api.importer.GetJob(showID, id); err != importer.ErrNotFound {
		job.Cancel()
	}
	if err = api.store.DeleteFile(showID, id); err != nil {
		sendError(c, err)
		return
	}
	c.JSON(http.StatusNoContent, nil)
}

func (api *API) ReadUsageOfFile(c *gin.Context) {
	showID := c.Param("show-id")
	if authorized, _ := authorizeRequest(c, showID); !authorized {
		return
	}

	id, err := idFromString(c.Param("file-id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, ErrorResponse{Error: "invalid file-id: " + err.Error()})
		return
	}
	result := FileUsageListing{}
	if result.Usage.Playlists, err = api.store.GetFileUsage(showID, id); err != nil {
		sendError(c, err)
		return
	}
	c.JSON(http.StatusOK, result)
}