/*
 * Copyright 2013 Martin Smock <smock.martin@gmail.com>
 *
 * 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 li.strolch.model.timevalue.impl;

import static li.strolch.model.parameter.ListParameter.VALUE_SEPARATOR2;

import java.io.Serializable;
import java.util.*;

import li.strolch.model.StrolchValueType;
import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.timevalue.IValue;
import li.strolch.utils.helper.StringHelper;

/**
 * {@link IValue} implementation to work with Double valued {@link ITimeValue} objects
 *
 * @author Martin Smock <smock.martin@gmail.com>
 */
public class FloatListValue implements IValue<List<Double>>, Serializable {

	public static final FloatListValue NEUTRAL = new FloatListValue(0.0d);

	private final List<Double> value;

	public FloatListValue(Double value) {
		this.value = Collections.singletonList(value);
	}

	public FloatListValue(double... values) {
		List<Double> value = new ArrayList<>();
		for (Double aDouble : values) {
			value.add(aDouble);
		}
		this.value = value;
	}

	public FloatListValue(List<Double> values) {
		this.value = new ArrayList<>(values);
	}

	public FloatListValue(String valueAsString) throws NumberFormatException {
		List<Double> value = new ArrayList<>();
		String[] values = valueAsString.split(",");
		for (String s : values) {
			value.add(Double.parseDouble(s.trim()));
		}
		this.value = value;
	}

	@Override
	public String getType() {
		return StrolchValueType.FLOAT_LIST.getType();
	}

	@Override
	public FloatListValue add(List<Double> o) {
		this.value.addAll(o);
		return this;
	}

	@Override
	public List<Double> getValue() {
		return this.value;
	}

	@Override
	public String getValueAsString() {
		if (this.value.isEmpty()) {
			return StringHelper.EMPTY;
		}

		StringBuilder sb = new StringBuilder();
		Iterator<Double> iter = this.value.iterator();
		while (iter.hasNext()) {

			sb.append(iter.next());

			if (iter.hasNext()) {
				sb.append(VALUE_SEPARATOR2);
				sb.append(" ");
			}
		}

		return sb.toString();
	}

	@Override
	public String toString() {
		return "FloatListValue [value=" + this.value + "]";
	}

	@Override
	public boolean matches(IValue<List<Double>> other) {
		return this.value.equals(other.getValue());
	}

	@Override
	public FloatListValue getInverse() {
		return new FloatListValue(this.value);
	}

	@Override
	public FloatListValue getCopy() {
		return new FloatListValue(this.value);
	}

	@Override
	public int compareTo(IValue<List<Double>> o) {
		List<Double> otherValues = o.getValue();
		if (this.value.equals(otherValues))
			return 0;
		return Integer.compare(this.value.size(), otherValues.size());
	}

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

	@Override
	public int hashCode() {
		return Objects.hash(value);
	}
}
