/**
 * License Agreement.
 *
 *  JBoss RichFaces - Ajax4jsf Component Library
 *
 * Copyright (C) 2007  Exadel, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

package org.richfaces.renderkit.html;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;
import java.util.Date;

import javax.faces.context.FacesContext;

import org.ajax4jsf.resource.GifRenderer;
import org.ajax4jsf.resource.InternetResourceBuilder;
import org.ajax4jsf.resource.Java2Dresource;
import org.ajax4jsf.resource.ResourceContext;
import org.ajax4jsf.util.HtmlColor;
import org.ajax4jsf.util.Zipper2;
import org.richfaces.skin.Skin;
import org.richfaces.skin.SkinFactory;

/**
 * @author Nick Belaevski - nbelaevski@exadel.com
 * created 02.02.2007
 * 
 */
public class BaseGradient extends Java2Dresource {

	private int width;
	private int height;
	private int gradientHeight;
	private String baseColor;
	private String gradientColor;
    private boolean horizontal = false;
	
	public BaseGradient(int width, int height, int gradientHeight) {
		super();
		this.width = width;
		this.height = height;
		this.gradientHeight = gradientHeight;

		setRenderer(new GifRenderer());
		setLastModified(new Date(InternetResourceBuilder.getInstance().getStartTime()));
	}

	public BaseGradient(int width, int height, int gradientHeight, String baseColor, String gradientColor) {
		this(width, height, gradientHeight);
		this.baseColor = baseColor;
		this.gradientColor = gradientColor;
	}

	public BaseGradient(int width, int height) {
		this(width, height, height);
	}

	public BaseGradient(int width, int height, String baseColor, String gradientColor) {
		this(width, height, height);
		this.baseColor = baseColor;
		this.gradientColor = gradientColor;
	}

	public BaseGradient() {
		this(30, 50, 20);
	}
	
	public BaseGradient(String baseColor, String gradientColor) {
		this(30, 50, 20);
		this.baseColor = baseColor;
		this.gradientColor = gradientColor;
	}
    
    public BaseGradient(int width, int height, int gradientHeight, boolean horizontal) {
        this(width, height, gradientHeight);
        this.horizontal = horizontal;
    }
    
    public BaseGradient(int width, int height, int gradientHeight, String baseColor, String gradientColor, boolean horizontal) {
        this(width, height, gradientHeight);
        this.baseColor = baseColor;
        this.gradientColor = gradientColor;
        this.horizontal = horizontal;
    }
    
    public BaseGradient(int width, int height, boolean horizontal) {
        this(width, height, horizontal?width:height);
        this.horizontal = horizontal;
    }
    
    public BaseGradient(int width, int height, String baseColor, String gradientColor, boolean horizontal) {
        this(width, height, horizontal?width:height);
        this.baseColor = baseColor;
        this.gradientColor = gradientColor;
        this.horizontal = horizontal;
    }

    public BaseGradient(boolean horizontal) {
        this(30, 50, 20);
        this.horizontal = horizontal;
    }
    
    public BaseGradient(String baseColor, String gradientColor, boolean horizontal) {
        this(30, 50, 20);
        this.baseColor = baseColor;
        this.gradientColor = gradientColor;
        this.horizontal = horizontal;
    }
    
	public Dimension getDimensions(FacesContext facesContext, Object data) {
		return new Dimension(width, height);
	}

	protected Dimension getDimensions(ResourceContext resourceContext) {
		return getDimensions(null, restoreData(resourceContext));
	}

	protected void paint(ResourceContext resourceContext, Graphics2D g2d) {
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
		g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
		Data dataToStore = (Data) restoreData(resourceContext);
		if (dataToStore != null && dataToStore.headerBackgroundColor!=null && dataToStore.headerGradientColor!=null) {
			Color baseColor = new Color(dataToStore.headerBackgroundColor.intValue());
			Dimension dim =getDimensions(resourceContext);
			Rectangle2D rect = 
				new Rectangle2D.Float(
						0, 
						0, 
						dim.width, 
						dim.height);
			int gradStart = 0;
			int gradEnd = gradientHeight;
			Color alternateColor = new Color(dataToStore.headerGradientColor.intValue());
            GradientPaint gragient;
            if (horizontal) {
                gragient = new GradientPaint(gradStart, 0, baseColor, gradEnd, 0, alternateColor);
            } else {
                gragient = new GradientPaint(0, gradStart, baseColor, 0, gradEnd, alternateColor);
            }
			g2d.setPaint(gragient);
			g2d.fill(rect);
		}
	}

	protected Object deserializeData(byte[] objectArray) {
		Data data = new Data();
		if (objectArray != null) {
			Zipper2 zipper2 = new Zipper2(objectArray);
			data.headerBackgroundColor = new Integer(zipper2.nextIntColor());
			data.headerGradientColor = new Integer(zipper2.nextIntColor());
		}

		return data;
	}
	
	protected Object getDataToStore(FacesContext context, Object data) {
		if (baseColor == null) {
			return new Data(context).toByteArray();
		} else {
			return new Data(context, baseColor, gradientColor).toByteArray();
		}
	}
	
	public boolean isCacheable() {
		return true;
	}
	
	protected static class Data implements Serializable {
		public Data() {
		}

		protected Data(FacesContext context) {
			this(context, Skin.headerBackgroundColor, "headerGradientColor");
		}

		protected Data(FacesContext context, String baseColor, String gradientColor) {
			this.headerBackgroundColor = getColorValueParameter(context, baseColor, false);
			this.headerGradientColor = getColorValueParameter(context, gradientColor, false);

			if (!(this.headerBackgroundColor == null && this.headerGradientColor == null)) {
				if (this.headerBackgroundColor == null) {
					this.headerBackgroundColor = getColorValueParameter(context, baseColor, true);
				}
				
				if (this.headerGradientColor == null) {
					this.headerGradientColor = getColorValueParameter(context, gradientColor, true);
				}
			}
		}

		private Integer getColorValueParameter(FacesContext context, String name, boolean useDefault) {
			Skin skin;
			if (useDefault) {
				skin = SkinFactory.getInstance().getDefaultSkin(context);
			} else {
				skin = SkinFactory.getInstance().getSkin(context);
			}
			
			String tmp = (String) skin.getParameter(context,name); 
			if (tmp!=null && tmp.length() != 0) {
				return new Integer(HtmlColor.decode(tmp).getRGB());
			} else {
				return null;
			}
		}

		/**
		 * 
		 */
		private static final long serialVersionUID = 1732700513743861250L;
		protected Integer headerBackgroundColor;
		protected Integer headerGradientColor;
		public byte[] toByteArray() {
			if (headerBackgroundColor != null && headerGradientColor != null) {
				byte[] ret = new byte[6];
				new Zipper2(ret).addColor(headerBackgroundColor.intValue()).addColor(headerGradientColor.intValue());
				return ret;
			} else {
				return null;
			}
		}
	}

}
