import java.awt.*;
import java.net.*;

public class line extends Panel implements tool
{
    asciiJam aj;
	int ox;
	int oy;
	int plantx;
	int planty;
	int zoom;
    int fill = 1;
	Label lbl=null;
	charChooser chCh = null;

    public void show(){
        setFont(env.dialogFont);
        super.show();
    }

    public void paint(Graphics g){
	  System.out.println(getLayout());
    }
    public line(asciiJam ajam){ 
        //this.setFont(env.dialogFont);
        this.aj = ajam;
        this.resize(123,275);
        this.setLayout(null);
        this.setBackground(Color.black);
        Panel prefs = new Panel();
        prefs.setLayout(new GridLayout(4,1,0,1));
        prefs.setBackground(Color.lightGray);
        prefs.add(new Label(" Line Prefs"));
        CheckboxGroup group1 = new CheckboxGroup();
		Checkbox tmp = null;
        prefs.add(new Checkbox("Line",group1,false));
        prefs.add(new Checkbox("Thin Line",group1,true));
        prefs.add(lbl=new Label(" Line letter:"));

	    chCh = new charChooser(aj);
		chCh.reshape(0,0,123,170);
        Panel inner = new Panel();
        inner.resize(size().width-2,size().height-2);
        inner.move(1,1);
		inner.setLayout(new BorderLayout());
        inner.add(chCh,BorderLayout.CENTER);
		inner.add(prefs,BorderLayout.NORTH);
        add(inner);
		lbl.hide();
	}

    public boolean mouseDrag(int x, int y)
    {
		int offX = aj.mapF.getX();
		int offY = aj.mapF.getY();
		x-= offX;
		y-=offY;

		Graphics g = aj.ta.getGraphics();
		if(g!=null){
			int z2 = zoom<<2;
			int z3 = zoom<<3;
			g.setColor(Color.white);
			g.setXORMode(Color.black);
			g.drawLine(ox+z2,oy+z3,plantx+z2,planty+z3);
			g.drawLine(x+z2,y+z3,plantx+z2,planty+z3);
			g.dispose();
		}
		ox = x;
		oy = y;
        return true;
    }


    public boolean mouseDown(int x, int y, int z, asciiJam ajam){
		zoom = z;
		int offX = aj.mapF.getX();
		int offY = aj.mapF.getY();
		ox = plantx = x -offX;
		oy = planty = y -offY;
		aj.ta.toolList[env.PENCIL_TOOL].mouseDown(x,y,z,ajam);
		return true;
    }

	public boolean mouseUp(int x, int y){ 
		int z3 = zoom<<3;
		int z4 = zoom<<4;
		int offX = aj.mapF.getX();
		int offY = aj.mapF.getY();
		if(fill==1)
		   aj.ta.toolList[env.PENCIL_TOOL].mouseDrag(x,y);
		else{
		    x -=offX;
		    y -=offY;
			int x0 = z3*(Math.min(x,plantx)/z3);
			int y0 = z4*(Math.min(y,planty)/z4);
			int x1 = z3*(Math.max(x,plantx)/z3);
			int y1 = z4*(Math.max(y,planty)/z4);
			int wid = ((x1-x0)/z3)*z3;
			int ht  = ((y1-y0)/z4)*z4;
			Graphics g = aj.img.getGraphics();
			if(g!=null){
			  g.setFont(env.font[aj.ta.zoom]);
			  if(wid==0){
			 	 for(int yy=y0;yy<y1;yy+=z4){
                     aj.ta.setchar(aj.fillChar, 
					 	x0,yy, aj.curColorIndex, aj.curCol, g,false);
				 }
			  }
			  else if(ht==0){
			 	 for(int xx=x0;xx<x1;xx+=z3){
                     aj.ta.setchar(aj.fillChar, 
					 	xx,y0, aj.curColorIndex, aj.curCol, g,false);
				 }
			  }else{
			  	if(wid>ht){
				   double xdbl = x0;
				   double ydbl = y0;
			       double slope = ((double)ht)/wid;
				   if(y<planty){
				   		slope = -1*slope;
				        if(x<plantx){
				   		  slope = -1*slope;
						  ydbl = y0;
				        }
						else{
							ydbl = y1;
						}
				   }else{
				        if(x<plantx){
				   		  slope = -1*slope;
						  ydbl = y1;
				        }
						else{
				   		 // slope = -1*slope;
						//	ydbl = y1;
						}
				   	   //if(x<plantx)
				   }
				   int curx,cury;
			       for(xdbl = x0;xdbl<x0+wid;xdbl++,ydbl+=slope){
				   	 curx = (int)xdbl;
				   	 cury = (int)ydbl;
                     aj.ta.setchar(aj.fillChar, 
					 	(curx/z3)*z3, (cury/z4)*z4, aj.curColorIndex, aj.curCol, g,false);
				   }
				}
			  	else{
				   double xdbl = x0;
				   double ydbl = y0;
			       double slope = ((double)wid)/ht;
				   if(x<plantx){
				   		slope = -1*slope;
						xdbl = x1;
				       if(y<planty){
				   		slope = -1*slope;
						xdbl = x0;
					   }
				   }
				   else if(y<planty){
				   		slope = -1*slope;
						xdbl = x1;
				   }
				   int curx,cury;
			       for(ydbl = y0;ydbl<y0+ht;ydbl++,xdbl+=slope){
				   	 curx = (int)xdbl;
				   	 cury = (int)ydbl;
                     aj.ta.setchar(aj.fillChar, 
					 	(curx/z3)*z3, (cury/z4)*z4, aj.curColorIndex, aj.curCol, g,false);
				   }
				}
			  }
			}

			

		}
		aj.ta.repaint();
		return true; 
	}
	public void setchar(int c,int pos, int zoom, asciiJam aj){}
    public void add(int x, int y, int dir){}
    public void identify(int x, int y, int z, asciiJam ajam){}

    public boolean handleEvent(Event e){
	 if(e.target instanceof Button){
        if(e.id==1001)
            try{
            aj.getAppletContext().showDocument(new URL(env.helpFillURL),"_blank");
            }catch(Exception ex){}
     }else if(e.target instanceof TextField){
        if(e.id==402){
            String s = ((TextField)e.target).getText();
            if(s!=null&&s.length()>0){
                aj.fillChar = s.charAt(0);
                ((TextField)e.target).setText(s.substring(0,1));
            }
        }
        else if(e.id==401){
            ((TextField)e.target).setText("");
        }
        return false;
     }
     else if(e.target instanceof Checkbox){
        if(e.id==1001){
            if( ((Checkbox)e.target).getLabel().equals("Line") ){
					lbl.show();
					chCh.show();
                    fill = 0; 
			}else{
					lbl.hide();
					chCh.hide();
				fill = 1;
			}
        }
     }else if(e.target instanceof Choice){
		    aj.fillChar = ((Choice)e.target).getSelectedItem().charAt(0);
			return false;
     }
     return true;
  }

}
