/*
 *
 * Copyright: (c) 2012 Enough Software GmbH & Co. KG
 *
 * Licensed under:
 * 1. MIT: http://www.opensource.org/licenses/mit-license.php
 * 2. Apache 2.0: http://opensource.org/licenses/apache2.0
 * 3. GPL with classpath exception: http://www.gnu.org/software/classpath/license.html
 *
 * You may not use this file except in compliance with these licenses.
 *
 */
 
package de.enough.glaze.style.definition.converter.border;

import net.rim.device.api.ui.XYEdges;
import de.enough.glaze.style.Color;
import de.enough.glaze.style.Dimension;
import de.enough.glaze.style.definition.Definition;
import de.enough.glaze.style.definition.converter.Converter;
import de.enough.glaze.style.definition.converter.utils.ColorConverterUtils;
import de.enough.glaze.style.definition.converter.utils.DimensionConverterUtils;
import de.enough.glaze.style.parser.exception.CssSyntaxError;
import de.enough.glaze.style.parser.property.ColorPropertyParser;
import de.enough.glaze.style.parser.property.DimensionPropertyParser;
import de.enough.glaze.style.parser.property.Property;
import de.enough.glaze.style.property.border.GzBorderFactory;

/**
 * A {@link Converter} implementation to convert a definition to a bevel border 
 * 
 * @author Andre
 * 
 */
public class BevelBorderConverter implements Converter {
	
	/**
	 * a light grey for the default inner bevel color
	 */
	private static int LIGHTGRAY = 0xd3d3d3;
	
	/**
	 * a dark grey for the default inner bevel color
	 */
	private static int DARKGRAY = 0xa9a9a9a;

	/**
	 * the instance
	 */
	private static BevelBorderConverter INSTANCE;

	/**
	 * Returns the instance
	 * 
	 * @return the instance
	 */
	public static BevelBorderConverter getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new BevelBorderConverter();
		}

		return INSTANCE;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.enough.glaze.style.definition.converter.Converter#getIds()
	 */
	public String[] getIds() {
		return new String[] { "border-inner-color", "border-outer-color",
				"border-width" };
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * de.enough.glaze.style.definition.converter.Converter#convert(de.enough
	 * .glaze.style.definition.Definition)
	 */
	public Object convert(Definition definition) throws CssSyntaxError {
		if (!definition.hasProperties(this)) {
			return null;
		}

		Property borderInnerColorProp = definition
				.getProperty("border-inner-color");
		Property borderOuterColorProp = definition
				.getProperty("border-outer-color");
		Property borderWidthProp = definition.getProperty("border-width");

		XYEdges borderInnerColors = ColorConverterUtils
				.toXYEdges(LIGHTGRAY);
		XYEdges borderOuterColors = ColorConverterUtils
				.toXYEdges(DARKGRAY);
		XYEdges borderWidths = DimensionConverterUtils.toXYEdges(1);

		if (borderInnerColorProp != null) {
			Object result = ColorPropertyParser.getInstance().parse(
					borderInnerColorProp);
			if (result instanceof Color) {
				Color color = (Color) result;
				borderInnerColors = ColorConverterUtils.toXYEdges(color);
			} else if (result instanceof Color[]) {
				Color[] colors = (Color[]) result;
				borderInnerColors = ColorConverterUtils.toXYEdges(colors,
						borderInnerColorProp);
			}
		}

		if (borderOuterColorProp != null) {
			Object result = ColorPropertyParser.getInstance().parse(
					borderOuterColorProp);
			if (result instanceof Color) {
				Color color = (Color) result;
				borderOuterColors = ColorConverterUtils.toXYEdges(color);
			} else if (result instanceof Color[]) {
				Color[] colors = (Color[]) result;
				borderOuterColors = ColorConverterUtils.toXYEdges(colors,
						borderOuterColorProp);
			}
		}

		if (borderWidthProp != null) {
			Object result = DimensionPropertyParser.getInstance().parse(
					borderWidthProp);
			if (result instanceof Dimension) {
				Dimension dimension = (Dimension) result;
				borderWidths = DimensionConverterUtils.toXYEdges(dimension);
			} else if (result instanceof Dimension[]) {
				Dimension[] dimensions = (Dimension[]) result;
				borderWidths = DimensionConverterUtils.toXYEdges(dimensions,
						borderWidthProp);
			}
		}

		return GzBorderFactory.createBevelBorder(borderWidths,
				borderOuterColors, borderInnerColors);
	}

}
