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

	"github.com/gorilla/mux"
)

func (api *API) ListGroups() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		groups, err := api.store.ListGroups()
		if err != nil {
			sendWebResponse(w, http.StatusInternalServerError, ErrorResponse{Error: err.Error()})
		}
		// TODO: add all groups that are not present in the store but are accessable to the user
		//       accroding to the auth info we got.
		sendWebResponse(w, http.StatusOK, GroupsListing{groups})
	})
}

func (api *API) CreateGroup() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// TODO: implement clone depending on query parameter
		vars := mux.Vars(r)
		group, err := api.store.CreateGroup(vars["group-id"])
		if err != nil {
			sendStoreError(w, err)
			return
		}
		sendWebResponse(w, http.StatusCreated, group)
	})
}