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

namespace SvetoforVKBot.Models.Commands.LK
{
    public class GetHeightCommand
    {
        public void Execute(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db)
        {
            MessagesSendParams @params = new MessagesSendParams();
            var chatId = update.@object.message.from_id;
            int i = 0;
            var keyboardBuilder = new KeyboardBuilder().Clear();

            try
            {
                if (int.TryParse(update.@object.message.text.Trim(), out int height))
                {
                    var user = db.Users.Single(usr => usr.chatId == chatId);
                    user.height = height;
                    user.tag = "Личный кабинет - Данные";
                    db.SaveChanges();

                    SelectEditDataCommand selectEditData = new SelectEditDataCommand();
                    selectEditData.Execute(update, client, db);
                }
                else
                {
                    @params.UserId = chatId;
                    @params.Message = "Вы некорректно ввели рост 🤔 Введите, пожалуйста, число (до 3 символов).";
                    @params.RandomId = GetRandomId();
                    client.Messages.SendAsync(@params);
                }
            }
            catch (Exception ee)
            {
                @params.Message = "‼Ошибка в GetHeightCommand: " + ee.Message;
                @params.Attachments = null;
                @params.Keyboard = null;
                @params.UserId = 59111081;
                @params.RandomId = GetRandomId();
                client.Messages.SendAsync(@params);
            }
        }

        private static readonly RandomNumberGenerator Rng = RandomNumberGenerator.Create();
        public int GetRandomId()
        {

            var intBytes = new byte[4];

            Rng.GetBytes(intBytes);

            return BitConverter.ToInt32(intBytes, 0);

        }
    }
}