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.Registration
{
    public class ShowSportDays
    {
        public void Execute(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db)
        {
            MessagesSendParams @params = new MessagesSendParams();
            var chatId = update.@object.message.from_id;
            var keyboardBuilder = new KeyboardBuilder().Clear();
            List<SportObject> sports = new List<SportObject>();
            var kbColor = KeyboardButtonColor.Primary;

            try
            {
                Dictionary<int, string> days = new Dictionary<int, string>(7);
                days.Add(1, "Пн");
                days.Add(2, "Вт");
                days.Add(3, "Ср");
                days.Add(4, "Чт");
                days.Add(5, "Пт");
                days.Add(6, "Сб");
                days.Add(7, "Вс");

                var user = db.Users.Single(usr => usr.chatId == chatId);
                List<int> sportDays = JsonConvert.DeserializeObject<List<int>>(user.sportDays);

                if (user.activityKind == 1)
                    @params.Message = "Отметьте все дни, в которые Вы занимаетесь спортом на текущей неделе👇\n";
                else
                    @params.Message = "Отметьте все дни, в которые Вы занимаетесь физкультурой на текущей неделе👇\n";


                for (int i = 1; i <= 7; i++)
                {
                    if (sportDays.Contains(i))
                        kbColor = KeyboardButtonColor.Positive;
                    else
                        kbColor = KeyboardButtonColor.Primary;

                    keyboardBuilder
                        .AddButton(days[i], "selectDay-" + i, kbColor);

                    if (i % 2 == 0)
                        keyboardBuilder.AddLine();
                }

                if (sportDays.Count > 0)
                    keyboardBuilder
                        .AddButton("Готово", "selectDay-0", KeyboardButtonColor.Positive);
                else
                    keyboardBuilder
                        .AddButton("Готово", "selectDay-0", KeyboardButtonColor.Negative);

                keyboardBuilder
                    .AddLine()
                    .AddButton("В начало", "startPL", KeyboardButtonColor.Default);

                @params.UserId = chatId;
                @params.Keyboard = keyboardBuilder.Build();
                @params.RandomId = GetRandomId();
                client.Messages.SendAsync(@params);
            }
            catch (Exception ee)
            {
                @params.Message = "‼Ошибка в ShowSportDays: " + 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();
        private int GetRandomId()
        {

            var intBytes = new byte[4];

            Rng.GetBytes(intBytes);

            return BitConverter.ToInt32(intBytes, 0);
        }
    }
}