Macro.add('Achievement', {
	skipArgs : false,
	handler  : function () {
        try {
            let achievmentId = this.args[0];
            if(!(achievmentId in achievements))
                return this.error(`Achievement not found (ID: ${achievmentId})`);

            var achievementsInMemory = recall("achievements",{});
            if(achievmentId in achievementsInMemory && achievementsInMemory[achievmentId].completion == 1)
                return; //Achievement already gotten

            let achievement = achievements[achievmentId];
            let aim = achievementsInMemory[achievmentId] || {completion:0};

            if("required" in achievement){
                //This is a multi-part achievement
                if (this.args.length < 2)
                    this.error(`Part-ID missing for Achievement (ID: ${achievmentId})`);
                let achievementPart = this.args[1];

                if(!("parts" in aim))
                    aim.parts = [achievementPart];
                else if(aim.parts.contains(achievementPart))
                    return; //Achievement part already gotten
                else
                    aim.parts.push(achievementPart);

                achievementsInMemory[achievmentId] = aim;

                if(aim.parts.length < achievement.required){
                    memorize("achievements",achievementsInMemory);
                    return; //Not enough parts to get the achievement
                }
            }


            let label = achievement.label;
            let markup = `<<AchievementPresent "${label}">>`;

            achievementsInMemory[achievmentId]={completion:1,time:Date.now()};

            memorize("achievements",achievementsInMemory);

            jQuery(this.output).wiki(markup);
        }
		catch (ex) {
			return this.error('Error: ' + ex.message);
		}
    }
});
