package notify

import (
	tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
	"log"
	"regexp"
	"strconv"
	"strings"
	"time"
	"watn3y/bloaterbotv3/botIO"
)

func Reminder(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
	commandArgs := strings.Fields(update.Message.CommandArguments())

	var timeArg string
	var textArg string

	if len(commandArgs) >= 1 {
		timeArg = strings.ToLower(commandArgs[0])
		textArg = strings.Replace(update.Message.CommandArguments(), timeArg, "", 1)
	} else {
		message := tgbotapi.MessageConfig{
			BaseChat:              tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
			ParseMode:             "html",
			DisableWebPagePreview: false,
			Text:                  "Please specify a valid notify time in this format: 1m, 6h, 2d",
		}
		botIO.SendMessage(message, bot)
		return
	}

	log.Println("[notify] Attempting to set reminder for user " + strconv.FormatInt(update.Message.From.ID, 10) + " in chat " + strconv.FormatInt(update.Message.Chat.ID, 10) + " with arguments " + timeArg)

	isValidFormat := regexp.MustCompile(`(?m)^\d{1,3}[mhd]$`)

	if !isValidFormat.MatchString(timeArg) {
		message := tgbotapi.MessageConfig{
			BaseChat:              tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
			ParseMode:             "html",
			DisableWebPagePreview: false,
			Text:                  "Please specify a valid notify time in this format: 1m, 6h, 2d",
		}
		botIO.SendMessage(message, bot)
		return
	}

	getChar := regexp.MustCompile(`(?m)[mhd]`)
	getNumbers := regexp.MustCompile(`(?m)\d{1,3}`)

	numberString := getNumbers.FindString(timeArg)
	number, _ := strconv.Atoi(numberString)
	char := getChar.FindString(timeArg)

	var modifyTime time.Duration
	switch char {
	case "m":
		modifyTime = time.Minute
	case "h":
		modifyTime = time.Hour
	case "d":
		modifyTime = time.Hour * 24
	}

	notifyTime := time.Now().UTC().Add(time.Duration(number) * modifyTime)

	var reminder reminderConfig

	reminder = reminderConfig{
		updateID:           update.UpdateID,
		notifyTime:         notifyTime.Unix(),
		chatID:             update.Message.Chat.ID,
		userID:             update.Message.From.ID,
		messageToReplyToID: update.Message.MessageID,
		reminderText:       textArg,
	}

	sqlAddReminder(reminder)

	message := tgbotapi.MessageConfig{
		BaseChat:              tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
		ParseMode:             "html",
		DisableWebPagePreview: false,
		Text:                  "Set reminder for " + notifyTime.Format("02.01.2006") + " at " + notifyTime.Format("15:04") + " UTC ",
	}
	botIO.SendMessage(message, bot)
	return

}

func NotifyHandler(bot *tgbotapi.BotAPI) {

	for {
		reminders := sqlGetReminders()

		for updateID, reminderTime := range reminders {
			if reminderTime <= time.Now().UTC().Unix() {
				details := sqlGetReminderDetails(updateID)

				message := tgbotapi.MessageConfig{
					BaseChat:              tgbotapi.BaseChat{ChatID: details.chatID, ReplyToMessageID: details.messageToReplyToID},
					ParseMode:             "html",
					DisableWebPagePreview: false,

					Text: details.reminderText,
				}

				botIO.SendMessage(message, bot)
				sqlDeleteReminder(updateID)
			}

		}
		time.Sleep(time.Second * 10)
	}

}