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

	"github.com/gorilla/mux"
)

func (api *API) ListFilesOfGroup() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		// TODO: implement pagination
		files, err := api.store.ListFiles(vars["group-id"])
		if err != nil {
			sendStoreError(w, err)
			return
		}
		sendWebResponse(w, http.StatusOK, FilesListing{files})
	})
}

func (api *API) ReadFileOfGroup() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		id, err := idFromString(vars["file-id"])
		if err != nil {
			sendWebResponse(w, http.StatusBadRequest, ErrorResponse{"invalid file-id: " + err.Error()})
			return
		}
		file, err := api.store.GetFile(vars["group-id"], id)
		if err != nil {
			sendStoreError(w, err)
			return
		}
		sendWebResponse(w, http.StatusOK, file)
	})
}

func (api *API) PatchFileOfGroup() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		id, err := idFromString(vars["file-id"])
		if err != nil {
			sendWebResponse(w, http.StatusBadRequest, ErrorResponse{"invalid file-id: " + err.Error()})
			return
		}
		// would be nice to have this and the Updatefile inside the same transaction...
		// but for this we would have to have the json.Decode inside the store to get the nice
		// update feature where elements which are not in the json document don't get changed
		orig, err := api.store.GetFile(vars["group-id"], id)
		if err != nil {
			sendStoreError(w, err)
			return
		}
		changed := orig
		if err = json.NewDecoder(r.Body).Decode(&changed); err != nil {
			sendWebResponse(w, http.StatusBadRequest, ErrorResponse{"error decoding file: " + err.Error()})
			return
		}
		orig.Metadata = changed.Metadata // for now only metadata can be updated
		// TODO: fis this once the store has been revamped
		// orig.Changed("unknown")          // TODO: take user from auth-session
		if err = api.store.UpdateFile(vars["group-id"], id, orig); err != nil {
			sendStoreError(w, err)
			return
		}
		sendWebResponse(w, http.StatusNoContent, nil)
	})
}

func (api *API) DeleteFileOfGroup() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		id, err := idFromString(vars["file-id"])
		if err != nil {
			sendWebResponse(w, http.StatusBadRequest, ErrorResponse{"invalid file-id: " + err.Error()})
			return
		}
		if err = api.store.DeleteFile(vars["group-id"], id); err != nil {
			sendStoreError(w, err)
			return
		}
		sendWebResponse(w, http.StatusNoContent, nil)
	})
}