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

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

export default class scoresSystem {
    constructor(entity,type) {
        this.dimension = dimensionSystem;
        this.Commands = Commands;
        this._player;
        this._entity;
        this._func = (entity,type) => {
            if (entity !== undefined) {
                if (type === "player") {
                    this.player = entity;
                } else if (type === "entity") {
                    this.entity = entity;
                }
            }
        };
        this._func(entity,type);
    }
    get player() {
        return this._player;
    }
    set player(value) {
        this._player = value;
    }
    get entity() {
        return this._entity;
    }
    set entity(value) {
        this._entity = value;
    }
    
    static create(...objectives) {
        let list = new Array(...objectives);
        let dimensions = dimensionSystem.get("all");
        for (let i of list) {
            for (let dimension of dimensions) {
                if (i.includes("->")) {
                    let obj = i.split("->")[0];
                    let name = i.split("->")[1];
                    Commands.run(`scoreboard objectives add ${obj} dummy ${name}`);
                } else {
                    Commands.run(`scoreboard objectives add ${i} dummy `);
                }
            }
        }
        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);
            })
        })
    }
    
    target(type,selector) {
        let _target;
        let _condition = (string) => {
            return string === undefined ? `` : `,${string}`;
        }
        if (this._player !== undefined && this._entity === undefined) {
            if (type === "selector") {
                _target = `@a[name="${this._player.nameTag}"${_condition(selector)}]`;
            } else if (type === "target" && selector === undefined) {
                _target = this._player;
            }
        } 
        if (this._player === undefined && this._entity !== undefined) {
            if (type === "selector") {
                _target = `@e[type=${this._entity.typeId}${_condition(selector)}]`;
            } else if (type === "target" && selector === undefined) {
                _target = this._entity;
            }
        }
        return _target;
    }
    
    scoreboard(list, ...args) {
        let a = new Array(...args);
        let _scoreboard = (list, selector, objectives, value) => {
            return this.Commands.run(`scoreboard players ${list} ${this.target("selector",selector)} ${objectives} ${value}`, this.target("target"));
        }
        if (list === "get") {
            return parseFloat(this.Commands.run(`scoreboard players test ${this.target("selector",undefined)} ${a[0]} * *`, this.target("target")).statusMessage.split(" ")[1]);
        } 
        if (list === "getV2") {
            let entityScore = this.target("target").scoreboard;
            return mc.world.scoreboard.getObjective(a[0]).getScore(entityScore);
        } else {
            if (a[2] === undefined) {
                return _scoreboard(list, a[2], a[0], a[1]);
            } else {
                return _scoreboard(list, a[1], a[0], a[2]);
            }
        }
    }
    
    get(...args) {
        return this.scoreboard("getV2", ...args);
    }
    set(...args) {
        return this.scoreboard("set", ...args);
    }
    add(...args) {
        return this.scoreboard("add", ...args);
    }
    remove(...args) {
        return this.scoreboard("remove", ...args);
    }
    reset(...args) {
        return this.scoreboard("reset", ...args);
    }
    
}
