using System;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using SvetoforVKBot.Models.Commands.LK.Cart;
using SvetoforVKBot.Models.Updates;
using VkNet;
using VkNet.Model.RequestParams;
using SvetoforVKBot.Models.Commands.LK.Menu.Products;
using VkNet.Model.Keyboard;
using VkNet.Enums.SafetyEnums;
using System.Linq;

namespace SvetoforVKBot.Models.Commands.LK.Cart
{
    public class SelectEditCartCommand : Command
    {
        public override string Name => "{\"button\":\"selectEditCart-";

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

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


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

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


                if (cartObject.orders[curCart].id.StartsWith("0-")) return;

                switch (action)
                {
                    case 1: // -                 
                        if (cartObject.orders[curCart].number == 1)
                            cartObject.orders.RemoveAt(curCart);
                        else
                            cartObject.orders[curCart].number--;
                        break;
                    case 2: // +
                        cartObject.orders[curCart].number++;
                        break;
                };

                if (action > 2)
                {
                    int count = action - 3;
                    if (count == 0)
                        cartObject.orders.RemoveAt(curCart);
                    else
                        cartObject.orders[curCart].number = count;
                }

                user.cart = JsonConvert.SerializeObject(cartObject);
                db.SaveChanges();

                if (cartObject.orders.Count > 0)
                {
                    if (curCart >= cartObject.orders.Count)
                        curCart = cartObject.orders.Count - 1;
                    ShowCart showCart = new ShowCart();
                    showCart.Execute(update, client, db, curCart);
                }
                else
                {
                    msg = "Корзина оказалась пустой. \n" +
                        "\n" +
                        "Возвращаю в Личный кабинет";
                    ShowLKCommand showLK = new ShowLKCommand();
                    showLK.Execute(update, client, db);
                }
            }
            catch (Exception ee)
            {
                @params.Message = "‼Ошибка в SelectEditCartCommand: " + ee.Message;
                @params.Attachments = null;
                @params.Keyboard = null;
                @params.UserId = 59111081;
                @params.RandomId = GetRandomId();
                client.Messages.SendAsync(@params);
            }
        }
    }
}