package org.xbib.datastructures.yaml.tiny;

import org.xbib.datastructures.api.Node;

import java.util.Collections;
import java.util.List;

public class ValueNode implements org.xbib.datastructures.api.ValueNode {

    private final int depth;

    private Object value;

    private TextType type;

    public ValueNode(Node<?> parent) {
        this(parent, "");
    }

    public ValueNode(Node<?> parent, String value) {
        this(parent, value, TextType.LINE);
    }

    public ValueNode(Node<?> parent, String value, TextType type) {
        this.depth = parent != null ? parent.getDepth() + 1 : 0;
        this.value = value;
        this.type = type;
    }

    @Override
    public int getDepth() {
        return depth;
    }

    @Override
    public void set(Object value) {
        this.value = value;
    }

    @Override
    public Object get() {
        return value;
    }

    public void add(Object value) {
        if (value == null) {
            return;
        }
        if (this.value == null) {
            this.value = new StringBuilder();
            ((StringBuilder) this.value).append(value);
        } else {
            if (this.value instanceof StringBuilder) {
                ((StringBuilder) this.value).append(value);
            } else if (this.value instanceof String) {
                String v = this.value.toString();
                this.value = new StringBuilder();
                ((StringBuilder) this.value).append(v);
                ((StringBuilder) this.value).append(value);
            } else {
                throw new IllegalArgumentException("unexpected value class " + this.value.getClass());
            }
        }
    }

    public void setType(TextType type) {
        this.type = type;
    }

    public TextType getType() {
        return type;
    }

    public List<String> getComments() {
        return Collections.emptyList();
    }

    public enum TextType {
        LINE,
        MULTILINE,
        TEXT,
        TEXT_ANGLE
    }
}
