package automatvgi.widgets;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ColorSelector extends JFrame implements MouseListener,ChangeListener,ActionListener{

	private static final long serialVersionUID = 1L;

	private Color selected=Color.BLACK;
	private Palette p;
	private JSlider js;
	private int alpha=255;
	private int ref=0;
	
	public ColorSelector(Palette p){
		this.p=p;
		JPanel jp=new JPanel();
		jp.setLayout(new BorderLayout(3,1));
		js=new JSlider(0,255,255);
		js.addChangeListener(this);
		jp.add(js,BorderLayout.NORTH);
		jp.add(new Cible(),BorderLayout.CENTER);
		JButton jb=new JButton("OK");
		jb.addActionListener(this);
		jp.add(jb,BorderLayout.SOUTH);
		//jr= new JPanel
		setContentPane(jp);
		pack();
		setVisible(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
	
	public static void main(String[] args) {
		new ColorSelector(null);
	}

	
	private Color getColor(int x, int y){
		int r=0, g=0, b=0;
		int theta=(int)(Math.atan2(y,x)*180/Math.PI);
		if(theta>=-180 && theta <=-120){
			r=0; b=255;
			g=-(theta+120)*255/60;
			
		}
		else if(theta>-120 && theta <=-60){
			g=0; b=255;
			r=(theta+120)*255/60;
			
		}
		else if(theta>-60 && theta<=0){
			r=255; g=0;
			b=-theta*255/60;
			
		}
		else if(theta>0 && theta <=60){
			r=255; b=0;
			g=theta*255/60;
			
		}
		else if(theta>60 && theta <=120){
			g=255; b=0;
			r=(120-theta)*255/60;
			
		}
		else if(theta>120 && theta <=180){
			r=0; g=255;
			b=(theta-120)*255/60;
			
		}
		int rayon=(int)(Math.sqrt(x*x+y*y));
		r=(255+(r-255)*rayon/ref)*alpha/255;
		g=(255+(g-255)*rayon/ref)*alpha/255;
		b=(255+(b-255)*rayon/ref)*alpha/255;
		try{
			return new Color(r,g,b);
		}catch(IllegalArgumentException e){
			System.out.println(""+r+","+g+","+b);
		}
		return null;
	}

	private class Cible extends JPanel{

		private static final long serialVersionUID = 1L;		
		
		public Cible(){
			setPreferredSize(new Dimension(512,512));
			addMouseListener(ColorSelector.this);
		}
		
				@Override
		public void paintComponent(Graphics gr){
			ref=Math.min(getWidth(), getHeight())*2/5;
			gr.setColor(selected);
			gr.fillRect(0, 0, getWidth(), getHeight());
			for(int x=-ref;x<=ref;x++){
				int l=(int)(Math.sqrt(ref*ref-x*x));
				for(int y=-l; y<=l; y++){
					gr.setColor(getColor(x,y));
					gr.fillRect(getWidth()/2+x,getHeight()/2+y,1,1);
				}
			}
		}
	}

	public void mouseClicked(MouseEvent e) {
		int x=e.getX()-getWidth()/2, y=e.getY()-getHeight()/2;
		if(Math.sqrt(x*x+y*y)<ref){
			selected=getColor(x,y);
			repaint();
		}
	}

	public void mouseEntered(MouseEvent e) {
		//nothing
	}

	public void mouseExited(MouseEvent e) {
		//nothing
	}

	public void mousePressed(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}

	public void stateChanged(ChangeEvent e) {
		alpha=js.getValue();
		repaint();
	}

	public void actionPerformed(ActionEvent arg0) {
		if(p!=null)
		{}
		dispose();
	}
}
	
	
