package GUI;

import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

import javax.swing.event.InternalFrameListener;

import ij.*;
import GUI.FileChooser;

public class internalFrame extends JInternalFrame  {
	
//	public JInternalFrame jinternalFrame;
	public JTextArea textArea;
	public ImageIcon imageIcon;
	public Image image;
	public ImagePlus imp;
	public JLabel jlabel;
	public JScrollPane jscrollPane;
	public int number;
	//public int numberResult;
	public int numberRes;
	private boolean draw = false;
	private boolean draw2 = true;
	public int width;
	private int height;
	private int originHeight;
	public int[][] hist;
	final int scale = 20;
	protected GridBagLayout gbl;
	protected GridBagConstraints gbc;	
	public int type; //0 - open image 1 - create image
	
/*	public internalFrame(String name, int numberResult) {
		this.numberResult = numberResult;
		
		new JInternalFrame(name);
		this.setTitle(name);
		this.resizable = false;
		this.maximizable = false;
		this.closable = false;
		this.iconable = true;
		this.setBounds(0 + numberResult*200, 0, 200, 480);
		//this.setBounds(10 , 10 + number*20, 300 + number*20, 400 + number*20);
		//this.setBounds(0 , 0 , 200 , 240 );
		////

		gbl = new GridBagLayout();
		gbc = new GridBagConstraints();

		getContentPane().setLayout(gbl);
		getContentPane().setLayout( new GridBagLayout() );
		gbc = new GridBagConstraints();
	//	gbc.anchor = GridBagConstraints.LINE_END;
	//	gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets( 2, 2, 2, 2 );
		getContentPane().add(getDiffLines(0,0), gbc );
		gbc.ipadx = 40;
		getContentPane().add( getCbDiffLines(0,1), gbc );
		getContentPane().add( getDiffWords(0,2), gbc );
		//gbc.insets = new Insets( 0, 200, 0, 200 );
		gbc.ipadx = 40;
		getContentPane().add( getCbDiffWords(0,3), gbc );
		getContentPane().add(getDensityHandwr(0,4), gbc );
		gbc.ipadx = 40;
		getContentPane().add(getCbDensityHandwr(0,5), gbc );
		getContentPane().add(getHeightHandwr(0,6), gbc );
		gbc.ipadx = 40;
		getContentPane().add(getCbHeightHandwr(0,7), gbc );
		getContentPane().add(getRatioWordsSegments(0,8), gbc );
		gbc.ipadx = 40;
		getContentPane().add(getCbRatioWordsSegments(0,9), gbc );
		getContentPane().add(getThreeElementsChar(0,10), gbc );
		gbc.ipadx = 40;
		getContentPane().add(getCbThreeElementsChar1(0,11), gbc );
		gbc.ipadx = 40;
		getContentPane().add(getCbThreeElementsChar2(0,12), gbc );
		gbc.ipadx = 40;
		getContentPane().add(getCbThreeElementsChar3(0,13), gbc );
		
		getContentPane().add(getRatioThreeElementsChar(0,14), gbc );
		getContentPane().add(getCbRatioThreeElementsChar1(0,15), gbc );
		getContentPane().add(getCbRatioThreeElementsChar2(0,16), gbc );
		getContentPane().add(getCbRatioThreeElementsChar3(0,17), gbc );		
		
		getContentPane().add(getRatioLinesHeight(0,18), gbc );
		getContentPane().add(getCbRatioLinesHeight(0,19), gbc );		
		this.setVisible(true);
		//getContentPane().setLayout(gbl);
		
		mainWindow.desktop.add(this, gbc);
		this.moveToFront();
		//this.addInternalFrameListener(new listener());

	}	*/
	
	public internalFrame(String name) {
		
		new JInternalFrame(name, true, true, true, false);
		this.setTitle(name);
		this.resizable = true;
		this.maximizable = true;
		this.closable = true;
		draw = false;
		draw2 = true;
		
		this.setBounds(10, 10, 200, 100);
		//this.setSize(50, 50);
		
		//imageIcon = new ImageIcon("pics\184-d04-005.png");
		//jlabel = new JLabel(imageIcon);
		//jscrollPane = new JScrollPane(jlabel);
		//this.getContentPane().add(jscrollPane);
		

		
		//jlabel = new JLabel(imageIcon);
		//jscrollPane = new JScrollPane(jlabel);
		getContentPane().add(mainWindow.progressBar);
		this.setVisible(true);
		//this.show();
		
		mainWindow.desktop.add(this);
		this.moveToFront();
		this.addInternalFrameListener(new listener());

	}	

	public internalFrame(String name, ImagePlus imp, int number, int numberRes, int type, boolean vykreslit) {
		
		this.number = number;
		this.numberRes = numberRes;
		this.type = type;
		this.image = imp.getImage();
		this.imp = imp;
		draw = false;
		draw2 = true;
		
		if (type == 0)
			mainWindow.numberOpenImages++;
		
		mainWindow.numberImages++;
		System.out.println("number "+number);
		System.out.println("numberRes "+numberRes);
		System.out.println("type "+type);
		System.out.println("FileChooser.number "+FileChooser.number);
		System.out.println("mainWindow.numberOpenImages "+mainWindow.numberOpenImages);
		System.out.println("mainWindow.numberImages "+mainWindow.numberImages);
		
		
		new JInternalFrame(name, true, true, true, true);
		this.setTitle(name);
		this.resizable = true;
		this.maximizable = true;
		this.closable = true;
		this.iconable = true;
		
		this.setBounds(10 + number*20, 10 + number*20, 300 + number*20, 400 + number*20);
		
		imageIcon = new ImageIcon(image);
		jlabel = new JLabel(imageIcon);
		jscrollPane = new JScrollPane(jlabel);
		getContentPane().add(jscrollPane);
		this.setVisible(true);
		
		if (vykreslit) {
			mainWindow.desktop.add(this);
			this.moveToFront();
			this.addInternalFrameListener(new listener());
		}
	}
	
	public internalFrame(String name, int number, ImagePlus imp, int width, int height, int[][] hist, int numberRes, int type) {
		System.out.println(width);
		System.out.println(height);
		this.numberRes = numberRes;
		this.type = type;
		this.imp = imp;
		this.hist = hist;
		this.number = number;
		this.width = width;
		originHeight = height;
		//this.height = (int)( ((double) 300 / (double)width) * height );
		this.height = (int)( ((double) 500 / (double)width) * height );
		draw = true;
		draw2 = true;
		
		mainWindow.numberImages++;
		
		new JInternalFrame(name, true, true, true, true);
		this.setTitle(name);
		this.resizable = true;
		this.maximizable = true;
		this.closable = true;
		this.iconable = true;
		
		this.setBounds(10 + number*20, 10 + number*20, 500 + number*20, this.height+100 + number*20);

		this.setVisible(true);
		
		mainWindow.desktop.add(this);
		this.moveToFront();
		this.addInternalFrameListener(new listener());

	}		
	
	public void paint(Graphics g) {
		if (draw2)
			super.paint(g);
		if (draw) {	
			g.drawRect(50, 50, 500, height);
			g.setColor(Color.white);
			g.fillRect(51, 51, 499, height-1);
			g.setColor(Color.BLACK);
			g.drawString(Integer.toString(originHeight), 20, 60);
			g.drawString(Integer.toString(0), 50, height+50+20);
			g.drawString(Integer.toString(width), 530, height+50+20);
			double pixel = ((double) 500 / (double)width);
			//horizontal histogram
			if (hist[0].length == originHeight) {
				System.out.println("hist.length " +hist.length);
				for (int k = 0; k < hist.length; k++)
					for (int i = 0; i < hist[k].length; i++) {
						if (hist[k][i] > 0) {					
							g.drawLine(50 + ((500 / scale) * k), (int)(50+ i*pixel), (int)(50 + hist[k][i]*pixel + ((500 / scale) * k)), (int)(50+ i*pixel));
						}
					}
			} else if (hist[0].length == width) { //vertical histogram
				for (int k = 0; k < hist.length; k++)
					for (int i = 0; i < hist[k].length; i++) {
						if (hist[k][i] > 0) {					
							g.drawLine((int)(50+ i*pixel), height+50, (int)(50+ i*pixel), (int)(height+50 - hist[k][i]*pixel));
						}
					}	
			}
		}
	}	
	
	public void close(){
		this.fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_CLOSED);
	}
	
	public class listener implements InternalFrameListener{
		
	    public void internalFrameActivated( javax.swing.event.InternalFrameEvent e ) { 
	    	System.out.println("Activated "+number);
	    	System.out.println("Activated numberRes "+numberRes);
	    	System.out.println("Activated type "+type);
	    }	    
	    public void internalFrameClosed( javax.swing.event.InternalFrameEvent e ){
	    	FileChooser.deleteIntFrame(number);
	    }
	    public void internalFrameClosing( javax.swing.event.InternalFrameEvent e ) { }
	    public void internalFrameDeactivated( javax.swing.event.InternalFrameEvent e ) { }
	    public void internalFrameDeiconified( javax.swing.event.InternalFrameEvent e ) { }
	    public void internalFrameIconified( javax.swing.event.InternalFrameEvent e ) { }
	    public void internalFrameOpened( javax.swing.event.InternalFrameEvent e ) { }	
		
	}

}

