package ru.redguy.qseller;

import org.bukkit.Material;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.SerializableAs;

import java.util.LinkedHashMap;
import java.util.Map;

@SerializableAs("SellItem")
public class SellItem implements Cloneable, ConfigurationSerializable {

    Material item = Material.AIR;
    short meta = 0;
    double basePrice = 0.0;
    double highPrice = 0.0;

    public SellItem(String item, short meta, double basePrice, double highPrice) {
        this.item = Material.valueOf(item);
        this.meta = meta;
        this.basePrice = basePrice;
        this.highPrice = highPrice;
    }

    public SellItem(Material material, short meta, double basePrice, double highPrice) {
        this.item = material;
        this.meta = meta;
        this.basePrice = basePrice;
        this.highPrice = highPrice;
    }

    public SellItem(Material material) {
        this.item = material;
    }

    public Material getItem() {
        return item;
    }

    public void setItem(Material item) {
        this.item = item;
    }

    public short getMeta() {
        return meta;
    }

    public void setMeta(short meta) {
        this.meta = meta;
    }

    public double getBasePrice() {
        return basePrice;
    }

    public void setBasePrice(double basePrice) {
        this.basePrice = basePrice;
    }

    public double getHighPrice() {
        return highPrice;
    }

    public void setHighPrice(double highPrice) {
        this.highPrice = highPrice;
    }

    public Map<String, Object> serialize() {
        LinkedHashMap<String,Object> result = new LinkedHashMap<>();
        result.put("item", this.getItem().name());
        result.put("meta", this.getMeta());
        result.put("basePrice", this.getBasePrice());
        result.put("highPrice", this.getHighPrice());
        return result;
    }

    public static SellItem deserialize(Map<String, Object> args) {
        Material item = Material.AIR;
        short meta = 0;
        double basePrice = 0.0D;
        double highPrice = 0.0D;

        if(args.containsKey("item")) {
            item = Material.valueOf(String.valueOf(args.get("item")));
        }
        if(args.containsKey("meta")) {
            meta = Short.parseShort(String.valueOf(args.get("meta")));
        }
        if(args.containsKey("basePrice")) {
            basePrice = Double.parseDouble(String.valueOf(args.get("basePrice")));
        }
        if(args.containsKey("highPrice")) {
            highPrice = Double.parseDouble(String.valueOf(args.get("highPrice")));
        }

        return new SellItem(item,meta,basePrice,highPrice);
    }
}