// • ▌ ▄ ·.  ▄▄▄·  ▄▄ • ▪   ▄▄· ▄▄▄▄·  ▄▄▄·  ▐▄▄▄  ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀  █▪▀▀▀ ▀  ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀  ▀  ▀ ▀▀  █▪ ▀▀▀
//      Magicbane Emulator Project © 2013 - 2022
//                www.magicbane.com

package engine.objects;

import engine.Enum;
import engine.math.Vector3fImmutable;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import java.util.EnumSet;
import java.util.HashMap;


public class ItemTemplate {

    // Global template lookup
    public static HashMap<Integer, ItemTemplate> itemTemplates = new HashMap<>();

    // Template Properties
    public String obj_name;
    public Vector3fImmutable obj_scale;
    public int obj_render_object;
    public int obj_icon;
    public float combat_health_current;
    public float combat_health_full;
    public HashMap<String, Float> combat_attack_resist = new HashMap<>();
    public Enum.ItemType item_type;
    public int item_eq_slots_value;
    public boolean item_eq_slots_type;
    public EnumSet<Enum.ItemEquipSlotType> item_eq_slots_or = EnumSet.noneOf(Enum.ItemEquipSlotType.class);
    public EnumSet<Enum.ItemEquipSlotType> item_eq_slots_and = EnumSet.noneOf(Enum.ItemEquipSlotType.class);
    public int item_value;
    public int item_wt;
    public float item_passive_defense_mod;
    public String item_base_name;
    public String item_dsc;
    public int item_render_object_female;
    public float item_health_full;

    public EnumSet<Enum.CharacterSkills> item_skill_used = EnumSet.noneOf(Enum.CharacterSkills.class);

    public ItemTemplate(JSONObject jsonObject) {

        // Reading a String

        obj_name = (String) jsonObject.get("obj_name");

        // Reading floats from an array

        JSONArray scaleData = (JSONArray) jsonObject.get("obj_scale");
        obj_scale = new Vector3fImmutable(((Double) scaleData.get(0)).floatValue(), ((Double) scaleData.get(1)).floatValue(),
                ((Double) scaleData.get(2)).floatValue());

        // Reading an integer value

        obj_render_object = ((Long) jsonObject.get("obj_render_object")).intValue();
        obj_icon = ((Long) jsonObject.get("obj_icon")).intValue();

        // Reading float values

        combat_health_current = ((Double) jsonObject.get("combat_health_current")).floatValue();
        combat_health_full = ((Double) jsonObject.get("combat_health_full")).floatValue();

        // Reading a hashmap of floats

        JSONObject resist_json = (JSONObject) jsonObject.get("combat_attack_resist");

        for (Object key : resist_json.keySet()) {
            float resist = ((Double) resist_json.get(key)).floatValue();
            combat_attack_resist.put((String) key, resist);
        }

        // Parsing an enum

        item_type = Enum.ItemType.valueOf((String) jsonObject.get("item_type"));

        item_eq_slots_value = ((Long) jsonObject.get("item_eq_slots_value")).intValue();
        item_eq_slots_type = (boolean) jsonObject.get("item_eq_slots_type");

        // Parsing an enumset

        JSONArray eq_slots_or = (JSONArray) jsonObject.get("item_eq_slots_or");

        if (eq_slots_or.isEmpty() == false)
            for (Object o : eq_slots_or)
                item_eq_slots_or.add(Enum.ItemEquipSlotType.valueOf((String) o));

        JSONArray eq_slots_and = (JSONArray) jsonObject.get("eq_slots_and");

        if (eq_slots_and.isEmpty() == false)
            for (Object o : eq_slots_or)
                item_eq_slots_and.add(Enum.ItemEquipSlotType.valueOf((String) o));

        item_value = ((Long) jsonObject.get("item_value")).intValue();
        item_wt = ((Long) jsonObject.get("item_wt")).intValue();
        item_passive_defense_mod = ((Double) jsonObject.get("item_passive_defense_mod")).floatValue();
        item_base_name = (String) jsonObject.get("item_base_name");
        item_dsc = (String) jsonObject.get("item_dsc");
        item_render_object_female = ((Long) jsonObject.get("item_render_object_female")).intValue();
        item_health_full = ((Double) jsonObject.get("item_health_full")).floatValue();

        JSONArray skills_used = (JSONArray) jsonObject.get("item_skill_used");

        if (skills_used.isEmpty() == false)
            for (Object o : eq_slots_or)
                item_skill_used.add(Enum.CharacterSkills.valueOf((String) o));
    }
}