package View;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.ArrayList;

import javax.swing.JPanel;

import Algorithms.Animated.BK.ExtremalLayoutCalc.LayoutType;
import Model.LayeredGraphEdge;

/**
 * A drawable representation of an edge.
 * @author kolja
 *
 */
public class EdgeView extends JPanel {
    private static final long serialVersionUID = 1L;

    private LayeredGraphEdge model;
    private LayoutType layout;
    
    public EdgeView( LayeredGraphEdge model, LayoutType lt ) {
        this.model = model;
        layout = lt;
    }
    
    @Override
    public Point getLocation()
    {
        ArrayList<Point> bps = model.getLinePoints( layout );
        double minX = bps.get( 0 ).getX();
        double minY = bps.get( 0 ).getY();
        for( Point p : bps )
        {
            minX = Math.min( minX, p.getX() );
            minY =  Math.min( minY, p.getY() );
        }
        minX -= 5;
        minY -= 5;
        return new Point( (int)minX, (int)minY );
    }
    
    @Override
    public int getWidth()
    {
        ArrayList<Point> bps = model.getLinePoints( layout );
        double max = bps.get( 0 ).getX();
        for( Point p : bps )
            max =  Math.max( max, p.getX() );
        return (int)max + 10;
    }
    
    @Override
    public int getHeight()
    {
        ArrayList<Point> bps = model.getLinePoints( layout );
        double max = bps.get( 0 ).getY();
        for( Point p : bps )
            max =  Math.max( max, p.getY() );
        return (int)max + 10;
    }
    
    @Override
    public Dimension getPreferredSize()
    {
    	return new Dimension( getWidth(), getHeight() );
    }
    
    @Override
    public void paint( Graphics g )
    {
        paintComponent( g );/*
        for( Component c : getComponents() )
            c.paint( g.create( 
                    c.getX() + 10, 
                    c.getY() + 10, 
                    Math.min( getWidth() - 10, c.getWidth() + c.getX() + 10 ), 
                    Math.min( getHeight() - 10, c.getHeight() + c.getY() + 10 ) ) );
    */}

    @Override
    public void paintComponent( Graphics g )
    {
        ((Graphics2D)g).setStroke(new BasicStroke(1));
        //System.out.println( "Clipping: x:" + g.getClip().getBounds().getX() + " y:" + g.getClip().getBounds().getY() + " w:" + g.getClip().getBounds().getWidth() + " h:" + g.getClip().getBounds().getHeight() );
        g.setColor( Color.GREEN );
        ArrayList<Point> bps = model.getLinePoints( layout );
        for( int i = 1; i < bps.size(); i++ )
        {
          //  System.out.println( "Draw a Line from (" + (int)bps.get( i - 1 ).getX() + "," + (int)bps.get( i - 1 ).getY() + ") to (" + (int)bps.get( i ).getX() + "," + (int)bps.get( i ).getY() + ")" );
            g.drawLine( (int)bps.get( i - 1 ).getX() - getLocation().x, (int)bps.get( i - 1 ).getY() - getLocation().y, (int)bps.get( i ).getX() - getLocation().x, (int)bps.get( i ).getY() - getLocation().y );
        }
        ((Graphics2D)g).fill( RenderHelper.createArrowShape( new Point( bps.get( bps.size() - 2 ).x - getLocation().x, bps.get( bps.size() - 2 ).y - getLocation().y ), new Point( bps.get( bps.size() - 1 ).x - getLocation().x, bps.get( bps.size() - 1 ).y - getLocation().y ) ) );
    }
}
