using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using SvetoforVKBot.Models.Updates;
using VkNet;
using VkNet.Enums.SafetyEnums;
using VkNet.Model.Keyboard;
using VkNet.Model.RequestParams;

namespace SvetoforVKBot.Models.Commands.LK.EditData
{
    public class GetEditFIOCommand
    {
        public void Execute(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db)
        {
            var chatId = update.@object.message.from_id;
            MessagesSendParams @params = new MessagesSendParams();

            try
            {
                if (update.@object.message.text.Length > 0 && update.@object.message.text.Length <= 50)
                {
                    var ids = new long[] { chatId };

                    var user = db.Users.Single(usr => usr.chatId == chatId);
                    user.fio = update.@object.message.text.Trim();
                    user.tag = "Личный кабинет - Данные";
                    db.SaveChanges();

                    SelectEditDataCommand selectEditData = new SelectEditDataCommand();
                    selectEditData.Execute(update, client, db);

                }
                else
                {
                    @params.Message = "Данные введены некорректно 😕 Напишите, пожалуйста, текст длиной до 50 символов.";
                    @params.UserId = chatId;
                    @params.RandomId = GetRandomId();
                    client.Messages.SendAsync(@params);
                }
            }
            catch (Exception ee)
            {
                @params.Message = "‼Ошибка в GetEditFIOCommand: " + 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);
        }
    }
}