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 System.Text.RegularExpressions;
using VkNet;
using VkNet.Enums.SafetyEnums;
using VkNet.Model.Attachments;
using VkNet.Model.Keyboard;
using VkNet.Model.RequestParams;

namespace SvetoforVKBot.Models.Commands.Registration
{
    public class ShowActivity
    {
        public void Execute(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db, int expert)
        {
            MessagesSendParams @params = new MessagesSendParams();
            var chatId = update.@object.message.from_id;
            int i = 0;
            int index = 0;
            Regex regex = new Regex("[^0-9]");
            string backPL = "startPL";
            var keyboardBuilder = new KeyboardBuilder().Clear();
            List<StackObject> techStack = new List<StackObject>();
            string msg = "";
            try
            {
                var user = db.Users.Single(usr => usr.chatId == chatId);
                if (expert == 0)
                {
                    msg = "На какое направление ты хочешь подать свой проект?\n\n";
                    user.tag = "Проектный офис - Направление";
                }
                else
                {
                    msg = "В какой области/направлении ты готов выступить экспертом?\n\n";
                    user.tag = "Проектный офис - Эксепрт - Направление";
                }
                db.SaveChanges();

                //SqlCommand getTechStack = new SqlCommand("SELECT * FROM Activity;", Con);
                //SqlDataReader rgetTechStack = getTechStack.ExecuteReader();
                //while (rgetTechStack.Read())
                //{
                //    techStack.Add(new StackObject()
                //    {
                //        id = Convert.ToInt32(rgetTechStack["id"]),
                //        name = rgetTechStack["name"].ToString(),
                //        description = rgetTechStack["description"].ToString(),
                //    });
                //}
                //rgetTechStack.Close();

                int btnCount = 0;
                foreach (var s in techStack)
                {

                    keyboardBuilder
                        .AddButton(s.name, "selectPOTheme-" + s.id + "-" + expert, KeyboardButtonColor.Primary);
                    btnCount++;

                    msg += btnCount + ". " + s.description + "\n";

                    if (btnCount % 2 == 0)
                        keyboardBuilder.AddLine();
                    if (btnCount == 14) break;
                }

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

                msg += "\nВыбери на клавиатуре👇";

                keyboardBuilder
                    .AddButton("⬅Назад", "startPL", KeyboardButtonColor.Default);


                @params.Message = msg;
                @params.UserId = chatId;
                @params.Keyboard = keyboardBuilder.Build();
                @params.RandomId = GetRandomId();
                client.Messages.SendAsync(@params);
            }
            catch (Exception ee)
            {
                @params.Message = "‼Ошибка в ShowActivity: " + 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);
        }
    }
}