package handlers

import (
	"fmt"
	"github.com/bwmarrin/discordgo"
	"kleincordBot/services"
	"log"
	"os"
	"time"
)

var LogChannelID string

func ReadBackCommand(data discordgo.ApplicationCommandInteractionData, s *discordgo.Session, i *discordgo.InteractionCreate) {
	err := s.InteractionRespond(
		i.Interaction,
		&discordgo.InteractionResponse{
			Type: discordgo.InteractionResponseChannelMessageWithSource,
			Data: &discordgo.InteractionResponseData{
				Content: data.Options[0].Value.(string),
			},
		},
	)
	if err != nil {
		services.HandleError(err, s)
	}
}

func Test1Command(s *discordgo.Session, i *discordgo.InteractionCreate) {
	err := s.InteractionRespond(
		i.Interaction,
		&discordgo.InteractionResponse{
			Type: discordgo.InteractionResponseChannelMessageWithSource,
			Data: &discordgo.InteractionResponseData{},
		},
	)
	if err != nil {
		services.HandleError(err, s)
	}
}

func StopCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
	s.InteractionRespond(
		i.Interaction,
		&discordgo.InteractionResponse{
			Type: discordgo.InteractionResponseChannelMessageWithSource,
			Data: &discordgo.InteractionResponseData{
				Content: "bye",
			},
		},
	)
	s.ChannelMessageSendEmbed(
		LogChannelID,
		&discordgo.MessageEmbed{
			Title:     "stopped " + os.Getenv("ENVIRONMENT") + " with command",
			Timestamp: time.Now().Format(time.RFC3339),
		})

	log.Fatalf("stop command")
}

func InitCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
	var serverListString string
	for i, server := range services.ServerList {
		serverListString = serverListString + fmt.Sprintf("\n %d) ", i+1) + server.Name
	}
	err := s.InteractionRespond(
		i.Interaction,
		&discordgo.InteractionResponse{
			Type: discordgo.InteractionResponseChannelMessageWithSource,
			Data: &discordgo.InteractionResponseData{
				Embeds: []*discordgo.MessageEmbed{
					{
						Title: "Servers",
						Fields: []*discordgo.MessageEmbedField{
							{
								Value: serverListString,
							},
						},
						Type: discordgo.EmbedTypeArticle,
					},
				},
			},
		},
	)
	if err != nil {
		services.HandleError(err, s)
	}
}
