import { getPredefineAttributes } from "./item_list";

export function setupMagicAttribute(container){
    if(container.getDynamicProperty("magic_property") != undefined) return;
    let data = {
        attribute_point: 0,
		soul_capacity: 0,
		fire_magic: 0,
		water_magic: 0,
		nature_magic: 0,
		ice_magic: 0,
		void_magic: 0,
		dark_magic: 0,
		light_magic: 0,
		wind_magic: 0,
		thunder_magic: 0
	}
    container.setDynamicProperty("magic_property", JSON.stringify(data));
};

export function getMagicAttributeData(container, only_item = true){
    let data = (container == undefined) ? undefined : container.getDynamicProperty("magic_property");
    if(data == undefined){
        data = {
            attribute_point: 0,
            soul_capacity: 0,
            fire_magic: 0,
            water_magic: 0,
            nature_magic: 0,
            ice_magic: 0,
            void_magic: 0,
            dark_magic: 0,
            light_magic: 0,
            wind_magic: 0,
            thunder_magic: 0
        }
    }else{
        data = JSON.parse(data);
    }

    if(container != undefined && !only_item && getPredefineAttributes()[container.typeId]){
        let predefiene = getPredefineAttributes()[container.typeId];
        for(let attribute of Object.keys(predefiene)){
            data[attribute] += predefiene[attribute];
        }
    }

    return data;
};

export function addAttributePoint(container, value = 1){
    let data = container.getDynamicProperty("magic_property");
    if(data == undefined){
        data = {
            attribute_point: 0,
            soul_capacity: 0,
            fire_magic: 0,
            water_magic: 0,
            nature_magic: 0,
            ice_magic: 0,
            void_magic: 0,
            dark_magic: 0,
            light_magic: 0,
            wind_magic: 0,
            thunder_magic: 0
        };
    }else{
        data = JSON.parse(data);
    }
    data.attribute_point += value;
    container.setDynamicProperty("magic_property", JSON.stringify(data));
};

export function addMagicAttribute(container, attribute_name, value = 1){
    let data = container.getDynamicProperty("magic_property");
    if(data == undefined){
        data = {
            attribute_point: 0,
            soul_capacity: 0,
            fire_magic: 0,
            water_magic: 0,
            nature_magic: 0,
            ice_magic: 0,
            void_magic: 0,
            dark_magic: 0,
            light_magic: 0,
            wind_magic: 0,
            thunder_magic: 0
        };
    }else{
        data = JSON.parse(data);
    }

    data[attribute_name] += value;
    
    container.setDynamicProperty("magic_property", JSON.stringify(data));
};