package content.global.skill.construction;


import core.api.Container;
import core.game.world.map.Location;
import org.rs09.consts.Items;
import core.game.node.entity.Entity;
import core.game.node.entity.player.Player;
import core.game.world.map.zone.MapZone;
import core.game.world.map.RegionManager;
import core.game.world.map.Region;
import core.game.system.task.Pulse;
import core.game.world.map.zone.ZoneType;

import static core.api.ContentAPIKt.*;

/**
 * Handles the player owned house zone.
 *
 * @author Emperor
 */
public final class HouseZone extends MapZone {

    /**
     * The house manager.
     */
    private HouseManager house;

    /**
     * The previous house region id.
     */
    private int previousRegion = -1;

    /**
     * The previous dungeon region id.
     */
    private int previousDungeon = -1;

    /**
     * Constructs the house zone object.
     */
    public HouseZone(HouseManager house) {
        super("poh-zone" + house, true);
        this.house = house;
    }

    @Override
    public void configure() {
        setZoneType(ZoneType.P_O_H.getId());
        unregisterOldRegions();
        registerRegion(house.getHouseRegion().getId());
        if (house.getDungeonRegion() != null) {
            registerRegion(house.getDungeonRegion().getId());
        }
    }

    private void unregisterOldRegions() {
        if (previousRegion != -1) {
            unregisterRegion(previousRegion);
        }
        if (previousDungeon != -1) {
            unregisterRegion(previousDungeon);
        }
    }

    @Override
    public boolean enter(Entity e) {
        if (e instanceof Player) {
            Player pl = (Player) e;
            pl.setAttribute("/save:lastActivity","POH-Activity");
            if (house == pl.getHouseManager()) {
                previousRegion = house.getHouseRegion().getId();
                if (house.getDungeonRegion() != null)
                    previousDungeon = house.getDungeonRegion().getId();
            }
            registerLogoutListener(pl, "houselogout", (p) -> {
                    p.setLocation(house.getLocation().getExitLocation());
                    p.removeAttribute("lastActivity");
                    return kotlin.Unit.INSTANCE;
            });
        }
        return super.enter(e);
    }

    @Override
    public boolean death(Entity e, Entity killer) {
        if (e instanceof Player) {
            Player p = (Player) e;
            HouseManager.leave(p);
	        return true;
        }
        return super.death(e, killer);
    }

    @Override
    public boolean leave(Entity e, boolean logout) {
        if (e instanceof Player) {
            Player p = (Player) e;

            // if the player is moving between the dungeon and main house, don't trigger the leave sequence
            if (house.getDungeonRegion() != null && !logout) {
                // current loc
                Location dest = p.getProperties().getTeleportLocation();
                int currentRegionId = p.getLocation().getRegionId();
                int houseId = house.getHouseRegion().getId();
                int dungeonId = house.getDungeonRegion().getId();

                // check if still in house
                boolean currentlyInHouse = (currentRegionId == houseId || currentRegionId == dungeonId);

                // check if moving between house regions
                int destRegionId = (dest != null) ? dest.getRegionId() : -1;
                boolean movingToHouse = (destRegionId == houseId || destRegionId == dungeonId);

                // if yes, return true and stop the leave sequence
                if (currentlyInHouse || movingToHouse) {
                    return true;
                }
            }

            remove_items(p);
            // The below tears down the house if the owner was the one who left
            if (house == p.getHouseManager()) {
                house.expelGuests(p);
                int toRemove = previousRegion;
                int dungRemove = previousDungeon;
                submitWorldPulse(new Pulse(1) {
                    public boolean pulse() {
                        Region r = RegionManager.forId(toRemove);
                        Region dr = dungRemove != -1 ? RegionManager.forId(dungRemove) : null;
                        RegionManager.removeRegion(toRemove);
                        unregisterRegion(toRemove);
                        r.flagInactive();
                        if (dungRemove != -1) {
                            RegionManager.removeRegion(dungRemove);
                            unregisterRegion(dungRemove);
                            dr.flagInactive();
                        }
                        return true;
                    }
                });
            }
            // Clear logout listener and original-loc (if appropriate)
            clearLogoutListener(p, "houselogout");
            if (!getAttribute(p, "kidnapped-by-random", false)) {
                removeAttribute(p, "/save:original-loc");
                removeAttribute(p,"lastActivity");
            }
            return true;
        }
        return true;
    }

    public static void remove_items(Player p) {
        for (int item = Items.KETTLE_7688; item <= Items.CHEFS_DELIGHT_7755; item++) {//Removes all PoH versions of tea and beer barrel-related items
            removeAll(p, item, Container.INVENTORY);
            removeAll(p, item, Container.BoB);
        }
    }
}
