package server

import (
	d "playback-device-server/data"
	m "playback-device-server/management"

	"github.com/labstack/echo/v4"
	"github.com/rs/zerolog/log"
)

type RemoteApiHandler struct {
	router        *echo.Echo
	remoteManager *m.RemoteManager
}

func (r *RemoteApiHandler) Initialize(authenticator *Authenticator) {
	r.router.Use(authenticator.Authenticate("/api/remotes", []string{}))
	remotesApi := r.router.Group("/api/remotes")
	remotesApi.GET("", r.handleGetRemotes)
	remotesApi.GET("/:id", r.handleGetRemote)
	remotesApi.POST("", r.handleCreateRemote)
	remotesApi.DELETE("/:id", r.handleDelteRemote)

	r.router.Use(authenticator.Authenticate("/api/commands", []string{}))
	commandsApi := r.router.Group("/api/commands")
	commandsApi.GET("", r.handleGetCommands)
	commandsApi.POST("", r.handleCreateCommands)
	commandsApi.DELETE("/:id", r.handleDeleteCommand)

}

func (r *RemoteApiHandler) handleCreateRemote(context echo.Context) error {
	remote := d.Remote{}
	if err := context.Bind(&remote); err != nil {
		SendError(400, context, err.Error())
		return nil
	}
	id, err := r.remoteManager.CreateRemote(remote)
	if err != nil {
		SendError(500, context, err.Error())
		return err
	}
	remote.Id = id
	return context.JSON(200, remote)
}

func (r *RemoteApiHandler) handleGetRemote(context echo.Context) error {
	id := context.Param("id")
	remote, err := r.remoteManager.GetRemote(id)
	if err != nil {
		SendError(500, context, err.Error())
		log.Error().Err(err)
		return err
	}
	return context.JSON(200, remote)
}

func (r *RemoteApiHandler) handleGetRemotes(context echo.Context) error {
	remotes, err := r.remoteManager.GetRemotes()
	if err != nil {
		SendError(500, context, err.Error())
		return err
	}
	return context.JSON(200, remotes)
}

func (r *RemoteApiHandler) handleDelteRemote(context echo.Context) error {
	id := context.Param("id")
	err := r.remoteManager.DeleteRemote(id)
	if err != nil {
		SendError(500, context, err.Error())
		return err
	}
	return context.JSON(200, "")
}

func (r *RemoteApiHandler) handleCreateCommands(context echo.Context) error {
	commands := []d.Command{}
	if err := context.Bind(&commands); err != nil {
		SendError(400, context, err.Error())
		return nil
	}
	err := r.remoteManager.CreateCommands(commands)
	if err != nil {
		SendError(500, context, err.Error())
		return err
	}
	return context.JSON(200, "")
}

func (r *RemoteApiHandler) handleGetCommands(context echo.Context) error {
	commands, err := r.remoteManager.GetCommands()
	if err != nil {
		SendError(500, context, err.Error())
		return err
	}
	return context.JSON(200, commands)
}

func (r *RemoteApiHandler) handleDeleteCommand(context echo.Context) error {
	id := context.Param("id")
	err := r.remoteManager.DeleteCommand(id)
	if err != nil {
		SendError(500, context, err.Error())
		return err
	}
	return context.JSON(200, "")
}

func (r *RemoteApiHandler) SetRouter(router *echo.Echo) {
	r.router = router
}

func (r *RemoteApiHandler) SetRemoteManager(remoteManager *m.RemoteManager) {
	r.remoteManager = remoteManager
}
