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 SelectAcceptPDCommand : Command
    {
        public override string Name => "{\"button\":\"selectAcceptPD-";

        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 = "";

            try
            {
                string[] payload = update.@object.message.payload.Split('-');
                int curOption = Convert.ToInt32(regex.Replace(payload[1], ""));
                switch (curOption)
                {
                    case 0: // Согласен
                        var user = db.Users.Single(usr => usr.chatId == chatId);

                        user.agree = 1;
                        user.tag = "Подписчик - Регистрация - Пол";
                        if (user.voronka == "Старт")
                        {
                            
                            user.voronka = "Регистрация";
                        }
                        db.SaveChanges();

                        keyboardBuilder
                            .AddButton("М", "selectGender-1", KeyboardButtonColor.Primary)
                            .AddButton("Ж", "selectGender-2", KeyboardButtonColor.Primary)
                            .AddLine()
                            .AddButton("Отменить", "startPL", KeyboardButtonColor.Default);

                        @params.Keyboard = keyboardBuilder.Build();
                        @params.UserId = chatId;
                        @params.Message = //"1 шаг из 4. Расскажите немного о себе 😉\n" +
                            "Выберите на клавиатуре внизу или напишите пол в формате \"М\" или \"Ж\"";
                        @params.RandomId = GetRandomId();
                        client.Messages.SendAsync(@params);
                        break;
                    case 1: // Не согласен
                        StartCommand start = new StartCommand();
                        start.Execute(update, client, db);
                        break;
                }
                

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