using System;
using System.Data.SqlClient;
using SvetoforVKBot.Models.Updates;
using VkNet;
using VkNet.Enums.SafetyEnums;
using VkNet.Model.Keyboard;
using VkNet.Model.RequestParams;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;

namespace SvetoforVKBot.Models.Commands.LK.Menu
{
    public class SelectPersonalMenuCommand : Command
    {
        public override string Name => "{\"button\":\"selectPersonalMenu-";

        public override void Execute(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db)
        {
        }

        public override void ExecutePL(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db)
        {
            var chatId = update.@object.message.from_id;
            MessagesSendParams @params = new MessagesSendParams();
            Regex regex = new Regex("[^0-9]");
            string msg = "";
            var keyboardBuilder = new KeyboardBuilder().Clear();
            double sum = 0;
            string backPL = "startPL";
            int kkals = 0;

            try
            {
                switch (DateTime.Now.DayOfWeek)
                {
                    case DayOfWeek.Saturday:
                    case DayOfWeek.Sunday:
                        keyboardBuilder
                            .AddButton("Назад", "startPL", KeyboardButtonColor.Default);
                        @params.Message = "Рекомендации по дневному меню доступны с понедельника по пятницу.\n" +
                            "Чтобы вернуться в Личный кабинет, нажмите кнопку \"Назад\"";
                        @params.Keyboard = keyboardBuilder.Build();
                        @params.UserId = chatId;
                        @params.RandomId = GetRandomId();
                        client.Messages.SendAsync(@params);
                        return;
                }

                string[] payload = update.@object.message.payload.Split('-');
                int foodType = Convert.ToInt32(regex.Replace(payload[1], ""));
                int curDayKkal = Convert.ToInt32(regex.Replace(payload[2], ""));

                var user = db.Users.Single(usr => usr.chatId == chatId);
                var cartObject = JsonConvert.DeserializeObject<CartObject>(user.cart);

                foreach (var o in cartObject.orders)
                {
                    sum += o.price * o.number;
                    kkals += o.kkal * o.number;
                }

                //keyboardBuilder
                //            .AddButton($"Завтрак (до {Math.Round(curDayKkal * 1.2 * 0.3)} ккал)", "selectPersonalMenu-1-" + curDayKkal, KeyboardButtonColor.Primary)
                //            .AddLine()
                //            .AddButton($"Обед (до {Math.Round(curDayKkal * 1.2 * 0.4)} ккал)", "selectPersonalMenu-2-" + curDayKkal, KeyboardButtonColor.Primary)
                //            .AddLine()
                //            .AddButton($"Перекус (до {Math.Round(curDayKkal * 1.2 * 0.1)} ккал)", "selectPersonalMenu-3-" + curDayKkal, KeyboardButtonColor.Primary)
                //            .AddLine()
                //            .AddButton($"Ужин (до {Math.Round(curDayKkal * 1.2 * 0.3)} ккал)", "selectPersonalMenu-4-" + curDayKkal, KeyboardButtonColor.Primary)
                //            .AddLine();

                //if (cartObject.orders.Count > 0)
                //{
                //    keyboardBuilder
                //        .AddButton("Личный кабинет", "startPL", KeyboardButtonColor.Default)
                //        .AddButton("Корзина: " + sum + " р", "selectCart-0", KeyboardButtonColor.Negative);
                //}
                //else
                //{
                //    keyboardBuilder
                //        .AddButton("Личный кабинет", "startPL", KeyboardButtonColor.Default);
                //}

                ////@params.Template = templateBuilder.Build();
                //@params.Keyboard = keyboardBuilder.Build();
                //@params.Message = "Если хотите подобрать блюда ещё раз, нажмите кнопку повторно😉";
                //@params.UserId = chatId;
                //@params.RandomId = GetRandomId();
                //client.Messages.SendAsync(@params);

                ShowPersonalMenuCarusel showPersonalMenu = new ShowPersonalMenuCarusel();
                showPersonalMenu.Execute(update, client, db, foodType, 0, 1, curDayKkal);
            }
            catch (Exception ee)
            {
                @params.Message = "‼Ошибка в SelectMenuCommand: " + ee.Message;
                @params.Attachments = null;
                @params.Keyboard = null;
                @params.UserId = 59111081;
                @params.RandomId = GetRandomId();
                client.Messages.SendAsync(@params);
            }
        }
    }
}