
package jp.riken.brain.ni.samuraigraph.figure.java2d;

import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import jp.riken.brain.ni.samuraigraph.base.SGDrawingElement;
import jp.riken.brain.ni.samuraigraph.base.SGProperties;
import jp.riken.brain.ni.samuraigraph.base.SGTuple2f;
import jp.riken.brain.ni.samuraigraph.base.SGUtilityNumber;
import jp.riken.brain.ni.samuraigraph.base.SGUtilityText;
import jp.riken.brain.ni.samuraigraph.figure.SGDrawingElementString;
import jp.riken.brain.ni.samuraigraph.figure.SGIStringConstants;

import org.w3c.dom.Element;


public abstract class SGElementGroupString extends SGElementGroupSXY
	implements SGIStringConstants, SGIElementGroupConstants
{

	/**
	 * 
	 */
	protected String mFontName;


	/**
	 *
	 */
	protected float mFontSize;


	/**
	 *
	 */
	protected int mFontStyle;


	/**
	 * 
	 */
	protected float mAngle;


	/**
	 * 
	 */
	public SGElementGroupString()
	{
		super();
	}



	/**
	 * 
	 * @param name
	 * @param style
	 * @param size
	 * @return
	 */
	public boolean setFont( final String name, final int style, final float size )
	{
		this.mFontName = name;
		this.mFontStyle = style;
		this.mFontSize = size;
		if( this.mDrawingElementArray!=null )
		{
			for( int ii=0; ii<this.mDrawingElementArray.length; ii++ )
			{
				SGDrawingElementString el
					= (SGDrawingElementString)this.mDrawingElementArray[ii];
				el.setFont( name, style, size );
			}
		}

		return true;
	}


	/**
	 * 
	 */
	public boolean setAngle( final float angle )
	{
		this.mAngle = angle;
		if( this.mDrawingElementArray!=null )
		{
			for( int ii=0; ii<this.mDrawingElementArray.length; ii++ )
			{
				SGDrawingElementString el
					= (SGDrawingElementString)this.mDrawingElementArray[ii];
				el.setAngle(angle);
			}
		}
		return true;
	}


	/**
	 * 
	 */
	public String getFontName()
	{
		return this.mFontName;
	}


	/**
	 * 
	 */
	public float getFontSize()
	{
		return this.mFontSize;
	}


	/**
	 * 
	 */
	public int getFontStyle()
	{
		return this.mFontStyle;
	}


	/**
	 * 
	 */
	public float getAngle()
	{
		return this.mAngle;
	}



	/**
	 * 
	 */
	public boolean paintElement( final Graphics2D g2d, Rectangle2D clipRect )
	{
		if( this.mDrawingElementArray!=null )
		{
			for( int ii=0; ii<this.mDrawingElementArray.length; ii++ )
			{
				SGDrawingElementString2DExtended el
					= (SGDrawingElementString2DExtended)this.mDrawingElementArray[ii];
				el.drawString(g2d);
			}
		}
		return true;
	}



	/**
	 * 
	 */
	public SGDrawingElement getDrawingElement()
	{
		SGDrawingElementString2D el = new SGDrawingElementString2D();
		el.setVisible( this.mVisibleFlag );
		el.setFont( this.mFontName, this.mFontStyle, this.mFontSize );
		el.setAngle( this.mAngle );
		el.setColorList( this.mColorList );
		return el;
	}



	/**
	 * 
	 * @return
	 */
	protected boolean initDrawingElement( final int num )
	{
		SGDrawingElementString[] array = new SGDrawingElementString2DExtended[num];
		for( int ii=0; ii<num; ii++ )
		{
			array[ii] = new SGDrawingElementString2DExtended();
		}
		this.mDrawingElementArray = array;
		return true;
	}



	/**
	 * 
	 */
	public boolean setLocation(
		final SGTuple2f[] pointArray )
	{

		if( pointArray==null )
		{
			throw new IllegalArgumentException("pointArray==null");
		}

		if( pointArray.length != this.mDrawingElementArray.length )
		{
			throw new Error("pointArray.length != this.mDrawingElementArray.length");
		}

		// シンボルの描画要素を作成
		for( int ii=0; ii<pointArray.length; ii++ )
		{
			SGDrawingElementString2DExtended el
				= (SGDrawingElementString2DExtended)this.mDrawingElementArray[ii];
			final boolean eff = !( pointArray[ii].isInfinite() || pointArray[ii].isNaN() );
			el.setVisible( eff );
			if( eff )
			{
				el.setLocation( pointArray[ii] );
			}
		}

		return true;
	}


	/**
	 * 
	 * @return
	 */
	public boolean setPropertiesOfDrawingElements()
	{
		for( int ii=0; ii<this.mDrawingElementArray.length; ii++ )
		{
			final SGDrawingElementString el
				= (SGDrawingElementString)this.mDrawingElementArray[ii];
			el.setFont( this.mFontName, this.mFontStyle, this.mFontSize );
			el.setAngle( this.mAngle );
			el.setColorList( this.mColorList );
			el.setMagnification( this.mMagnification );
		}
		return true;
	}



	/**
	 * 
	 */
	public boolean setProperty( final SGDrawingElement element )
	{
		if( !(element instanceof SGDrawingElementString) )
		{
			return false;
		}

		if( super.setProperty( element ) == false )
		{
			return false;
		}

		SGDrawingElementString el = (SGDrawingElementString)element;

		this.setFont( el.getFontName(), el.getFontStyle(), el.getFontSize() );		
		this.setAngle( el.getAngle() );

		return true;
	}





	
	/**
	 * 
	 * @return
	 */
	public String getTagName()
	{
		return TAG_NAME_LABEL;
	}


	/**
	 * 
	 */
	public boolean writeProperty( final Element el )
	{
		String pt = SGUtilityNumber.pt;
		String degree = SGUtilityNumber.degree;
		el.setAttribute( KEY_VISIBLE, Boolean.toString( this.mVisibleFlag ) );
		el.setAttribute( KEY_FONT_SIZE, Float.toString( this.mFontSize ) + pt );
		el.setAttribute( KEY_FONT_NAME, this.mFontName );
		el.setAttribute( KEY_FONT_STYLE, SGUtilityText.getFontStyleName( this.mFontStyle ) );
		el.setAttribute( KEY_COLOR_LIST, SGUtilityText.getColorListString( this.mColorList ) );
		el.setAttribute( KEY_ANGLE, Float.toString( - this.mAngle/RADIAN_DEGREE_RATIO ) + degree );
		return true;
	}



	/**
	 * 
	 */
	public SGProperties getProperties()
	{
		StringProperties p = new StringProperties();
		this.getProperties(p);
		return p;
	}


	/**
	 * 
	 */
	public boolean getProperties( SGProperties p )
	{
		if( p==null ) return false;
		if( ( p instanceof StringProperties ) == false ) return false;

		super.getProperties(p);

		StringProperties sp = (StringProperties)p;
		sp.setFontName( this.getFontName() );
		sp.setFontSize( this.getFontSize() );
		sp.setFontStyle( this.getFontStyle() );
		sp.setAngle( this.getAngle() );

		return true;
	}


	/**
	 * 
	 */
	public boolean setProperties( SGProperties p )
	{
		if( ( p instanceof StringProperties ) == false ) return false;

		if( super.setProperties(p) == false ) return false;
		
		StringProperties sp = (StringProperties)p;

		final Float fontSize = sp.getFontSize();
		if( fontSize==null )
		{
			return false;
		}

		final Integer fontStyle = sp.getFontStyle();
		if( fontStyle==null )
		{
			return false;
		}

		this.setFont( sp.getFontName(), fontStyle.intValue(), fontSize.floatValue() );

		final Float angle = sp.getAngle();
		if( angle==null )
		{
			return false;
		}
		this.setAngle( angle.floatValue() );

		return true;
	}




	/**
	 * 
	 */
	public static class StringProperties extends ElementGroupProperties
	{
		
		public static final String[] keys = {
			KEY_FONT_SIZE, KEY_FONT_STYLE, KEY_FONT_NAME, KEY_ANGLE
		};



		/**
		 * 
		 *
		 */
		public StringProperties()
		{
			super();
		}


		/**
		 * 
		 */
		public boolean equals( final Object obj )
		{

			if( ( obj instanceof StringProperties ) == false )
			{
				return false;
			}
			
			if( super.equals(obj) == false ) return false;

			StringProperties p = (StringProperties)obj;
			for( int ii=0; ii<keys.length; ii++ )
			{
				if( this.getProperty(keys[ii]).equals(p.getProperty(keys[ii])) == false )
				{
					return false;
				}
			}

			return true;
		}



		public Float getFontSize()
		{
			final String value = this.getProperty(KEY_FONT_SIZE);
			return SGUtilityText.getFloat(value);
		}


		public Integer getFontStyle()
		{
			final String value = this.getProperty(KEY_FONT_STYLE);
			return SGUtilityText.getInteger(value);
		}


		public String getFontName()
		{
			return this.getProperty(KEY_FONT_NAME);
		}


		public Float getAngle()
		{
			final String value = this.getProperty(KEY_ANGLE);
			return SGUtilityText.getFloat(value);
		}


		public boolean setFontSize( final float size )
		{
			if( size<0.0f )
			{
				return false;
			}
			this.setProperty( KEY_FONT_SIZE, new Float(size).toString() );
			return true;
		}


		public boolean setFontStyle( final int style )
		{
			this.setProperty( KEY_FONT_STYLE, new Integer(style).toString() );
			return true;
		}


		public boolean setFontName( final String name )
		{
			if( name==null )
			{
				return false;
			}
			this.setProperty( KEY_FONT_NAME, new String(name) );
			return true;
		}

		public boolean setAngle( final float angle )
		{
			this.setProperty( KEY_ANGLE, new Float(angle).toString() );
			return true;
		}

	}


}

