//
//  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"
	"net/url"
	"time"

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

func (api *API) ListFilesOfShow() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		showID := vars["show-id"]
		if authorized, _ := authorizeRequestOld(w, r, showID); !authorized {
			return
		}

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

func (api *API) CreateFileForShow() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		showID := vars["show-id"]
		authorized, sess := authorizeRequestOld(w, r, showID)
		if !authorized {
			return
		}

		request := &FileCreateRequest{}
		err := json.NewDecoder(r.Body).Decode(request)
		if err != nil {
			sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "error decoding request: " + err.Error()})
			return
		}
		if request.SourceURI == "" {
			sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "source-uri is mandatory"})
			return
		}
		srcURI, err := url.Parse(request.SourceURI)
		if err != nil {
			sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "source-uri is invalid: " + err.Error()})
			return
		}
		q := r.URL.Query()
		waitFor := q.Get("wait-for")
		switch waitFor {
		case "running":
		case "done":
		case "":
		default:
			sendWebResponse(w, 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 {
			sendErrorOld(w, err)
			return
		}
		// TODO: from here on, in case of an error we should either delete the newly created
		//       file or at least return the id of the created file so the UI can deal with it.

		refID := "" // TODO: get this from query paremeter
		job, err := api.importer.CreateJob(showID, file.ID, *srcURI, sess.Username, refID)
		if err != nil {
			// possible errors:
			//   - importer.ErrSourceNotSupported -> delete file
			//   - any error returned by store.GetFile():
			//     possible reasons for this:
			//       * connection problem to the store database
			//       * somebody deleted the file
			//   - importer.ErrFileNotNew -> leave file
			sendErrorOld(w, err)
			return
		}
		if err = job.Start(context.Background(), 3*time.Hour); err != nil { // TODO: hardcoded value
			// possible errors:
			//   - importer.ErrAlreadyCanceled -> ??
			//   - error while creating job workdir -> ??
			//   - importer.ErrTooManyJobs -> ??
			sendErrorOld(w, err)
			return
		}
		if waitFor == "" {
			sendWebResponse(w, http.StatusCreated, file)
			return
		}

		switch waitFor {
		case "running":
			<-job.Running()
		case "done":
			<-job.Done()
		}
		if file, err = api.store.GetFile(showID, file.ID); err != nil {
			// possible reasons why this failed:
			//   - connection problem to the store database
			//   - somebody deleted the file
			sendErrorOld(w, err)
			return
		}
		sendWebResponse(w, http.StatusCreated, file)
	})
}

func (api *API) ReadFileOfShow() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		showID := vars["show-id"]
		if authorized, _ := authorizeRequestOld(w, r, showID); !authorized {
			return
		}

		id, err := idFromString(vars["file-id"])
		if err != nil {
			sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "invalid file-id: " + err.Error()})
			return
		}
		file, err := api.store.GetFile(showID, id)
		if err != nil {
			sendErrorOld(w, err)
			return
		}
		sendWebResponse(w, http.StatusOK, file)
	})
}

func (api *API) PatchFileOfShow() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		showID := vars["show-id"]
		if authorized, _ := authorizeRequestOld(w, r, showID); !authorized {
			return
		}

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

func (api *API) DeleteFileOfShow() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		showID := vars["show-id"]
		if authorized, _ := authorizeRequestOld(w, r, showID); !authorized {
			return
		}

		id, err := idFromString(vars["file-id"])
		if err != nil {
			sendWebResponse(w, 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 {
			sendErrorOld(w, err)
			return
		}
		sendWebResponse(w, http.StatusNoContent, nil)
	})
}

func (api *API) ReadUsageOfFile() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		showID := vars["show-id"]
		if authorized, _ := authorizeRequestOld(w, r, showID); !authorized {
			return
		}

		id, err := idFromString(vars["file-id"])
		if err != nil {
			sendWebResponse(w, http.StatusBadRequest, ErrorResponse{Error: "invalid file-id: " + err.Error()})
			return
		}
		result := FileUsageListing{}
		if result.Usage.Playlists, err = api.store.GetFileUsage(showID, id); err != nil {
			sendErrorOld(w, err)
			return
		}
		sendWebResponse(w, http.StatusOK, result)
	})
}