/*
    This file is part of the HeavenMS MapleStory Server
    Copyleft (L) 2016 - 2019 RonanLana

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation 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 client.processor.action;    // thanks Alex for pointing out some package structures containing broad modules

import client.Character;
import client.Client;
import server.maps.MapleMap;
import tools.PacketCreator;

/**
 * @author RonanLana
 */
public class BuybackProcessor {

    public static void processBuyback(Client c) {
        Character chr = c.getPlayer();
        boolean buyback;

        c.lockClient();
        try {
            buyback = !chr.isAlive() && chr.couldBuyback();
        } finally {
            c.unlockClient();
        }

        if (buyback) {
            String jobString;
            switch (chr.getJobStyle()) {
                case WARRIOR:
                    jobString = "warrior";
                    break;

                case MAGICIAN:
                    jobString = "magician";
                    break;

                case BOWMAN:
                    jobString = "bowman";
                    break;

                case THIEF:
                    jobString = "thief";
                    break;

                case BRAWLER:
                case GUNSLINGER:
                    jobString = "pirate";
                    break;

                default:
                    jobString = "beginner";
            }

            chr.healHpMp();
            chr.purgeDebuffs();
            chr.broadcastStance(chr.isFacingLeft() ? 5 : 4);

            MapleMap map = chr.getMap();
            map.broadcastMessage(PacketCreator.playSound("Buyback/" + jobString));
            map.broadcastMessage(PacketCreator.earnTitleMessage(chr.getName() + " just bought back into the game!"));

            chr.sendPacket(PacketCreator.showBuybackEffect());
            map.broadcastMessage(chr, PacketCreator.showForeignBuybackEffect(chr.getId()), false);
        }
    }
}
