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 ShowSportKinds
    {
        public void Execute(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db, int kind)
        {
            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<SportObject> sports = new List<SportObject>();
            string msg = "";
            try
            {
                switch (kind)
                {
                    case 1:
                        msg = "Выберите основной вид спорта. Нажмите кнопку👇\n";
                        break;

                    case 2:
                        msg = "Выберите основной вид активности. Нажмите кнопку👇\n";
                        break;

                    default:
                        return;
                }

                var user = db.Users.Single(usr => usr.chatId == chatId);
                user.activityKind = kind;
                user.tag = "Регистрация - Вид спорта";
                db.SaveChanges();

                sports = db.SportKinds.Where(s => s.kind == kind).ToList().
                    ConvertAll<SportObject>(s => new SportObject()
                    {
                        id = s.id,
                        name = s.name,
                        btnName = s.btnName,
                        kind = s.kind
                    }) ;

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

                    keyboardBuilder
                        .AddButton(s.btnName, "selectSports-" + s.id + "-" + kind, 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 = "‼Ошибка в ShowSportKinds: " + 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);
        }
    }
}