using Newtonsoft.Json;
using SvetoforVKBot.Models.Updates;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Linq;
using System.Security.Cryptography;
using VkNet;
using VkNet.Enums.SafetyEnums;
using VkNet.Model.Attachments;
using VkNet.Model.Keyboard;
using VkNet.Model.RequestParams;

namespace SvetoforVKBot.Models.Commands.LK.Menu
{
    public class ShowMenuDostavka
    {
        public void Execute(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db, string msg, int plAction, int subMenu)
        {
            MessagesSendParams @params = new MessagesSendParams();
            var chatId = update.@object.message.from_id;
            var ids = new long[] { chatId };
            var user = client.Users.Get(ids);
            string voronka = "";
            double sum = 0;
            string backPL = "";
            List<CategoriesDostavka> categories = new List<CategoriesDostavka>();
            var row = new List<MessageKeyboardButton>();
            var listButtons = new List<ReadOnlyCollection<MessageKeyboardButton>>();
            var kbColor = KeyboardButtonColor.Positive;
            var keyboardBuilder = new KeyboardBuilder().Clear();

            try
            {
                categories = db.CategoriesDostavkas.Where(c=>c.subMenu == subMenu).ToList().ConvertAll<CategoriesDostavka>(c =>
                    new CategoriesDostavka()
                    {
                        categoryId = c.categoryId,
                        inTheOrder = c.inTheOrder,
                        name = c.name,
                        btnName = c.btnName,
                        kbColor = c.kbColor,
                    });

                //SqlCommand getCategories = new SqlCommand("SELECT * FROM CategoriesDostavka WHERE subMenu = @subMenu;", Con);
                //getCategories.Parameters.AddWithValue("@subMenu", subMenu);
                //SqlDataReader rgetCategories = getCategories.ExecuteReader();
                //while (rgetCategories.Read())
                //    categories.Add(new CategoriesDostavka()
                //    {
                //        categoryId = Convert.ToInt32(rgetCategories["categoryId"]),
                //        inTheOrder = Convert.ToInt32(rgetCategories["inTheOrder"]),
                //        name = rgetCategories["name"].ToString(),
                //        btnName = rgetCategories["btnName"].ToString(),
                //        kbColor = rgetCategories["kbColor"].ToString(),
                //    });
                //rgetCategories.Close();

                categories.RemoveAll(c => c.inTheOrder.Equals(0));
                categories.Sort((x, y) => x.inTheOrder.CompareTo(y.inTheOrder));

                int btnCount = 0;
                foreach (var c in categories)
                {
                    switch (c.kbColor)
                    {
                        case "Default":
                            kbColor = KeyboardButtonColor.Default;
                            break;
                        case "Positive":
                            kbColor = KeyboardButtonColor.Positive;
                            break;
                        case "Negative":
                            kbColor = KeyboardButtonColor.Negative;
                            break;
                        case "Primary":
                            kbColor = KeyboardButtonColor.Primary;
                            break;
                    }

                    keyboardBuilder
                        .AddButton(c.btnName, "selectProduct-" + c.categoryId + "-0-1", kbColor);

                    btnCount += 1;

                    if (btnCount % 2 == 0)
                    {
                        keyboardBuilder.AddLine();
                    }
                }
                if (btnCount % 2 != 0)
                {
                    keyboardBuilder.AddLine();
                }

                if (subMenu == 0)
                {
                    backPL = "startPL";
                }
                else
                    backPL = "selectMenu-0";


                keyboardBuilder
                    .AddButton("Назад", "selectMenu-0", KeyboardButtonColor.Default)
                    .AddButton("Личный кабинет", "startPL", KeyboardButtonColor.Default);


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

        private static readonly RandomNumberGenerator Rng = RandomNumberGenerator.Create();
        private int GetRandomId()
        {

            var intBytes = new byte[4];

            Rng.GetBytes(intBytes);

            return BitConverter.ToInt32(intBytes, 0);
        }
    }
}
