/*
 * Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @author GeyserMC
 * @link https://github.com/GeyserMC/Geyser
 */

package org.geysermc.geyser.session.cache;

import lombok.AllArgsConstructor;
import net.kyori.adventure.text.Component;
import org.cloudburstmc.math.vector.Vector2f;
import org.cloudburstmc.math.vector.Vector3f;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityDataMap;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityDataTypes;
import org.cloudburstmc.protocol.bedrock.packet.AddEntityPacket;
import org.cloudburstmc.protocol.bedrock.packet.BossEventPacket;
import org.cloudburstmc.protocol.bedrock.packet.RemoveEntityPacket;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.text.MessageTranslator;

@AllArgsConstructor
public class BossBar {
    private final GeyserSession session;

    private final long entityId;
    private Component title;
    private float health;
    private int color;
    private final int overlay;
    private final int darkenSky;

    public void addBossBar() {
        addBossEntity();
        updateBossBar();
    }

    //TODO: There is a player unique entity ID - if this didn't exist before, we may be able to get rid of our hack

    public void updateBossBar() {
        BossEventPacket bossEventPacket = new BossEventPacket();
        bossEventPacket.setBossUniqueEntityId(entityId);
        bossEventPacket.setAction(BossEventPacket.Action.CREATE);
        bossEventPacket.setTitle(bossbarTitle(title));
        bossEventPacket.setHealthPercentage(health);
        bossEventPacket.setColor(color);
        bossEventPacket.setOverlay(overlay);
        bossEventPacket.setDarkenSky(darkenSky);

        session.sendUpstreamPacket(bossEventPacket);
    }

    public void updateTitle(Component title) {
        this.title = title;
        BossEventPacket bossEventPacket = new BossEventPacket();
        bossEventPacket.setBossUniqueEntityId(entityId);
        bossEventPacket.setAction(BossEventPacket.Action.UPDATE_NAME);
        bossEventPacket.setTitle(bossbarTitle(title));

        session.sendUpstreamPacket(bossEventPacket);
    }

    private String bossbarTitle(Component title) {
        String message = MessageTranslator.convertMessage(title, session.locale()).replace("%", "%%%%");

        if (message.length() > 256) {
            GeyserImpl.getInstance().getLogger().debug(session, "Bossbar with a length higher than 256! Limiting length.");
            message = message.substring(0, 253) + "...";
        }

        return message;
    }

    public void updateHealth(float health) {
        this.health = health;
        BossEventPacket bossEventPacket = new BossEventPacket();
        bossEventPacket.setBossUniqueEntityId(entityId);
        bossEventPacket.setAction(BossEventPacket.Action.UPDATE_PERCENTAGE);
        bossEventPacket.setHealthPercentage(health);

        session.sendUpstreamPacket(bossEventPacket);
    }

    public void updateColor(int color) {
        this.color = color;
        BossEventPacket bossEventPacket = new BossEventPacket();
        bossEventPacket.setBossUniqueEntityId(entityId);
        bossEventPacket.setAction(BossEventPacket.Action.UPDATE_STYLE);
        bossEventPacket.setColor(color);

        session.sendUpstreamPacket(bossEventPacket);
    }

    public void removeBossBar() {
        BossEventPacket bossEventPacket = new BossEventPacket();
        bossEventPacket.setBossUniqueEntityId(entityId);
        bossEventPacket.setAction(BossEventPacket.Action.REMOVE);

        session.sendUpstreamPacket(bossEventPacket);
        removeBossEntity();
    }

    /**
     * Bedrock still needs an entity to display the BossBar.<br>
     * Just like 1.8 but it doesn't care about which entity
     */
    private void addBossEntity() {
        AddEntityPacket addEntityPacket = new AddEntityPacket();
        addEntityPacket.setUniqueEntityId(entityId);
        addEntityPacket.setRuntimeEntityId(entityId);
        addEntityPacket.setIdentifier("minecraft:creeper");
        addEntityPacket.setEntityType(33);
        addEntityPacket.setPosition(session.getPlayerEntity().position().down(10));
        addEntityPacket.setRotation(Vector2f.ZERO);
        addEntityPacket.setMotion(Vector3f.ZERO);
        EntityDataMap metadata = addEntityPacket.getMetadata();
        metadata.put(EntityDataTypes.SCALE, 0F);
        metadata.put(EntityDataTypes.WIDTH, 0F);
        metadata.put(EntityDataTypes.HEIGHT, 0F);

        session.sendUpstreamPacket(addEntityPacket);
    }

    private void removeBossEntity() {
        RemoveEntityPacket removeEntityPacket = new RemoveEntityPacket();
        removeEntityPacket.setUniqueEntityId(entityId);

        session.sendUpstreamPacket(removeEntityPacket);
    }
}
