package tg

import (
	"fmt"

	tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
	"stud.l9labs.ru/bot/modules/database"
)

var bell = map[bool]string{true: "üîî", false: "üîï"}
var milBell = map[bool]string{true: "ü´° –ï—Å—Ç—å –≤–æ–µ–Ω–∫–∞", false: "üèñ –ù–µ—Ç –≤–æ–µ–Ω–∫–∏"}
var optStr = "–ù–∞—Å—Ç—Ä–æ–π–∫–∏ —É–≤–µ–¥–æ–º–ª–µ–Ω–∏–π\n–ù–∞–∂–º–∏ –Ω–∞ –∫–Ω–æ–ø–∫—É, —á—Ç–æ–±—ã –ø–µ—Ä–µ–∫–ª—é—á–∏—Ç—å –ø–∞—Ä–∞–º–µ—Ç—Ä"

func (bot *Bot) GetOptions(user *database.TgUser) (tgbotapi.Message, error) {
	options := database.ShedulesInUser{
		L9Id: user.L9Id,
	}
	if _, err := bot.DB.Get(&options); err != nil {
		return nilMsg, err
	}
	// –ï—Å–ª–∏ –∫—Ç–æ-—Ç–æ —Ö–∏—Ç—Ä–æ–∂–æ–ø—ã–π –Ω–∞–∂–∞–ª –Ω–∞ –∫–Ω–æ–ø–∫—É –±–µ–∑ –ø–æ–¥–∫–ª—é—á–µ–Ω–Ω–æ–π –≥—Ä—É–ø–ø—ã
	if options.UID == 0 {
		return bot.SendMsg(
			user,
			"–£ —Ç–µ–±—è –ø–æ–∫–∞ –Ω–µ –ø–æ–¥–∫–ª—é—á–µ–Ω–æ –Ω–∏ –æ–¥–Ω–æ–π –≥—Ä—É–ø–ø—ã\n\n"+
				"–í–≤–µ–¥–∏ <b>–Ω–æ–º–µ—Ä –≥—Ä—É–ø–ø—ã</b> "+
				"(–≤ —Ñ–æ—Ä–º–∞—Ç–µ 2305 –∏–ª–∏ 2305-240502D), "+
				"–∏ –≤ –ø–æ—è–≤–∏–≤—à–µ–º—Å—è —Ä–∞—Å–ø–∏—Å–∞–Ω–∏–∏ –Ω–∞–∂–º–∏ <b>üîî –ü–æ–¥–∫–ª—é—á–∏—Ç—å —É–≤–µ–¥–æ–º–ª–µ–Ω–∏—è</b>\n\n"+
				"https://youtube.com/shorts/FHE2YAGYBa8",
			nil,
		)
	}

	return bot.SendMsg(user, optStr, OptMarkup(options))
}

func OptMarkup(options database.ShedulesInUser) tgbotapi.InlineKeyboardMarkup {
	markup := [][]tgbotapi.InlineKeyboardButton{
		{tgbotapi.NewInlineKeyboardButtonData(fmt.Sprintf("%s –ù–∞—á–∞–ª–æ –∑–∞–Ω—è—Ç–∏–π", bell[options.First]), "opt_first")},
		{tgbotapi.NewInlineKeyboardButtonData(fmt.Sprintf("%s –°–ª–µ–¥—É—é—â–∞—è –ø–∞—Ä–∞", bell[options.NextNote]), "opt_lesson")},
		{tgbotapi.NewInlineKeyboardButtonData(fmt.Sprintf("%s –°–ª–µ–¥—É—é—â–∏–π –¥–µ–Ω—å", bell[options.NextDay]), "opt_day")},
		{tgbotapi.NewInlineKeyboardButtonData(fmt.Sprintf("%s –°–ª–µ–¥—É—é—â–∞—è –Ω–µ–¥–µ–ª—è", bell[options.NextWeek]), "opt_week")},
		{tgbotapi.NewInlineKeyboardButtonData(milBell[options.Military], "opt_mil")},
		{tgbotapi.NewInlineKeyboardButtonData("‚ùå –û—Ç–∫–ª—é—á–∏—Ç—å—Å—è –æ—Ç –≥—Ä—É–ø–ø—ã", "opt_del")},
		{tgbotapi.NewInlineKeyboardButtonData("‚Ü© –ó–∞–∫—Ä—ã—Ç—å", "cancel")},
	}
	if options.First {
		markup = append(markup[:2], markup[1:]...)
		markup[1] = []tgbotapi.InlineKeyboardButton{
			tgbotapi.NewInlineKeyboardButtonData(fmt.Sprintf("‚è∞ –ù–∞—Å—Ç—Ä–æ–∏—Ç—å –≤—Ä–µ–º—è (%d)", options.FirstTime), "opt_set"),
		}
	}

	return tgbotapi.NewInlineKeyboardMarkup(markup...)
}

func (bot *Bot) HandleOptions(user *database.TgUser, query *tgbotapi.CallbackQuery) error {
	options := database.ShedulesInUser{
		L9Id: user.L9Id,
	}
	if _, err := bot.DB.Get(&options); err != nil {
		return err
	}
	switch query.Data {
	case "opt_first":
		options.First = !options.First
	case "opt_set":
		user.PosTag = database.Set
		if _, err := bot.DB.ID(user.L9Id).Update(user); err != nil {
			return err
		}
		txt := fmt.Sprintf(
			"–í–≤–µ–¥–∏ –≤—Ä–µ–º—è –≤ –º–∏–Ω—É—Ç–∞—Ö, –∑–∞ –∫–æ—Ç–æ—Ä–æ–µ –º–Ω–µ –Ω–∞–¥–æ —Å–æ–æ–±—â–∏—Ç—å –æ –Ω–∞—á–∞–ª–µ –∑–∞–Ω—è—Ç–∏–π"+
				"(–æ—Ç 15 –¥–æ 240 –º–∏–Ω—É—Ç)\n"+
				"–°–µ–π—á–∞—Å —É—Å—Ç–∞–Ω–æ–≤–ª–µ–Ω–æ %d –º–∏–Ω—É—Ç",
			options.FirstTime,
		)
		_, err := bot.EditOrSend(user.TgId, txt, "", CancelKey(), *query.Message)

		return err

	case "opt_del":
		user.PosTag = database.Delete
		if _, err := bot.DB.ID(user.L9Id).Update(user); err != nil {
			return err
		}
		txt := "‚ÅâÔ∏è–¢—ã –¥–µ–π—Å—Ç–≤–∏—Ç–µ–ª—å–Ω–æ —Ö–æ—á–µ—à—å –æ—Ç–∫–ª—é—á–∏—Ç—å—Å—è –æ—Ç —ç—Ç–æ–π –≥—Ä—É–ø–ø—ã?\n" +
			"–ù–∞–ø–∏—à–∏ <b>–î–∞</b> –¥–ª—è –ø–æ–¥—Ç–≤–µ—Ä–∂–¥–µ–Ω–∏—è, –¥–ª—è –æ—Ç–º–µ–Ω—ã –Ω–∞–∂–º–∏ –∫–Ω–æ–ø–∫—É –∏–ª–∏ –Ω–∞–ø–∏—à–∏ –ª—é–±–æ–π –¥—Ä—É–≥–æ–π —Ç–µ–∫—Å—Ç"
		_, err := bot.EditOrSend(user.TgId, txt, "", CancelKey(), *query.Message)

		return err

	case "opt_lesson":
		options.NextNote = !options.NextNote
	case "opt_day":
		options.NextDay = !options.NextDay
	case "opt_week":
		options.NextWeek = !options.NextWeek
	case "opt_mil":
		options.Military = !options.Military
	}
	if _, err := bot.DB.UseBool().ID(options.UID).Update(&options); err != nil {
		return err
	}
	_, err := bot.EditOrSend(user.TgId, optStr, "", OptMarkup(options), *query.Message)

	return err
}
