import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;

public class ManualAlign extends Canvas implements Runnable, MouseListener, MouseMotionListener {
  ImArray ima, imb, uima, uimb;
  int ax=0, ay=0;
  int xstart, ystart, curscale=1;
  cornerTransform ct, uct;

  public static void main(String args[]){
    (new Thread(new ManualAlign(args))).start();
  }

  public void run() {
    boolean toprompt;
    BufferedReader userin = new BufferedReader(new InputStreamReader(System.in));
    toprompt = true;
    while(true){
      if(toprompt)
	System.out.print("still> ");
      toprompt = false;
      String uin;
      try {
	if(!userin.ready()){
	  Thread.sleep(100);
	  continue;
	}
	uin = userin.readLine();
	toprompt = true;
      } catch (Exception e) { uin = new String("error"); }
      StringTokenizer st = new StringTokenizer(uin);
      String args[];
      int ntok;
      args = new String[ntok = st.countTokens()];

      if(ntok < 1)
	continue;
      for(int i = 0;i<ntok;i++){
	args[i] = st.nextToken();
      }

      if(args[0].equals("wiggle")){
	int howmuch, howlong;
	if(ntok > 1){
	  try { howmuch = Integer.parseInt(args[1]); }
	  catch (Exception e) { howmuch = 1; }
	}
	else{
	  howmuch = 1;
	}
	if(ntok > 2){
	  try { howlong = Integer.parseInt(args[2]); }
	  catch (Exception e) { howlong = 3; }
	}
	else{
	  howlong = 3;
	}

	uct = StillUtil.downhill(uima, uimb, uct, howlong, howmuch, false);
	ct = uct.grow(curscale);
      }
      else if(args[0].equals("print")){
	System.out.println("("+ct.x0+", "+ct.y0+"), "+
			   "("+ct.x1+", "+ct.y1+"), "+
			   "("+ct.x2+", "+ct.y2+"), "+
			   "("+ct.x3+", "+ct.y3+")");
      }
      else if(args[0].equals("save")){
	StillUtil.writeMerge(ima, imb, ct);
      }
      else if(args[0].equals("shrink")){
	int howmuch;
	if(ntok > 1){
	  try { howmuch = Integer.parseInt(args[1]); }
	  catch (Exception e) { howmuch = 1; }
	}
	else{
	  howmuch = 1;
	}
	curscale = howmuch;
	uct = ct.shrink(howmuch);
	uima = ima.shrink(howmuch);
	uimb = imb.shrink(howmuch);
      }
    }
  }

  public void mouseClicked(MouseEvent me){ }
  public void mousePressed(MouseEvent me){ 
    xstart = me.getX();
    ystart = me.getY();
  }
  public void mouseReleased(MouseEvent me){
    System.out.println("Difference: "+ImArray.imageDifference(ima, imb, ct)+" Current translation: "+ct.x0+", "+ct.y0);
  }
  public void mouseEntered(MouseEvent me){ }
  public void mouseExited(MouseEvent me){ }
  public void mouseDragged(MouseEvent me){ 
    int curx, cury;
    curx = me.getX();
    cury = me.getY();
    ax += (curx-xstart);
    ay += (cury-ystart);
    ct = ct.translate((double) -1*(curx-xstart), (double) -1*(cury-ystart));
    uct = ct;
    xstart = curx;
    ystart = cury;
    repaint();
  }
  public void mouseMoved(MouseEvent me){ }

  public void paint(Graphics g){
    g.drawImage(imb.image, ax, ay, this);
    g.drawImage(ima.image, 0, 0, this);
  }

  ManualAlign(String args[]){
    ima = StillUtil.loadImage(args[0]);
    imb = StillUtil.loadImage(args[1]);
    uima = ima;
    uimb = imb;
    Frame f = new Frame();
    ct = new cornerTransform(ima);
    uct = ct;
    f.add(this);
    addMouseListener(this);
    addMouseMotionListener(this);
    f.setBounds(0,0,500,500);
    f.doLayout();
    f.show();
    repaint();
  }


}

