package at.int32.sweaty.ui.controls;

import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.UnsupportedEncodingException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

import at.int32.sweaty.ui.Control;
import at.int32.sweaty.ui.controls.events.ClickBehaviour.IOnClickListener;
import at.int32.sweaty.ui.utils.ImageUtils;
import at.int32.sweaty.ui.utils.SWTGraphics2D;
import at.int32.sweaty.ui.utils.SWTUtils;

public class Avatar extends Widget<Composite>{

	private Color color;
	private int borderSize;
	private Image overlay;
	
	public Avatar(Control parent) {
		super(parent);
	}
	
	public Avatar size(int size) {
		data().widthHint = size;
		data().heightHint = size;
		return this;
	}
	
	@Override
	public Avatar center() {
		return (Avatar)super.center();
	}
	
	public Avatar color(Color color) {
		this.color = color;
		return this;
	}
	
	public Avatar border(int size) {
		this.borderSize = size;
		return this;
	}
	
	public Avatar overlay(Image overlay) {
		this.overlay = overlay;
		ctrl().redraw();
		return this;
	}
	
	public Avatar image(final Image img) {
		ctrl().addPaintListener(new PaintListener() {

			@Override
			public void paintControl(PaintEvent e) {
				SWTGraphics2D gb = null;
				int size = data().widthHint;

				try {
					if (img == null)
						return;

					BufferedImage bi = SWTUtils.convertToAWT(img.getImageData());
					gb = new SWTGraphics2D(e.gc);
					RoundRectangle2D r = new RoundRectangle2D.Float(0, 0, size, size, size, size);
					gb.setClip(r);
					gb.drawImage(bi, 0, 0, null);
					
					if(overlay != null) {
						gb.drawImage(overlay, 0, 0);
					}

					if(color != null) {
						e.gc.setForeground(color);
						e.gc.setLineWidth(borderSize > 0 ? borderSize : 4);
						e.gc.drawOval(0, 0, size, size);
					}

				} catch (Exception ex) {
					ex.printStackTrace();
				} finally {
					if (gb != null)
						gb.dispose();
				}
			}
		});

		ctrl().redraw();

		
		return this;
	}
	
	public Avatar image(final String base64) {
		
		try {
			return image(ImageUtils.getImageFromBase64(base64, data().widthHint, data().heightHint));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		return this;
	}
	
	@Override
	public Avatar handCursor() {
		return (Avatar)super.handCursor();
	}

	@Override
	public Composite getBaseControl(Composite parent) {
		return createDefaultComposite();
	}

}

@@ -311,48 +311,8 @@
 or;%0A
-import org.eclipse.swt.graphics.Cursor;%0A
 impo
@@ -416,20 +416,22 @@
 swt.
-graphics.RGB
+widgets.Canvas
 ;%0Aim
@@ -474,157 +474,42 @@
 te;%0A
-import org.eclipse.swt.widgets.Display;%0A%0Aimport at.int32.sweaty.ui.Control;%0Aimport at.int32.sweaty.ui.controls.events.ClickBehaviour.IOnClickListener
+%0Aimport at.int32.sweaty.ui.Control
 ;%0Aim
@@ -685,16 +685,17 @@
 mposite%3E
+ 
 %7B%0A%0A%09priv
@@ -760,17 +760,16 @@
 verlay;%0A
-%09
 %0A%09public
@@ -810,25 +810,24 @@
 parent);%0A%09%7D%0A
-%09
 %0A%09public Ava
@@ -912,33 +912,32 @@
 return this;%0A%09%7D%0A
-%09
 %0A%09@Override%0A%09pub
@@ -971,24 +971,25 @@
 urn (Avatar)
+ 
 super.center
@@ -991,25 +991,24 @@
 enter();%0A%09%7D%0A
-%09
 %0A%09public Ava
@@ -1064,33 +1064,32 @@
 return this;%0A%09%7D%0A
-%09
 %0A%09public Avatar 
@@ -1143,33 +1143,32 @@
 return this;%0A%09%7D%0A
-%09
 %0A%09public Avatar 
@@ -1247,33 +1247,32 @@
 return this;%0A%09%7D%0A
-%09
 %0A%09public Avatar 
@@ -1740,38 +1740,28 @@
 age(
-b
 i
+mg
 , 0, 0
-, null);%0A%09%09%09%09%09
+);%0A
 %0A%09%09%09%09%09if
 (ove
@@ -1756,16 +1756,17 @@
 %0A%09%09%09%09%09if
+ 
 (overlay
@@ -1827,16 +1827,17 @@
 %0A%09%09%09%09%09if
+ 
 (color !
@@ -2135,19 +2135,16 @@
 raw();%0A%0A
-%09%09%0A
 %09%09return
@@ -2153,17 +2153,16 @@
 his;%0A%09%7D%0A
-%09
 %0A%09public
@@ -2198,18 +2198,16 @@
 se64) %7B%0A
-%09%09
 %0A%09%09try %7B
@@ -2373,18 +2373,16 @@
 ();%0A%09%09%7D%0A
-%09%09
 %0A%09%09retur
@@ -2392,17 +2392,16 @@
 his;%0A%09%7D%0A
-%09
 %0A%09@Overr
@@ -2451,16 +2451,17 @@
 (Avatar)
+ 
 super.ha

fix image drawing