/*
 * The MIT License
 * Copyright © 2013 Richard Harrah
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.tealcube.minecraft.bukkit.mythicdrops.repair;

import com.tealcube.minecraft.bukkit.mythicdrops.api.repair.RepairCost;
import com.tealcube.minecraft.bukkit.mythicdrops.api.repair.RepairItem;
import com.tealcube.minecraft.bukkit.mythicdrops.items.ItemStackExtensionsKt;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData;

import java.util.*;

public final class MythicRepairItem implements RepairItem {

  private final String name;
  private final Material material;
  private final String itemName;
  private final List<String> itemLore;
  private final Map<String, RepairCost> repairCostMap;

  @Deprecated
  public MythicRepairItem(
      String name, MaterialData materialData, String itemName, List<String> itemLore) {
    this(name, materialData.getItemType(), itemName, itemLore);
  }

  public MythicRepairItem(String name, Material material, String itemName, List<String> itemLore) {
    this.name = name;
    this.material = material;
    this.itemName = itemName;
    this.itemLore = itemLore;
    repairCostMap = new HashMap<>();
  }

  @Override
  public String getName() {
    return name;
  }

  @Override
  public Material getMaterial() {
    return material;
  }

  @Override
  public String getItemName() {
    return itemName;
  }

  @Override
  public List<String> getItemLore() {
    return itemLore;
  }

  @Override
  public List<RepairCost> getRepairCosts() {
    return new ArrayList<>(repairCostMap.values());
  }

  @Override
  public MythicRepairItem addRepairCosts(MythicRepairCost... mythicRepairCosts) {
    for (MythicRepairCost rc : mythicRepairCosts) {
      repairCostMap.put(rc.getName(), rc);
    }
    return this;
  }

  @Override
  public MythicRepairItem removeRepairCosts(String... names) {
    for (String s : names) {
      repairCostMap.remove(s);
    }
    return this;
  }

  @Override
  public ItemStack toItemStack(int amount) {
    ItemStack itemStack = new ItemStack(material, amount);
    ItemStackExtensionsKt.setDisplayNameChatColorized(
        itemStack, (itemName == null || itemName.isEmpty()) ? null : itemName);
    ItemStackExtensionsKt.setLoreChatColorized(
        itemStack, (itemLore == null || itemLore.isEmpty()) ? Collections.emptyList() : itemLore);
    return itemStack;
  }

  @Override
  public int hashCode() {
    int result = name != null ? name.hashCode() : 0;
    result = 31 * result + (material != null ? material.hashCode() : 0);
    result = 31 * result + (itemName != null ? itemName.hashCode() : 0);
    result = 31 * result + (itemLore != null ? itemLore.hashCode() : 0);
    return result;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    MythicRepairItem that = (MythicRepairItem) o;

    return !(itemLore != null ? !itemLore.equals(that.itemLore) : that.itemLore != null)
        && !(itemName != null ? !itemName.equals(that.itemName) : that.itemName != null)
        && material == that.material
        && !(name != null ? !name.equals(that.name) : that.name != null);
  }
}

@@ -3135,16 +3135,69 @@
 mount);%0A
+    if (itemName != null && !itemName.isEmpty()) %7B%0A  
     Item
@@ -3238,33 +3238,24 @@
 atColorized(
-%0A        
 itemStack, (
@@ -3253,25 +3253,24 @@
 mStack, 
-(
 itemName
  == null
@@ -3265,59 +3265,16 @@
 Name
- == null %7C%7C itemName.isEmpty()) ? null : itemName);
+);%0A    %7D
 %0A   

fix bug with repair costs not creating itemstacks correctly