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

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

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

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

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

	playlist := &store.Playlist{}
	err := json.NewDecoder(c.Request.Body).Decode(playlist)
	if err != nil {
		c.JSON(http.StatusBadRequest, ErrorResponse{Error: "error decoding playlist: " + err.Error()})
		return
	}
	if playlist, err = api.store.CreatePlaylist(showID, *playlist); err != nil {
		sendError(c, err)
		return
	}
	c.JSON(http.StatusCreated, playlist)
}

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

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

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

	id, err := idFromString(c.Param("playlist-id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, ErrorResponse{Error: "invalid playlist-id: " + err.Error()})
		return
	}
	playlist := &store.Playlist{}
	if err = json.NewDecoder(c.Request.Body).Decode(playlist); err != nil {
		c.JSON(http.StatusBadRequest, ErrorResponse{Error: "error decoding playlist: " + err.Error()})
		return
	}
	if playlist, err = api.store.UpdatePlaylist(showID, id, *playlist); err != nil {
		sendError(c, err)
		return
	}
	c.JSON(http.StatusOK, playlist)
}

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

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