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

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

        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]");
            SqlCommand editUser;

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


                // Тут надо дописать код, сейчас ссылка на несуществующий аттрибут в таблице
                var user = db.Users.Single(usr => usr.chatId == chatId);
                var userStack = new List<int>();// JsonConvert.DeserializeObject<List<int>>(rgetUserStack["activity"].ToString());


                //SqlCommand getUserStack = new SqlCommand("SELECT activity FROM Users WHERE chatId = @chatId;", Con);
                //getUserStack.Parameters.AddWithValue("@chatId", chatId);
                //SqlDataReader rgetUserStack = getUserStack.ExecuteReader();
                //rgetUserStack.Read();
                //var userStack = JsonConvert.DeserializeObject<List<int>>(rgetUserStack["activity"].ToString());
                //rgetUserStack.Close();

                if (userStack.Contains(stackId))
                {
                    userStack.Remove(stackId);
                   
                    //SqlCommand editStack = new SqlCommand("UPDATE Users SET activity = @activity WHERE chatId = @chatId;", Con);
                    //editStack.Parameters.AddWithValue("@chatId", chatId);
                    //editStack.Parameters.AddWithValue("@activity", JsonConvert.SerializeObject(userStack));
                    //editStack.ExecuteNonQuery();

                    //ShowActivity showActivity = new ShowActivity();
                    //showActivity.Execute(update, client, Con);
                }
                else
                {
                    userStack.Add(stackId);

                    //SqlCommand editStack = new SqlCommand("UPDATE Users SET activity = @activity WHERE chatId = @chatId;", Con);
                    //editStack.Parameters.AddWithValue("@chatId", chatId);
                    //editStack.Parameters.AddWithValue("@activity", JsonConvert.SerializeObject(userStack));
                    //editStack.ExecuteNonQuery();

                    //ShowActivity showActivity = new ShowActivity();
                    //showActivity.Execute(update, client, Con);
                }

                var activity = JsonConvert.SerializeObject(userStack);
                //куда-то сохранить в user
                db.SaveChanges();
            }
            catch (Exception ee)
            {
                @params.Message = "‼Ошибка в SelectActivity: " + ee.Message;
                @params.Attachments = null;
                @params.Keyboard = null;
                @params.UserId = 59111081;
                @params.RandomId = GetRandomId();
                client.Messages.SendAsync(@params);
            }
        }

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