package tools

import (
	"encoding/json"
	"flag"
	"fmt"
	"log"
	"os"
	"os/signal"
	"syscall"

	"github.com/bwmarrin/discordgo"
)

var (
	token      string
	dg         *discordgo.Session
	guild     	string
)

func init() {
	flag.StringVar(&token, "t", "", "Bot Token")
	flag.StringVar(&guild, "g", "", "Guild ID")
	flag.Parse()
}

func main() {
	if token == "" {
		fmt.Printf("No token provided. Please run: disgord-thanos -t <bot token>")
	}
	dg, _ = discordgo.New("Bot " + token)
	dg.AddHandler(messageCreate)
	_ = dg.Open()
	sc := make(chan os.Signal, 1)
	signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
	<-sc
	dg.Close()
}

func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
	if guild != "" {
		if m.GuildID != guild {
			return
		}
	}
	jsonMsg, err := json.Marshal(m)
	if err != nil {
		jsonMsg = append(jsonMsg, '0')
	}
	log.Printf("----------\n%+v: %+v\n\n%+v\n------------------------------\n\n", m.Author.Username, m.Content, string(jsonMsg))
}