﻿using ProtoBuf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace PCMRBot
{
    [ProtoContract]
    public class PrivateChatHandler
    {
        [ProtoMember(1)]
        public IDictionary<string, IPrivateCommand> commands = new Dictionary<string,IPrivateCommand>();
        
        //internal PrivateCommandContainer pcc = new PrivateCommandContainer();
        public PrivateChatHandler()
        {
            //init();
        }
        public void init()
        {
            //commands.Add("ping", new PCPing());
            commands.Add("bind", new PrivateCommands.PCBindCommand());
            commands.Add("claimsu", new PrivateCommands.PCClaimSU());
        }
        
        internal List<ChatReply> handleMessage(SteamKit2.SteamFriends.FriendMsgCallback obj)
        {
            List<ChatReply> reply = new List<ChatReply>();
            GlobalUser chatter;
            try
            {
                chatter = Program.users[obj.Sender];
            }
            catch
            {
                Program.users.Add(obj.Sender, new GlobalUser(obj.Sender));
                chatter = Program.users[obj.Sender];
            }
            if (true)
            {
                string[] args = obj.Message.Split(' ');
                args[0] = args[0].ToLower();
                IPrivateCommand command;
                try
                {
                    command = commands[args[0]];
                }
                catch
                {
                    command = new CommandAsk();
                    reply = command.RunPrivateCommand(args, obj);
                    //reply.Add(new ChatReply(chatter.user, string.Format("The command {0} does not exist", args[0])));
                    return reply;
                }
                if (command.RequiredPermsForPrivate <= chatter.perms)
                {
                    reply = command.RunPrivateCommand(args, obj);
                }
                else
                {
                    reply.Add(new ChatReply(chatter.id, string.Format("You do not have permission to execute the command {0}", args[0])));
                }
            }
            /*else
            {
                string[] args = obj.Message.Split(' ');
                args[0] = args[0].ToLower(); 
                IPrivateCommand command; 
                command = new CommandAsk();
                reply = command.RunPrivateCommand(Program.users, args, obj);
            }*/
            return reply;
        }

        internal void SaveData()
        {
            /*string commandsPath = System.IO.Path.Combine(Program.filePath, "privatecommands");
            if (Directory.Exists(commandsPath) == false)
            {
                Directory.CreateDirectory(commandsPath);
            }
            List<PrivateCommandsItem> tempdataitems = new List<PrivateCommandsItem>(commands.Count);
            foreach (string key in commands.Keys)
            {
                tempdataitems.Add(new PrivateCommandsItem(key, commands[key]));
            }
            //System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(List<DataItem>));
            using (System.IO.StreamWriter w = new System.IO.StreamWriter(commandsPath + ".xml"))
            {
                System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(List<PrivateCommandsItem>));
                xs.Serialize(w, tempdataitems);
            }*/
        }
        internal void ReadData()
        {
            /*string commandsPath = System.IO.Path.Combine(Program.filePath, "privatecommands");
            using (System.IO.StreamReader w = new System.IO.StreamReader(commandsPath + ".xml"))
            {
                System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(List<PrivateCommandsItem>));
                List<PrivateCommandsItem> tempdataitems = (List<PrivateCommandsItem>)xs.Deserialize(w);
                foreach (PrivateCommandsItem pci in tempdataitems)
                {
                    commands.Add(pci.trigger, pci.command);
                }
            }*/
        }
        [Serializable]
        public class PrivateCommandsItem
        {

            public string trigger;
            public IPrivateCommand command;

            public PrivateCommandsItem(string trigger, IPrivateCommand command)
            {
                this.trigger = trigger;
                this.command = command;
            }
            public PrivateCommandsItem()
            {

            }
        }

        
    }
}
