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

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

        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;
            string msg = "";
            string voronka = "";
            double sum = 0;
            string backPL = "startPL";
            Regex regex = new Regex("[^0-9]");
            ShowProductCaruselMain showProductCaruselMain = new ShowProductCaruselMain();
            ShowProductMain showProductMain = new ShowProductMain();
            CategoriesDostavka categories = new CategoriesDostavka();
            var keyboardBuilder = new KeyboardBuilder().Clear();

            try
            {
                string[] payload = update.@object.message.payload.Split('-');
                int curCategory = Convert.ToInt32(regex.Replace(payload[1], ""));
                int curProduct = Convert.ToInt32(regex.Replace(payload[2], ""));
                int curWay = Convert.ToInt32(regex.Replace(payload[3], "")); //для ShowProductCaruselMain направление навигации, 1 по умолчанию

                var cats = db.CategoriesDostavkas.Where(e => e.subMenu == curCategory);
                if(cats.Count() > 0)
                {                    
                    ShowMenuDostavka showMenuDostavka = new ShowMenuDostavka();
                    showMenuDostavka.Execute(update, client, db, msg, 2, categories.subMenu); //1
                }
                else
                {
                    switch (curCategory)
                    {                       
                        default:
                            if (update.@object.client_info.carousel == true)
                                showProductCaruselMain.Execute(update, client, db, curCategory, curProduct, curWay);
                            else
                                showProductMain.Execute(update, client, db, curCategory, curProduct, 0, 1);

                            break;
                    }
                }

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