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 System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace SvetoforVKBot.Models.Commands.LK.Reports
{
    public class SelectReportRateCommand : Command
    {
        public override string Name => "{\"button\":\"selectReportRate-";

        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();

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

                var user = db.Users.Single(usr => usr.chatId == chatId);

                db.Reports.Add(new Data.Reports()
                {
                    chatId = chatId,
                    rate = rate,
                    datetime = DateTime.Now
                });
                db.SaveChanges();

                @params.Message = "Ваша дневная норма: " + user.kkal + " ккал.\n" +
                    "Справились?";

                keyboardBuilder
                    .AddButton("Меньше", "selectReportCcal-2", KeyboardButtonColor.Primary)
                    .AddButton("Норма", "selectReportCcal-1", KeyboardButtonColor.Positive)
                    .AddButton("Больше", "selectReportCcal-3", KeyboardButtonColor.Primary)
                    .AddLine()
                    .AddButton("Назад", "startPL", KeyboardButtonColor.Default);

                @params.Keyboard = keyboardBuilder.Build();
                @params.UserId = chatId;
                @params.RandomId = GetRandomId();
                client.Messages.SendAsync(@params);

            }
            catch (Exception ee)
            {
                @params.Message = "‼Ошибка в SelectReportRateCommand: " + ee.Message;
                @params.Attachments = null;
                @params.Keyboard = null;
                @params.UserId = 59111081;
                @params.RandomId = GetRandomId();
                client.Messages.SendAsync(@params);
            }
        }
    }
}