/*
    This file is a part of the SteamWar software.

    Copyright (C) 2020  SteamWar.de-Serverteam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

package de.steamwar.velocitycore.inventory;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import de.steamwar.messages.Message;
import de.steamwar.messages.Chatter;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

@Setter
public class SWItem {

    @Getter
    private String material = "DYE";
    private Message title;
    private String skullOwner;
    private boolean enchanted;
    private boolean hideAttributes;
    private List<Message> lore = new ArrayList<>();
    @Getter
    private InvCallback callback;
    private int color = 0;

    public SWItem(String material, Message title) {
        this.material = material.toUpperCase();
        this.title = title;
    }

    public SWItem(Message title, int color) {
        this.title = title;
        this.color = color;
    }

    public static SWItem getSkull(String skullOwner) {
        SWItem item = new SWItem("SKULL", new Message("PLAIN_STRING", skullOwner));
        item.setSkullOwner(skullOwner);
        return item;
    }

    public SWItem addLore(Message lore) {
        this.lore.add(lore);
        return this;
    }

    public JsonObject writeToString(Chatter player, int position) {
        JsonObject object = new JsonObject();
        object.addProperty("material", material);
        object.addProperty("position", position);
        object.addProperty("title", player.parseToLegacy(title));
        if(skullOwner != null)
            object.addProperty("skullOwner", skullOwner);
        if(enchanted)
            object.addProperty("enchanted", true);
        if(hideAttributes)
            object.addProperty("hideAttributes", true);
        if(color != 0)
            object.addProperty("color", color);
        if(lore != null) {
            JsonArray array = new JsonArray();
            for (Message lores : lore) {
                array.add(player.parseToLegacy(lores));
            }
            object.add("lore", array);
        }

        return object;
    }
}
