package net.cobem.grizzly.habbohotel.roleplay.commands;

import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.communication.composers.rooms.SendRoomShoutComposer;
import net.cobem.grizzly.communication.composers.rooms.SendRoomWhisperComposer;
import net.cobem.grizzly.habbohotel.effects.EffectType;
import net.cobem.grizzly.habbohotel.misc.ChatCommand;
import net.cobem.grizzly.habbohotel.sessions.Session;

public class Hit implements ChatCommand {
    @Override
    public int rank() {
        return 0;
    }

    @Override
    public void execute(Session session, String[] args) {
        if (args.length != 1) {
            session.sendResponse(SendRoomWhisperComposer.compose(session.getHabbo().getID(), "Invalid number of arguments!", 0));
            return;
        }

        Session Target = Grizzly.getHabboHotel().getSessions().getByName(args[0]);

        if (Target == null) {
            session.sendResponse(SendRoomWhisperComposer.compose(session.getHabbo().getID(), "Can't find " + Target, 0));
            return;
        }

        if (!Target.getActor().CurrentRoom.equals(session.getActor().CurrentRoom)) {
            session.sendResponse(SendRoomWhisperComposer.compose(session.getHabbo().getID(), "You can't cross-room hit someone!", 0));
            return;
        }

        if (Target.equals(session)) {
            session.sendResponse(SendRoomWhisperComposer.compose(session.getHabbo().getID(), "You're a loser..!", 0));
            return;
        }

        if (Target.getRoleplay().isLeaving()) {
            session.sendResponse(SendRoomWhisperComposer.compose(session.getHabbo().getID(), args[0] + " is signing off..", 0));
            return;
        }

        if (!Target.getActor().CurrentPosition.TilesTouching(session.getActor().CurrentPosition)) {
            String Text = "Tries to punch " + args[0] + ", but misses!";
            session.getActor().CurrentRoom.sendMessage(SendRoomShoutComposer.compose(session.getHabbo().getID(), Text, 0, 22));
            return;
        }

        if (Grizzly.rand(0, 1) == 1) //Don't need an exhaustion check, just roll with the flow.
        {
            int Hit = session.getRoleplay().getStrength() * (Grizzly.rand(1, 3) + Grizzly.rand(1, 13));

            Grizzly.write(session.getRoleplay().getStrength());
            Grizzly.write(Hit);
            Grizzly.write(Target.getRoleplay().getHealth());

            if ((Target.getRoleplay().getHealth() - Hit) <= 0) //They died, yay.
            {
                String Text = "Knocks " + args[0] + " out, sending them to the hospital..";
                session.getActor().CurrentRoom.sendMessage(SendRoomShoutComposer.compose(session.getHabbo().getID(), Text, 0, 22));

                Target.getRoleplay().setHealth(100);
                session.getRoleplay().upgradeStrength(1);
            } else {
                String Text = "Punches " + args[0] + " for " + Hit + " damage!";
                session.getActor().CurrentRoom.sendMessage(SendRoomShoutComposer.compose(session.getHabbo().getID(), Text, 0, 22));

                Target.getRoleplay().decreaseHealth(Hit);

                if (Target.getRoleplay().getHealth() <= 25) //They're low on health..
                {
                    Target.effect(EffectType.BIRDIES_OVER_HEAD);
                    Target.sendResponse(SendRoomWhisperComposer.compose(Target.getHabbo().getID(), "You're low on health, get to a hospital!!", 0));
                }
            }
        } else {
            String Text = "Tries to punch " + args[0] + ", but misses!";
            session.getActor().CurrentRoom.sendMessage(SendRoomShoutComposer.compose(session.getHabbo().getID(), Text, 0, 22));
            return;
        }
    }
}
