package main

import (
	"fmt"
	"strings"
	"time"

	"github.com/bwmarrin/discordgo"
)

func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
	defer log.PanicSafe()
	if m.Author.ID == s.State.User.ID || m.Author.Bot {
		return
	}
	if m.GuildID == "" {
		handlePM(s, m)
		return
	}
	if m.ChannelID == config.MonitorChann {
		if strings.Contains(m.Content, "erif") && !m.Author.Bot {
			s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%+v send me a private message for verification.", m.Author.Mention()))
		}
		return
	}
	if isAdmin(m.Member) {
		adminInteraction(s, m.Author.ID)
	}
	if m.ChannelID != config.AdminChannel {
		lastActiveChan = m.ChannelID
		lastActiveTime = time.Now()
		if len(m.Attachments) > 0 {
			activeInteraction(s, m.Author.ID)
		}
	}
	if strings.HasPrefix(m.Content, "!d bump") {
		if time.Since(config.BumpTime) < 2*time.Hour {
			s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Sorry, <@%+v> already claimed the bump. Better luck next time!", config.LastBumper))
			return
		}
		activeInteraction(s, m.Author.ID)
		config.LastBumper = m.Author.ID
		go bumpTimer(s)
		return
	}
	if strings.Contains(m.Content, s.State.User.ID) {
		b := BotCommand{
			Session: s,
			Message: m,
			Parts:   strings.Split(m.Content, " ")[2:],
		}
		log.LogDebug("%+v", b.Parts)
		for _, cmd := range commands {
			for _, keyword := range cmd.Keywords {
				log.LogDebug("Checking if %+v contains %+v", m.Content, keyword)
				if strings.Contains(m.Content, keyword) {
					log.LogDebug("%+v found!", keyword)
					b.Command = keyword
					if !cmd.RequiresAdmin {
						log.LogDebug("%+v does not require admin, running!", cmd.Name)
						if !cmd.Exec(b) {
							s.ChannelMessageSend(config.AdminChannel, fmt.Sprintf("There was an error running %+v\n%+v", cmd.Name, cmd.Help))
						} else {
							log.LogInfo("Ran command %+v for %+v", cmd.Name, m.Author.Username)
						}
					} else {
						log.LogDebug("%+v does require admin, checking!", cmd.Name)
						if isAdmin(m.Member) {
							if !cmd.Exec(b) {
								s.ChannelMessageSend(config.AdminChannel, fmt.Sprintf("There was an error running %+v\n%+v", cmd.Name, cmd.Help))
							} else {
								log.LogInfo("Ran command %+v for %+v", cmd.Name, m.Author.Username)
							}
						} else {
							log.LogInfo("%+v tried to run an admin command (%+v) but isn't an admin.", m.Author.Username, keyword)
						}
					}
				}
			}
		}
	} else {
		log.LogDebug("strings.Contains(m.Content, s.State.User.ID) was false for %+v", m.Content)
		if bump {
			s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%+v please say \"!d bump\" without the quotes to bump our server :)", m.Author.Mention()))
		}
	}

}

func handlePM(s *discordgo.Session, m *discordgo.MessageCreate) {
	defer log.PanicSafe()
	if strings.Contains(m.Content, "Rule") || strings.Contains(m.Content, "rule") {
		s.ChannelMessageSend(m.ChannelID, "I specifically said to say \"!rules\" without quotes in the unverified channel for the rules.")
	}
	for _, uid := range config.Verifications {
		user := userFromID(uid.UserID)
		if m.Author.ID == user.ID {
			s.ChannelMessageSend(m.ChannelID, "Your verification is pending. An admin will respond to it when they are available.")
			s.ChannelMessageSend(config.AdminChannel, fmt.Sprintf("%+v said: %+v", m.Author.Mention(), m.Content))
			return
		}
	}
	if len(m.Attachments) != 1 {
		s.ChannelMessageSend(m.ChannelID, "```I am a bot and this is an autoreply.\n\nUntil you send a verification, I will always say the following message:```\nYou may only send me your verification (and nothing else) to be passed to the admins (and no one else). Verification is a clear full face pic, with your pinky finger held to the corner of your mouth.")
		s.ChannelMessageSend(config.AdminChannel, fmt.Sprintf("%+v said: %+v", m.Author.Mention(), m.Content))
		return
	}
	if strings.HasSuffix(strings.ToUpper(m.Attachments[0].ProxyURL), "HEIC") {
		s.ChannelMessageSend(m.ChannelID, "You have tried to send an unsupported file (HEIC). Please try again using an image (jpeg, jpg, png, etc).")
		return
	}
	delete(config.Unverified, m.Author.ID)
	var v Verification
	v.Submitted = time.Now()
	v.UserID = m.Author.ID
	v.Username = m.Author.Username
	v.Photo = m.Attachments[0].ProxyURL
	v.Status = "Submitted"
	msg, _ := s.ChannelMessageSend(config.AdminChannel, fmt.Sprintf("%+v\n%+v", v.Username, v.Photo))
	config.Verifications[msg.ID] = v
	s.MessageReactionAdd(config.AdminChannel, msg.ID, "👎")
	s.MessageReactionAdd(config.AdminChannel, msg.ID, "👍")
	s.MessageReactionAdd(config.AdminChannel, msg.ID, "👶")
	s.MessageReactionAdd(config.AdminChannel, msg.ID, "⛔")
}