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

namespace SvetoforVKBot.Models.Commands.Registration
{
    public class SelectGenderCommand : Command
    {
        public override string Name => "{\"button\":\"selectGender-";

        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;
            var keyboardBuilder = new KeyboardBuilder().Clear();
            Regex regex = new Regex("[^0-9]");
            string voronka = "";
            int i = 0;
            int btnCount = 0;
            int btnLines = 0;
            int startYear = DateTime.Now.Year - 18;

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

                var user = db.Users.Single(usr => usr.chatId == chatId);
                user.tag = "Подписчик - Регистрация - ДР - Год";
                user.gender = gender;
                db.SaveChanges();

                keyboardBuilder
                            .AddButton("Отменить", "startPL", KeyboardButtonColor.Default);
                for (i = startYear; i > startYear - 20; i--)
                {
                    keyboardBuilder
                        .AddButton(i.ToString(), i.ToString(), KeyboardButtonColor.Primary);

                    btnCount++;

                    if (btnCount % 4 == 0)
                    {
                        btnLines++;
                        keyboardBuilder.AddLine();
                    }
                }

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

                keyboardBuilder
                    .AddButton("Отменить", "startPL", KeyboardButtonColor.Default);

                @params.Keyboard = keyboardBuilder
                    .Build();

                @params.Message = "Отлично! Теперь выберите на клавиатуре внизу или введите свой год рождения, например 1993. \n";
                @params.UserId = chatId;
                @params.RandomId = GetRandomId();
                client.Messages.SendAsync(@params);


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