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 VkNet.Model.Attachments;
using System.Collections.Generic;
using System.Threading;
using SvetoforVKBot.Models.Dop;
using SvetoforVKBot.Models.Commands;
using System.Linq;

namespace SvetoforVKBot.Models.Commands.Registration
{
    public class SelectParticipateCommand : Command
    {
        public override string Name => "{\"button\":\"selectParticipate\"}";

        public override void Execute(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db)
        {
        }

        public override void ExecutePL(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db)
        {
            var chatId = update.@object.message.from_id;
            MessagesSendParams @params = new MessagesSendParams();
            var keyboardBuilder = new KeyboardBuilder().Clear();

            try
            {
                var user = db.Users.Single(usr => usr.chatId == chatId);

                user.tag = "Подписчик - Регистрация - Авторизация";
                db.SaveChanges();

                if (user.isAuthorized == 0)
                {
                    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" +
                        "Например, stud0000";
                    @params.RandomId = GetRandomId();
                    client.Messages.SendAsync(@params);
                }
              
            }
            catch (Exception ee)
            {
                @params.Message = "Ошибка в SelectParticipateCommand: " + ee.Message;
                @params.Attachments = null;
                @params.Keyboard = null;
                @params.UserId = 59111081;
                @params.RandomId = GetRandomId();
                client.Messages.SendAsync(@params);
            }
        }
    }
}