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

        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>();
            string name = "";
            Regex regex = new Regex("[^0-9]");
            try
            {
                string[] payload = update.@object.message.payload.Split('-');
                int userId = Convert.ToInt32(regex.Replace(payload[1], ""));
                //@params.Message = "Выберите участника.\n";


                var user = db.Users.Single(usr => usr.id == userId);
                int uChatId = user.chatId;
                string fio = user.fio;

                user.personal = 1;
                db.Entry(db.PersonalConsultations.Where(el => el.chatId == uChatId)).Property("state").CurrentValue = 2;
                db.SaveChanges();

                //SqlCommand updTag = new SqlCommand("UPDATE Users SET personal = @personal WHERE chatId = @chatId;", Con);
                //updTag.Parameters.AddWithValue("@chatId", uChatId);
                //updTag.Parameters.AddWithValue("@personal", 1);
                //updTag.ExecuteNonQuery();

                //SqlCommand updState = new SqlCommand("UPDATE PersonalConsultation SET state = @state WHERE chatId = @chatId;", Con);
                //updState.Parameters.AddWithValue("@chatId", uChatId);
                //updState.Parameters.AddWithValue("@state", 2);
                //updState.ExecuteNonQuery();

                @params.Message = "✅Персональный режим для участника " + fio + " включен";

                keyboardBuilder
                    //.AddButton("Вкл. персональный режим", "selectPersonalPlan-", KeyboardButtonColor.Positive)
                    //.AddLine()
                    .AddButton("Назад", "selectExpertLK", KeyboardButtonColor.Default);

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

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