package com.rafkind.paintown.animator.events;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.File;
import com.rafkind.paintown.animator.Animator;
import com.rafkind.paintown.animator.Animation;
import com.rafkind.paintown.animator.DrawState;
import com.rafkind.paintown.animator.DrawArea;
import com.rafkind.paintown.MaskedImage;
import com.rafkind.paintown.Token;
import com.rafkind.paintown.animator.events.AnimationEvent;
import org.swixml.SwingEngine;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

public class FrameEvent implements AnimationEvent
{
	private String frame = "";
	
	public void loadToken(Token token)
	{
		Token parent = token.getParent();
		Token basedir = parent.findToken("basedir");
		if(basedir != null){
			frame = basedir.readString(0) + token.readString(0);
		} else {
			frame = token.readString(0);
		}
	}
	
	public void interact( Animation animation ){
		try{
			animation.setImage(MaskedImage.load(Animator.getDataPath() + "/" + frame));
			animation.delay();
		} catch ( Exception e ){
			e.printStackTrace();
			System.out.println("Problem! --> " + Animator.getDataPath() + "/" + frame);
		}
	}
	
	public String getName(){
		return getToken().toString();
	}
	
	public JDialog getEditor( Animation animation ){
		SwingEngine engine = new SwingEngine( "animator/eventframe.xml" );
		((JDialog)engine.getRootComponent()).setSize(350,270);
		JPanel canvas = (JPanel)engine.find("canvas");
		canvas.setSize(350,200);
		
		class drawArea extends Canvas
		{
			private BufferedImage img = null;
			public void paint( Graphics g )
			{
				g.setColor( new Color( 0, 0, 0 ) );
				g.fillRect( 0, 0, 640, 480 );
				if(img != null)
					g.drawImage( img, 125 - (img.getTileWidth()/2), 100 - (img.getTileHeight()/2), null );
			}
			
			public void setImage(BufferedImage i)
			{
				img = i;
			}
		};
		final drawArea area = new drawArea();
		area.setSize(350,200);
		
		canvas.add(area);
		
		final JComboBox framebox = (JComboBox) engine.find( "frame" );
		for ( Iterator it = getFiles( animation.getBaseDirectory() ).iterator(); it.hasNext(); ){
			String frame = (String) it.next();
			framebox.addItem(frame);
		}
		
		framebox.addActionListener( new ActionListener(){
			public void actionPerformed(ActionEvent actionEvent){
				frame = (String)framebox.getSelectedItem();
				try
				{
					area.setImage(MaskedImage.load(Animator.getDataPath() + "/" + frame));
				}
				catch(Exception e)
				{
					System.out.println("Couldn't load file!");
				}
				area.repaint();
			}
		});
		
		framebox.setSelectedIndex(0);
		
		return (JDialog)engine.getRootComponent();
	}

	private List getFiles( String path ){
		File dir = Animator.dataPath( new File( path ) );
		List files = new ArrayList();
		if ( dir.isDirectory() ){
			File[] all = dir.listFiles();
			for ( int i = 0; i < all.length; i++ ){
				if ( all[ i ].getName().endsWith( ".png" ) ||
				     all[ i ].getName().endsWith( ".bmp" ) ){
					files.add( path.replaceAll("data/","") + all[ i ].getName().replaceAll("^./","") );
				}
			}
		}
		return files;
	}
	
	public Token getToken(){
		Token temp = new Token("frame");
		temp.addToken( new Token( "frame" ) );
		temp.addToken( new Token( frame ) );
		return temp;
	}
}

@@ -1592,17 +1592,19 @@
 nds 
-Canvas%0A%09%09
+JComponent 
 %7B%0A%09%09
@@ -1672,20 +1672,16 @@
 hics g )
-%0A%09%09%09
 %7B%0A%09%09%09%09g.
@@ -1754,17 +1754,19 @@
 ;%0A%09%09%09%09if
-(
+ ( 
 img != n
@@ -1768,17 +1768,19 @@
  != null
-)
+ )%7B
 %0A%09%09%09%09%09g.
@@ -1864,16 +1864,22 @@
 null );%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%09
@@ -1919,20 +1919,16 @@
 Image i)
-%0A%09%09%09
 %7B%0A%09%09%09%09im
@@ -1944,16 +1944,17 @@
 %09%7D%0A%09%09%7D;%0A
+%0A
 %09%09final 
@@ -1985,16 +1985,17 @@
 Area();%0A
+%0A
 %09%09area.s
@@ -2007,24 +2007,77 @@
 e(350,200);%0A
+%09%09area.setPreferredSize( new Dimension( 350,200 ) );%0A
 %09%09%0A%09%09canvas.
@@ -2484,21 +2484,16 @@
 %0A%09%09%09%09try
-%0A%09%09%09%09
 %7B%0A%09%09%09%09%09a
@@ -2571,19 +2571,17 @@
 %09%09%09%7D
-%0A%09%09%09%09
+ 
 catch
-(
+ ( 
 Exce
@@ -2591,14 +2591,10 @@
 on e
+ 
 )
-%0A%09%09%09%09
 %7B%0A%09%09
@@ -2638,10 +2638,77 @@
 file
-!%22
+: %22 + Animator.getDataPath() + %22/%22 + frame );%0A%09%09%09%09%09e.printStackTrace(
 );%0A%09
@@ -2774,17 +2774,19 @@
 edIndex(
-0
+ 0 
 );%0A%09%09%0A%09%09

clean up preview frame code