/*
 * This file is part of GenericsLib for Bukkit, licensed under the MIT License (MIT).
 *
 * Copyright (c) JCThePants (www.jcwhatever.com)
 *
 * 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.jcwhatever.bukkit.generic.items.serializer.metahandlers;

import com.jcwhatever.bukkit.generic.items.ItemStackHelper;
import com.jcwhatever.bukkit.generic.utils.PreCon;
import com.jcwhatever.bukkit.generic.utils.TextUtils;

import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Handles {@code ItemStack} lore meta.
 */
public class LoreHandler implements MetaHandler {

    private static final Pattern PATTERN_ESCAPED_PIPE = Pattern.compile("\\|");

    @Override
    public String getMetaName() {
        return "lore";
    }

    @Override
    public boolean canHandle(ItemStack itemStack) {
        PreCon.notNull(itemStack);

        ItemMeta meta = itemStack.getItemMeta();
        return meta != null && meta.hasLore();
    }

    @Override
    public boolean apply(ItemStack itemStack, ItemMetaObject meta) {
        PreCon.notNull(itemStack);
        PreCon.notNull(meta);

        if (!meta.getName().equals(getMetaName()))
            return false;

        String[] lineArray = TextUtils.PATTERN_PIPE.split(meta.getRawData());

        List<String> lines = new ArrayList<>(lineArray.length);

        for (String line : lineArray) {
            Matcher matcher = PATTERN_ESCAPED_PIPE.matcher(line);
            lines.add(matcher.replaceAll("|"));
        }

        ItemStackHelper.setLore(itemStack, lines);

        return true;
    }

    @Override
    public List<ItemMetaObject> getMeta(ItemStack itemStack) {
        PreCon.notNull(itemStack);

        ItemMeta meta = itemStack.getItemMeta();
        if (meta == null)
            return new ArrayList<>(0);

        List<String> lore = meta.getLore();
        if (lore == null || lore.isEmpty())
            return new ArrayList<>(0);

        List<ItemMetaObject> result = new ArrayList<>(1);

        StringBuilder buffy = new StringBuilder(lore.size() * 35);

        for (int i=0, last = lore.size() - 1; i < lore.size(); i++) {

            String line = lore.get(i);
            if (line == null)
                continue;

            Matcher matcher = TextUtils.PATTERN_PIPE.matcher(line);
            buffy.append(matcher.replaceAll("\\|"));

            if (i < last) {
                buffy.append('|');
            }
        }

        result.add(new ItemMetaObject(getMetaName(), buffy.toString()));

        return result;
    }
}

@@ -1398,62 +1398,8 @@
 Con;
-%0Aimport com.jcwhatever.bukkit.generic.utils.TextUtils;
 %0A%0Aim
@@ -1533,72 +1533,8 @@
 ist;
-%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;
 %0A%0A/*
@@ -1634,89 +1634,8 @@
  %7B%0A%0A
-    private static final Pattern PATTERN_ESCAPED_PIPE = Pattern.compile(%22%5C%5C%7C%22);%0A%0A
     
@@ -2152,144 +2152,271 @@
     
+List%3C
 String
-%5B%5D lineArray = TextUtils.PATTERN_PIPE.split(meta.getRawData());%0A%0A        List%3CString%3E lines = new ArrayList%3C%3E(lineArray.length);%0A%0A
+%3E currentLore = ItemStackHelper.getLore(itemStack);%0A%0A        List%3CString%3E newLore = currentLore == null%0A                ? new ArrayList%3CString%3E(5)%0A                : new ArrayList%3CString%3E(currentLore.size() + 1);%0A%0A        if (currentLore != null) %7B%0A    
     
@@ -2442,17 +2442,19 @@
 e : 
-lineArray
+currentLore
 ) %7B%0A
@@ -2469,119 +2469,95 @@
     
-Matcher matcher = PATTERN_ESCAPED_PIPE.matcher(line);%0A            lines.add(matcher.replaceAll(%22%7C%22));%0A        %7D
+    newLore.add(line);%0A            %7D%0A        %7D%0A%0A        newLore.add(meta.getRawData());
 %0A%0A  
@@ -2601,13 +2601,15 @@
 ck, 
-lines
+newLore
 );%0A%0A
@@ -3063,151 +3063,13 @@
     
-StringBuilder buffy = new StringBuilder(lore.size() * 35);%0A%0A        for (int i=0, last = lore.size() - 1; i %3C lore.size(); i++) %7B%0A%0A            
+for (
 Stri
@@ -3080,290 +3080,22 @@
 ine 
-=
+:
  lore
-.get(i);%0A            if (line == null)%0A                continue;%0A%0A            Matcher matcher = TextUtils.PATTERN_PIPE.matcher(line);%0A            buffy.append(matcher.replaceAll(%22%5C%5C%7C%22));%0A%0A            if (i %3C last) %7B%0A                buffy.append('%7C');%0A            %7D%0A        %7D%0A%0A
+) %7B%0A    
     
@@ -3147,27 +3147,25 @@
 (), 
-buffy.toString()));
+line));%0A        %7D
 %0A%0A  

remove pipe line separation