﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using VkNet;
using VkNet.Model;
using VkNet.Model.RequestParams;

namespace VkSystemBot.Classes
{
    internal class SpecialAction
    {
        private readonly static Random Rnd = new Random();
        private readonly static Dictionary<object, object> Hashtable = new Dictionary<object, object>();
        public static List<Tuple<string, Func<Message, bool>, Func<VkApi, long, RestrictSytemDescribe[], User[], long, long>>> Actions;

        static SpecialAction()
        {
            Actions = new List<Tuple<string, Func<Message, bool>, Func<VkApi, long, RestrictSytemDescribe[], User[], long, long>>>()
            {
                new Tuple<string,Func<Message,bool>, Func<VkApi, long, RestrictSytemDescribe[], User[], long, long>>(
                    "НЕ МЕШАЙТЕ!",
                    msg => msg.UserId.GetValueOrDefault() == 5846359 && Rnd.NextDouble() > 0.75,
                    (api, l, arg3,users,msgId) => api.Messages.Send(new MessagesSendParams() { PeerId = l, Message = "НЕ МЕШАЙТЕ РАБОТАТЬ!!!"})),

                    new Tuple<string,Func<Message,bool>,Func<VkApi, long, RestrictSytemDescribe[], User[], long, long>>(
                        "АВТОСТАРТ",
                        msg =>
                        {
                            var dtime = (DateTime)Hashtable["АВТОСТАРТ"];
                            if ((dtime - DateTime.Now).Hours > 4)
                            {
                                Hashtable["АВТОСТАРТ"] = dtime;
                                return true;
                            }
                            return false;
                        },
                        (api, l, arg3,users,msgId) =>
                        {
                            foreach (RestrictSytemDescribe o in arg3)
                            {
                                if (o.TriggeredWords.Contains("СМУЗИ") || o.TriggeredWords.Contains("ПИДОР") ||
                                    o.TriggeredWords.Contains("смузи") || o.TriggeredWords.Contains("пидор"))
                                {
                                    var fullDesc = SqLiteManager.GetPhrases(o);
                                    var lastProcessedMsg = msgId;
                                    foreach (var phrase in fullDesc.TreePhrase.GetPhrases(users.ToList()))
                                    {
                                        lastProcessedMsg = api.Messages.Send(new MessagesSendParams
                                        {
                                            PeerId = l,
                                            Message = phrase

                                        });
                                        Thread.Sleep(Rnd.Next(1500, 5000));
                                    }
                                    return lastProcessedMsg;
                                }

                            }
                            return msgId;
                        }
                        )
            };
            Hashtable.Add("АВТОСТАРТ", DateTime.Now);

        }

        public static bool IsThrowed(Message msg, VkApi api, long chatId, RestrictSytemDescribe[] systems,User[] users, ref long msgId, out string name)
        {
            foreach (var action in Actions)
            {
                if (action.Item2(msg))
                {
                    name = action.Item1;
                    msgId = action.Item3(api, chatId, systems, users, msgId);
                    return true;
                }
            }
            name = String.Empty;
            return false;
        }
    }
}
