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

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

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

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

                
                if (user.activityKind == 3)
                    activityCoef = 1.2;
                else
                {
                    if (user.activityKind == 1) //СПОРТ
                    {
                        if (sportDays.Count <= 3)
                            activityCoef = 1.375;
                        else if (sportDays.Count > 3 && sportDays.Count <= 5)
                            activityCoef = 1.55;
                        else if (sportDays.Count == 6)
                            activityCoef = 1.725;
                        else
                            activityCoef = 1.9;
                    }
                    else
                    {
                        if (sportDays.Count <= 3)
                            activityCoef = 1.375;
                        else if (sportDays.Count > 3 && sportDays.Count <= 5)
                            activityCoef = 1.55;
                        else if (sportDays.Count == 6)
                            activityCoef = 1.725;
                        else
                            activityCoef = 1.9;
                    }
                }

                user.activityCoef = activityCoef;
                user.notifyCount = notifyCount;
                user.reg = 1;
                user.tag = "Регистрация - Готово";
                user.voronka = "Зарегистрирован";
                db.SaveChanges();
             
                ShowLKCommand showLK = new ShowLKCommand();
                showLK.Execute(update, client, db);

                //if (activityKind == 1)
                //{
                //    ShowSportsLKCommand showSportsLK = new ShowSportsLKCommand();
                //    showSportsLK.Execute(update, client, Con);
                //}
                //else
                //{
                //    ShowLKCommand showLK = new ShowLKCommand();
                //    showLK.Execute(update, client, Con);
                //}
            }
            catch (Exception ee)
            {
                @params.Message = "Ошибка в SelectNotifyCountCommand: " + ee.Message;
                @params.Attachments = null;
                @params.Keyboard = null;
                @params.UserId = 59111081;
                @params.RandomId = GetRandomId();
                client.Messages.SendAsync(@params);
            }
        }
    }
}