/*
    Copyright 2011 MCForge
        
    Dual-licensed under the Educational Community License, Version 2.0 and
    the GNU General Public License, Version 3 (the "Licenses"); you may
    not use this file except in compliance with the Licenses. You may
    obtain a copy of the Licenses at
    
    http://www.opensource.org/licenses/ecl2.php
    http://www.gnu.org/licenses/gpl-3.0.html
    
    Unless required by applicable law or agreed to in writing,
    software distributed under the Licenses are distributed on an "AS IS"
    BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
    or implied. See the Licenses for the specific language governing
    permissions and limitations under the Licenses.
 */
using System;
namespace MCGalaxy.Commands {
    public sealed class CmdReload : Command {
        public override string name { get { return "reload"; } }
        public override string shortcut { get { return "reveal"; } }
        public override string type { get { return CommandTypes.World; } }
        public override bool museumUsable { get { return false; } }
        public override LevelPermission defaultRank { get { return LevelPermission.Guest; } }
        public override CommandAlias[] Aliases {
            get { return new [] { new CommandAlias("rejoin"), new CommandAlias("rd") }; }
        }
        public override CommandPerm[] AdditionalPerms {
            get { return new[] { new CommandPerm(LevelPermission.Operator, "+ can reload for other players") }; }
        }

        public override void Use(Player p, string message) {
            if (CheckSuper(p, message, "player or level name")) return;
            if (message == "") message = p.name;
            string[] parts = message.ToLower().Split(' ');
            
            if (parts[0] == "all") {
                if (!ReloadAll(p, parts)) return;
            } else {
                Player who = PlayerInfo.FindMatches(p, parts[0]);
                if (who == null) return;
                if (p != null && who.group.Permission > p.group.Permission) {
                    MessageTooHighRank(p, "reload the map for", true); return;
                }
                ReloadMap(p, who, true);
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        
        bool ReloadAll(Player p, string[] parts) {
            Level lvl = null;
            if (parts.Length == 2) {
                lvl = LevelInfo.Find(parts[1]);
                if (lvl == null) return false;
            } else if (!Player.IsSuper(p)) {
                lvl = p.level;
            } else {
                SuperRequiresArgs(name + " all", p, "level name"); return false;
            }
            
            if (!CheckExtraPerm(p)) { MessageNeedPerms(p, "can reload all players in a map."); return false; }
            Player[] players = PlayerInfo.Online.Items;
            foreach (Player who in players) {
                if (who.level == lvl)
                    ReloadMap(p, who, true);
            }
            return true;
        }
        
        internal static void ReloadMap(Player p, Player who, bool showMessage) {
            who.Loading = true;
            Entities.DespawnEntities(who);
            who.SendUserMOTD(); who.SendMap(who.level);
            Entities.SpawnEntities(who);
            who.Loading = false;

            if (!showMessage) return;
            if (p != null && !p.hidden) { who.SendMessage("&bMap reloaded by " + p.name); }
            if (p != null && p.hidden) { who.SendMessage("&bMap reloaded"); }
            Player.Message(p, "&4Finished reloading for " + who.name);
        }
        
        public override void Help(Player p) {
            Player.Message(p, "%T/reload %H- Reloads the map you are in, just for you.");
            Player.Message(p, "%T/reload <name> %H- Reloads the map for [name].");
            Player.Message(p, "%T/reload all %H- Reloads for all players in map you are in.");
            Player.Message(p, "%T/reload all [map] %H- Reloads for all players in [map]");
        }
    }
}
