package main

import (
	"fmt"
	"time"

	"github.com/bwmarrin/discordgo"
)

func ready(s *discordgo.Session, event *discordgo.Ready) {
	// Set the playing status.
	s.UpdateGameStatus(0, fmt.Sprintf("DreamDaddy v%+v %+v", version, gitCommit))
}

func guildMemberUpdate(s *discordgo.Session, m *discordgo.GuildMemberUpdate) {
	defer log.PanicSafe()
	for role := range m.Roles {
		if fmt.Sprintf("%+v", role) == config.MonitorRole {
			s.ChannelMessageSend(config.AdminChannel, "New unverified user detected.")
			s.ChannelMessageSend(config.MonitorChann, fmt.Sprintf("Welcome %+v, you may PM me your verification, or I will ban you in an hour!\nSay \"!rules\" in this channel, without quotes for the rules. You may private/direct message me for verification instructions.\n\nYou will not be able to read/see other channels or users until you verify.", m.User.Mention()))
			config.Unverified[m.User.ID] = time.Now()
			config.Probations[m.User.ID] = time.Now()
			saveConfig()
		}
	}

}

func guildMemberAdd(s *discordgo.Session, m *discordgo.GuildMemberAdd) {
	defer log.PanicSafe()
	config.Unverified[m.User.ID] = time.Now()
	config.Probations[m.User.ID] = time.Now()
	s.GuildMemberRoleAdd(config.GuildID, m.User.ID, config.MonitorRole)
	s.ChannelMessageSend(config.MonitorChann, fmt.Sprintf("Welcome %+v, you may PM me your verification, or I will ban you in an hour!\nSay \"!rules\" in this channel, without quotes for the rules. You may private/direct message me for verification instructions.\n\nYou will not be able to read/see other channels or users until you verify.", m.User.Mention()))
	saveConfig()
}

func guildMemberBanned(s *discordgo.Session, m *discordgo.GuildBanAdd) {
	defer log.PanicSafe()
	for uid := range config.Probations {
		if m.User.Email == uid {
			delete(config.Probations, uid)
			s.ChannelMessageDelete(config.IntroChann, introMsg[uid])
		}
	}
	saveConfig()
}

func guildMemberRemove(s *discordgo.Session, m *discordgo.GuildMemberRemove) {
	defer log.PanicSafe()
	go runPurge(s)
	banned := false
	for uid, join := range config.Probations {
		if time.Since(join) < 2*time.Hour {
			if m.User.ID == uid {
				banned = true
				s.GuildBanCreateWithReason(config.GuildID, m.User.ID, fmt.Sprintf("Left within 2 hours of joining. %+v", time.Since(join)), 0)
				delete(config.Probations, uid)
				s.ChannelMessageDelete(config.IntroChann, introMsg[uid])
			}
		} else {
			delete(config.Probations, uid)
			s.ChannelMessageDelete(config.IntroChann, introMsg[uid])
		}
	}
	s.ChannelMessageSend(config.AdminChannel, fmt.Sprintf("%+v (@%+v) has left, ban: %+v", m.User.ID, m.User.Username, banned))
	delete(config.Unverified, m.User.ID)
	for msg, v := range config.Verifications {
		if v.UserID == m.User.ID {
			delete(config.Verifications, msg)
			s.ChannelMessageDelete(config.IntroChann, introMsg[m.User.ID])
		}
	}
	saveConfig()

}

func readReaction(s *discordgo.Session, m *discordgo.MessageReactionAdd) {
	defer log.PanicSafe()
	if m.ChannelID != config.AdminChannel || m.UserID == s.State.User.ID {
		return
	}
	admin, _ := s.GuildMember(config.GuildID, m.UserID)
	adminInteraction(s, admin.User.ID)
	verification, ok := config.Verifications[m.MessageID]
	if !ok {
		return
	}
	verification.Admin = admin.User.Username
	verification.Closed = time.Now()
	user := userFromID(verification.UserID)
	if user.ID == "" {
		s.ChannelMessageSend(config.AdminChannel, fmt.Sprintf("%+v, that user was not found, they might have left.", admin.Mention()))
		delete(config.Verifications, m.MessageID)
		return
	}
	if m.Emoji.Name == "👎" {
		rejectVerification(s, user)
		verification.Status = "Rejected"
	} else if m.Emoji.Name == "👍" {
		verifyMember(s, user)
		verification.Status = "Accepted"
		go storeVerification(verification)
	} else if m.Emoji.Name == "👶" {
		requestAge(s, user)
		log.LogInfo("%+v has requested ASL for user %+v.", admin.User.Username, user.Username)
		return
	} else if m.Emoji.Name == "⛔" {
		s.GuildBanCreateWithReason(config.GuildID, user.ID, fmt.Sprintf("Underage or too many failed verifications. %+v", admin.User.Username), 5)
		verification.Status = "Banned"
	} else {
		return
	}
	log.LogInfo("%+v", verification.prettyPrint())
	delete(config.Verifications, m.MessageID)
}