import * as mc from "@minecraft/server";
import range from "../../../modules/arrayRange.js";
import Mathf from '../../../modules/Mathf.js';
import registerSystem from "../../../register/registerSystem.js";

export default class standMaster_health extends registerSystem {
    constructor() {
        super();
        this.icon_size = 5;
        this.max_health = 200;
        this.default_icons = "".split("");
        this.rpg_icons = "".split("");
        this.hurt_icons = "".split("");
    }
    
    
    events() {
        this.tick(() => {
            this.getPlayers().forEach(player => {
                let shealth_max = this.GlobalVar(player,"standMaster.health_max");
                shealth_max.var = this.max_health;
                let shealth = this.GlobalVar(player,"standMaster.health");
                shealth.var = Mathf.clamp(shealth.var,0,shealth_max.var);
                // console.error(shealth.var)
            })
        },1);
        this.tick(() => {
            this.getPlayers().forEach(player => {
                let shealth = this.GlobalVar(player,"standMaster.health");
                let shealth_max = this.GlobalVar(player,"standMaster.health_max");
                shealth.var = Mathf.clamp(shealth.var + 1, 0, shealth_max.var);
            })
        },260);
        this.listenForEvent("minecraft:entityHurt", (eventData) => {
            let { hurtEntity } = eventData;
            if (hurtEntity?.typeId == "minecraft:player") {
                let player = hurtEntity;
                let delay_tick = this.GlobalVar(player,"delay_tick");
                let instant_health = player?.getEffect("instant_health");
                let damage = (delay_tick.var >= 0) ? 0 : eventData.damage;
                let shealth = this.GlobalVar(player,"standMaster.health");
                let shealth_max = this.GlobalVar(player,"standMaster.health_max");
                if (player?.getTags().join("|").includes("standMaster.")) {
                    player.addTag("isHurt")
                    shealth.var = Mathf.clamp(shealth.var - damage, 0, shealth_max.var);
                }
            }
        });
    }
    
    loadingUI(player, health, max_health, type) {
        /*
        return [
            this.progress_bar(health,max_health,20,29),
            this.progress_bar(health,max_health,10,19),
            this.progress_bar(health,max_health,0,9)
        ].join("\n")
        */
        
        let progress_bar = (player, health, max_health, min, max) => {
            let heart_icons = () => {
                if (type == "rpg") {
                    return this.rpg_icons;
                }
                if (type == "vanilla") {
                    return player?.hasTag("isHurt") ? this.hurt_icons : this.default_icons;
                }
            }
            let icons = heart_icons();
            let count = range(1, max_health / this.icon_size).map(() => {
                return icons;
            })
            let result = count.map((icon,index) => {
                let icon_frame = icons.length - 1;
                let icon_count = index + 1;
                let value = icon[Mathf.clamp(icon_count * icon_frame - health, 0, icon_frame)] 
                return index >= min && index <= max ? value : "";
            });
            return result.join("");
        }
        
        return range(0, max_health / (this.icon_size * 10)).map(value => {
            let min = value * 10;
            let max = value * 10 + 9;
            return progress_bar(player, health, max_health, min, max);
        }).reverse().join("\n")
        
    }
    
}