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.ExpertLK
{
    public class SelectExpertLKCommand : Command
    {
        public override string Name => "{\"button\":\"selectExpertLK\"}";

        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();
            List<UserObject> users = new List<UserObject>();
            List<int> chatIds = new List<int>();
            try
            {
                @params.Message = "Личный кабинет специалиста.\n";

                //вот это
                chatIds = db.PersonalConsultations.Where(c => c.chatId == chatId && c.state == 1)?
                    .ToList().ConvertAll<int>(el => el.chatId);
                //вместо этого
                //SqlCommand getCons = new SqlCommand("SELECT * FROM PersonalConsultation WHERE chatId = @chatId AND state = 1;", Con);
                //getCons.Parameters.AddWithValue("@chatId", chatId);
                //SqlDataReader rgetCons = getCons.ExecuteReader();
                //if (rgetCons.HasRows)
                //{
                //    while (rgetCons.Read())
                //        chatIds.Add(Convert.ToInt32(rgetCons["chatId"]));

                //    rgetCons.Close();
                //}
                //else
                //    rgetCons.Close();

                if (chatIds.Count > 0)
                {
                    keyboardBuilder
                    .AddButton("Участники: " + chatIds.Count.ToString(), "selectParticipants", KeyboardButtonColor.Positive)
                    .AddButton("Вопросы", "selectExQuestions", KeyboardButtonColor.Primary)
                    .AddLine()
                    .AddButton("Назад", "selectAdminMenu", KeyboardButtonColor.Default);
                }
                else
                {
                    keyboardBuilder
                    .AddButton("Участники", "selectExpertLK", KeyboardButtonColor.Primary)
                    .AddButton("Вопросы", "selectExQuestions", KeyboardButtonColor.Primary)
                    .AddLine()
                    .AddButton("Назад", "selectAdminMenu", KeyboardButtonColor.Default);
                }

                

                @params.Keyboard = keyboardBuilder.Build();
                @params.UserId = chatId;
                @params.RandomId = GetRandomId();
                client.Messages.SendAsync(@params);

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