using System;
using System.Data.SqlClient;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using SvetoforVKBot.Models.Updates;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using VkNet;
using VkNet.Model.Keyboard;
using VkNet.Model.RequestParams;
using VkNet.Enums.SafetyEnums;
using SvetoforVKBot.Models.Commands.LK;
using System.Linq;

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

        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;
            Regex regex = new Regex("[^0-9]");
            var keyboardBuilder = new KeyboardBuilder().Clear();
            string tag = "";
            try
            {
                string[] callBack = update.@object.message.payload.Split('-');
                string[] date = callBack[1].Split('.');
                DateTime selectedDateTime = new DateTime(Convert.ToInt32(regex.Replace(date[2], "")), Convert.ToInt32(regex.Replace(date[1], "")), Convert.ToInt32(regex.Replace(date[0], "")));


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

                //SqlCommand getUser = new SqlCommand("SELECT reg FROM Users WHERE chatId = @chatId;", Con);
                //getUser.Parameters.AddWithValue("@chatId", chatId);
                //SqlDataReader rgetUser = getUser.ExecuteReader();
                //rgetUser.Read();
                //int reg = Convert.ToInt32(rgetUser["reg"]);
                //rgetUser.Close();

                if (user.reg == 1)
                {
                    user.birthday = selectedDateTime;
                    user.tag = "Личный кабинет - Данные";
                    db.SaveChanges();

                    SelectEditDataCommand selectEditData = new SelectEditDataCommand();
                    selectEditData.Execute(update, client, db);
                    return;
                }
                else
                {
                    user.birthday = selectedDateTime;
                    user.tag = "Подписчик - Регистрация - Телефон";
                    db.SaveChanges();

                    @params.Message = "Напишите номер телефона в формате 11 цифр через \"8\" или \"7\" \n";

                    keyboardBuilder
                                .AddButton("Отменить", "startPL", KeyboardButtonColor.Default);
                    @params.Keyboard = keyboardBuilder.Build();
                    @params.UserId = chatId;
                    @params.RandomId = GetRandomId();
                    client.Messages.SendAsync(@params);
                }
            }
            catch (Exception ee)
            {
                @params.Message = "‼Ошибка в SelectDateCommand: " + ee.Message;
                @params.Attachments = null;
                @params.Keyboard = null;
                @params.UserId = 59111081;
                @params.RandomId = GetRandomId();
                client.Messages.SendAsync(@params);
            }
        }
    }
}