import java.awt.Color;
import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.io.*;

public class LinkNodes extends JPanel 
{
	private static LinkedList UserList = new LinkedList();
	
	private static BufferedReader stdin = 
		new BufferedReader( new InputStreamReader( System.in ) );
    public static void main(String[] args) throws IOException
    {
        String input;
        for(;;)
		{
			System.out.println("Choose option: (C)reate Node, (P)rint List, or (E)xit and Plot all nodes");
			input = stdin.readLine();
			if (input.equals("C") || input.equals("c"))
			{
				String data;
				int positionX;
				int positionY;
				System.out.println("Input Node data (Max of 5 characters)");
				data = stdin.readLine();
				System.out.println("Input Node positionX");
				positionX = Integer.parseInt(stdin.readLine());
				System.out.println("Input Node positionY");
				positionY = Integer.parseInt(stdin.readLine());
				Node n = new Node(data, null, positionX, positionY);
				UserList.insert(n);
			}
			else if (input.equals("P") || input.equals("p"))
			{
				UserList.print();
			}
			else if (input.equals("E") || input.equals("e"))
			{
				break;
			}
		}
		JFrame frame = new JFrame("LinkNodes");
        frame.add(new LinkNodes());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(280, 240);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    public void paint(Graphics g) 
    {
        Graphics2D g2d = (Graphics2D) g;
        RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        rh.put(RenderingHints.KEY_RENDERING,
               RenderingHints.VALUE_RENDER_QUALITY);
        g2d.setRenderingHints(rh);
        plotMultipleNodes(g2d, UserList.getHead());
    }
    public void plotMultipleNodes(Graphics g, Node n)
    {
    	Graphics2D g2d = (Graphics2D) g;
        for(Node plot = n; plot != null; plot = plot.getNext())
        {
        	plotNode(g2d, plot);
        	//adjustPosition(plot);
        	System.out.println(plot.getData());
        	System.out.println("x => " + plot.getPositionX());
        }
    }
    public void makeArrow(Graphics g, Node n)
    {
    	Graphics2D g2d = (Graphics2D) g;
    	int dx = n.getNext().getPositionX() - n.getPositionX();
    	int dy = n.getNext().getPositionY() - n.getPositionY();
    	g2d.drawLine(n.getPositionX() + 50, n.getPositionY() + 20, n.getNext().getPositionX(), n.getNext().getPositionY());
    	// ^Plots actual line between the nodes.
    	double frac = .95;
    	g2d.drawLine(n.getPositionX() + (int)(frac*dx - (1-frac)*dy), n.getPositionY() + (int)(frac*dy + (1-frac)*dx), n.getNext().getPositionX(), n.getNext().getPositionY());
    	g2d.drawLine(n.getPositionX() + (int)(frac*dx + (1-frac)*dy), n.getPositionY() + (int)(frac*dy - (1-frac)*dx), n.getNext().getPositionX(), n.getNext().getPositionY());
    }
    public void plotNode(Graphics g, Node n)
    {
    	Graphics2D g2d = (Graphics2D) g;
    	System.out.println(n.getPositionX() + " PLOTNODE CHECK X");
    	System.out.println(n.getPositionY() + " PLOTNODE CHECK Y");
    	g2d.drawRect(n.getPositionX(), n.getPositionY(), 50, 20);
    	g2d.drawString(n.getData(), n.getPositionX() + 5, n.getPositionY() + 15);
    	if(n.getNext() != null)
    	{
	    	//g2d.drawLine(n.getPositionX() + 50, n.getPositionY() + 20, n.getNext().getPositionX(), n.getNext().getPositionY());
    		makeArrow(g2d, n);
    	}
    	System.out.println(n.getPositionX());
    	System.out.println(n.getPositionY());
    	}
    public void adjustPosition(Node n)
    {
    	if (n.getPositionX() >= 170)
    	{
    		n.setPositionX(20);
    		n.setPositionY(n.getPositionY() + 50);
    	}
    	else
    	{
    		n.setPositionX(n.getPositionX() + 75);
    	}
    }
}