/*
            This file is part of the OdinMS Maple Story Server
    Copyright (C) 2008 ~ 2010 Patrick Huy <patrick.huy@frz.cc>
                       Matthias Butz <matze@odinms.de>
                       Jan Christian Meyer <vimes@odinms.de>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License version 3
    as published by the Free Software Foundation. You may not use, modify
    or distribute this program under any other version of the
    GNU Affero General Public License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package net.sf.odinms.net.channel.handler;

import java.rmi.RemoteException;
import java.util.Collection;
import net.sf.odinms.client.MapleCharacter;
import net.sf.odinms.client.MapleClient;
import net.sf.odinms.net.channel.ChannelServer;
import net.sf.odinms.net.world.MapleParty;
import net.sf.odinms.net.world.MaplePartyCharacter;
import net.sf.odinms.net.world.PartyOperation;
import net.sf.odinms.net.world.remote.WorldChannelInterface;
import net.sf.odinms.server.maps.MapleMap;
import net.sf.odinms.server.maps.MapleMapObject;
import net.sf.odinms.tools.MaplePacketCreator;
import net.sf.odinms.tools.data.input.SeekableLittleEndianAccessor;

/**
 *
 * @author Admin
 */
public class PartyHandler {
public static final void DenyPartyRequest(final SeekableLittleEndianAccessor slea, final MapleClient c) {
	final MapleCharacter cfrom = c.getChannelServer().getPlayerStorage().getCharacterByName(slea.readMapleAsciiString());
	final String to = slea.readMapleAsciiString();

	if (cfrom != null) {
	    cfrom.getClient().getSession().write(MaplePacketCreator.partyStatusMessage(23, c.getPlayer().getName()));
	}
    }

    public static final void PartyOperatopn(final SeekableLittleEndianAccessor slea, final MapleClient c) {
	final int operation = slea.readByte();
	final WorldChannelInterface wci = ChannelServer.getInstance(c.getChannel()).getWorldInterface();
	MapleParty party = c.getPlayer().getParty();
	MaplePartyCharacter partyplayer = new MaplePartyCharacter(c.getPlayer());

	switch (operation) {
	    case 1: // create
		if (c.getPlayer().getParty() == null) {
		    try {
			party = wci.createParty(partyplayer);
			c.getPlayer().setParty(party);
		    } catch (RemoteException e) {
			c.getChannelServer().reconnectWorld();
		    }
		    c.getSession().write(MaplePacketCreator.partyCreated());
		} else {
		    c.getPlayer().dropMessage(5, "You can't create a party as you are already in one");
		}
		break;
	    case 2: // leave
		if (party != null) { //are we in a party? o.O"
		    try {
			if (partyplayer.equals(party.getLeader())) { // disband
			    wci.updateParty(party.getId(), PartyOperation.DISBAND, partyplayer);
			    if (c.getPlayer().getEventInstance() != null) {
				c.getPlayer().getEventInstance().disbandParty();
			    }
			} else {
			    wci.updateParty(party.getId(), PartyOperation.LEAVE, partyplayer);
			    if (c.getPlayer().getEventInstance() != null) {
				c.getPlayer().getEventInstance().leftParty(c.getPlayer());
			    }
			}
		    } catch (RemoteException e) {
			c.getChannelServer().reconnectWorld();
		    }
		    c.getPlayer().setParty(null);
		}

    }
}

