import * as mc from '@minecraft/server';

import Commands from "../commands/runCommand.js";
import dimensionSystem from "../dimension/dimensionSystem.js";

export default class scoresSystem {
    constructor(entity,score) {
        this.entity = entity;
        this.score = score;
        this.dimension = dimensionSystem;
        this.Commands = Commands;
    }
    
    static create(entity,...objectives) {
        let list = new Array(...objectives);
        for (let i of list) {
            if (i.includes("->")) {
                let obj = i.split("->")[0];
                let name = i.split("->")[1];
                try {
                    mc.world.scoreboard.addObjective(obj,name);
                } catch(e) {
                    /*
                    let getObjective = mc.world.scoreboard.getObjective(obj);
                    if (entity?.scoreboard !== undefined && entity?.scoreboard !== null) {
                        let getScore = getObjective.getScore(entity?.scoreboard);
                        if (getScore !== undefined && getScore !== null) {
                            mc.world.scoreboard.setScore(getObjective,entity?.scoreboard,0)
                        }
                    }*/
                }
            }/* else {
                try {
                    mc.world.scoreboard.addObjective(i);
                } catch(e) {}
            }*/
        }
        return list;
    }
    
    static setdisplay(...objectives) {
        let list = new Array(...objectives);
        list.forEach(value => {
            let objective = value.split("/")[0];
            let hud_pos = value.split("/")[1];
            let rank = value.split("/")[2];
            let all = (hud_pos === "belowname" ? `${hud_pos} ${objective}` : `${hud_pos} ${objective} ${rank}`);
            let dimension = dimensionSystem;
            dimension.get("all").forEach(dimension => {
                Commands.run(`scoreboard objectives setdisplay ${all}`, dimension).catch(e => {});
            })
        })
    }
    
    
    set var(value) {
        mc.world.scoreboard.getObjective(this.score).setScore(this.entity?.scoreboardIdentity,value);
    }
    
    get var() {
        return mc.world.scoreboard.getObjective(this.score).getScore(this.entity?.scoreboardIdentity);
    }
    
}
