using SvetoforVKBot.Models.Updates;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using VkNet;
using VkNet.Enums.SafetyEnums;
using VkNet.Model.Keyboard;
using VkNet.Model.RequestParams;

namespace SvetoforVKBot.Models.Commands.Admin
{
    public class SelectStatisticCommand : Command
    {
        private const int ADMIN1 = 59111081;
        private const int ADMIN2 = 10160301; 
        private const int ADMIN3 = 369217524; 
        private const int ADMIN4 = 178385801; 
        //chatId == 59111081 || chatId == 10160301 || chatId == 369217524 || chatId == 178385801
        public override string Name => "{\"button\":\"selectStatistic\"}";

        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;
            string msg = "";
            double sumOrders = 0;
            DateTime dt;
            DateTime dt1;
            DateTime dt2;
            List<UserStats> userStats = new List<UserStats>();
            List<UserStats> userStats1 = new List<UserStats>();
            SalesFunnel salesFunnel = new SalesFunnel();

            try
            {
                if (update.@object.message.from_id == ADMIN1 || update.@object.message.from_id == ADMIN2 || update.@object.message.from_id == ADMIN3 || update.@object.message.from_id == ADMIN4)
                {

                    userStats = db.Users.ToList().ConvertAll<UserStats>(x => new UserStats {
                        chatId = x.chatId,
                        sourses = JsonConvert.DeserializeObject<Sourses>(x.sourse),
                        voronka = x.voronka,
                    });

                    /*SqlCommand getUsers = new SqlCommand("SELECT chatId, sourse, voronka FROM Users;", Con);
                    SqlDataReader rgetUsers = getUsers.ExecuteReader();
                    while (rgetUsers.Read())
                        userStats.Add(new UserStats()
                        {
                            chatId = Convert.ToInt32(rgetUsers["chatId"]),
                            sourses = JsonConvert.DeserializeObject<Sourses>(rgetUsers["sourse"].ToString()),
                            voronka = rgetUsers["voronka"].ToString(),
                        });
                    rgetUsers.Close();*/

                    msg += "📊 Статистика \n" +
                        "ВСЕГО: сег вч нед мес all\n";

                    foreach (var st in salesFunnel.stages)
                    {
                        dt = DateTime.Now;
                        dt2 = DateTime.Now;

                        msg += st + ": " + userStats.FindAll(u => u.voronka.Equals(st) && u.sourses.sourses.Count > 0 &&
                        u.sourses.sourses[0].dateTime.Date.Equals(dt.Date)).Count;

                        dt = dt.AddDays(-1);
                        msg += "__" + userStats.FindAll(u => u.voronka.Equals(st) && u.sourses.sourses.Count > 0 &&
                        u.sourses.sourses[0].dateTime.Date.Equals(dt.Date)).Count;

                        dt2 = dt2.AddDays(-7);
                        msg += "__" + userStats.FindAll(u => u.voronka.Equals(st) && u.sourses.sourses.Count > 0 &&
                        u.sourses.sourses[0].dateTime.Date.Equals(dt2.Date)).Count;
                        dt2 = dt2.AddDays(7);

                        dt2 = dt2.AddMonths(-1);
                        msg += "__" + userStats.FindAll(u => u.voronka.Equals(st) && u.sourses.sourses.Count > 0 &&
                        u.sourses.sourses[0].dateTime.Date.Equals(dt2.Date)).Count;
                        dt2 = dt2.AddMonths(1);

                        msg += "__" + userStats.FindAll(u => u.voronka.Equals(st)).Count;

                        msg += "\n";
                    }

                    msg += "Всего пользователей: " + userStats.Count + "\n";
                    msg += "\n";

                    @params.UserId = chatId;
                    @params.Message = msg;
                    @params.RandomId = GetRandomId();
                    client.Messages.SendAsync(@params);
                }
            }
            catch (Exception ee)
            {
                @params.Message = "‼Ошибка в SelectStatisticCommand: " + ee.Message;
                @params.Attachments = null;
                @params.Keyboard = null;
                @params.UserId = 59111081;
                @params.RandomId = GetRandomId();
                client.Messages.SendAsync(@params);
            }
        }
    }
}