/*
 * Copyright 2016 Axel Howind.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.dua3.utility.text;

import java.util.Map;
import java.util.Objects;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 *
 * @author axel
 */
public class RichTextBuilder implements Appendable {

    private final StringBuilder buffer = new StringBuilder();

    private final SortedMap<Integer, TextAttributes> parts = new TreeMap<>();

    /**
     * Construct a new empty builder.
     */
    public RichTextBuilder() {
        parts.put(0, new TextAttributes());
    }

    @Override
    public RichTextBuilder append(char c) {
        buffer.append(c);
        return this;
    }

    @Override
    public RichTextBuilder append(CharSequence csq) {
        buffer.append(csq);
        return this;
    }

    @Override
    public Appendable append(CharSequence csq, int start, int end) {
        buffer.append(csq, start, end);
        return this;
    }

    /**
     * Get attribute of the current Run.
     *
     * @param property
     *            the property
     * @return value of the property
     */
    public Object get(String property) {
        return currentStyle().get(property);
    }

    /**
     * Remove and return the value of a property from the property stack.
     *
     * @param property
     *            the property
     * @return the last stored value for this property on the stack
     */
    public Object pop(String property) {
        Object prev = null;
        for (Map.Entry<Integer, TextAttributes> e : parts.entrySet()) {
            if (Objects.equals(e.getKey(), parts.lastKey())) {
                break;
            }
            prev = e.getValue().getOrDefault(property, prev);
        }
        Object current = currentStyle().get(property);
        if (prev != null) {
            currentStyle().put(property, prev);
        } else {
            currentStyle().remove(property);
        }
        return current;
    }

    /**
     * Push a style property.
     *
     * @param property
     *            the property to set
     * @param value
     *            the value to be set
     */
    public void push(String property, Object value) {
        currentStyle().put(property, value);
    }

    /**
     * Convert to RichText.
     *
     * @return RichText representation of this builder's content
     */
    public RichText toRichText() {
        String text = buffer.toString();
        Run[] runs = new Run[parts.size()];

        int runIdx = 0;
        int start = parts.firstKey();
        TextAttributes style = parts.get(start);
        for (Map.Entry<Integer, TextAttributes> e : parts.entrySet()) {
            int end = e.getKey();
            int runLength = end - start;

            if (runLength == 0) {
                continue;
            }

            runs[runIdx++] = new Run(text, start, end - start, style);
            start = end;
            style = e.getValue();
        }
        runs[runIdx] = new Run(text, start, text.length() - start, style);

        return new RichText(text, runs);
    }

    @Override
    public String toString() {
        return buffer.toString();
    }

    private TextAttributes currentStyle() {
        final TextAttributes style;
        if (parts.lastKey() == buffer.length()) {
            style = parts.get(parts.lastKey());
        } else {
            style = new TextAttributes();
            parts.put(buffer.length(), style);
        }
        return style;
    }

}

@@ -726,16 +726,316 @@
 %0A%0A/**%0A *
+ A builder class for rich text data.%0A * %3Cp%3E A rich text is created by appending strings to the builder using one of the %7B@link #append(CharSequence)%7D%0A * method or one of its overloads. Style properties are set using %7B@link #push(String, Object)%7D and then reset using %0A * %7B@link #pop(String)%7D.%0A * %3C/p%3E
 %0A * @aut

add support for nested styles