package ei.engine;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JButton;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Properties;

import javax.swing.JLabel;

public class LWJGLPropertiesDialog implements ActionListener{
	public static final int SOW_ONCE = 0;
	public static final int SOW_ALWAYS = 1;
	
	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="57,133"
	private JCheckBox fullscreen = null;
	private JPanel jPanel = null;
	private JComboBox resulution = null;
	private JComboBox pixelres = null;
	private JComboBox freq = null;
	private JButton ok = null;
	private JButton cancel = null;
	
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	
	private LWJGLGameWindow window;
	private DisplayMode[] display;
	
	public static void main(String[] args){

	}
	
	public LWJGLPropertiesDialog(LWJGLGameWindow window, int status){
		try {
			display = Display.getAvailableDisplayModes();
		} catch (LWJGLException e) {
			e.printStackTrace();
		}
		this.window = window;
		Properties settings = loadProp();
		getJFrame(display);
		
		if(settings != null){
			setSelected(resulution,settings.getProperty("Res"));
			setSelected(pixelres,settings.getProperty("Pixel"));
			setSelected(freq,settings.getProperty("Freq"));
			fullscreen.setSelected(Boolean.parseBoolean(settings.getProperty("Fullscreen")));
			if(status == SOW_ONCE){
				actionPerformed(new ActionEvent(ok,0,"loaded settings"));
			}
			else if(status == SOW_ALWAYS){
				jFrame.setVisible(true);
			}
		}
		else{
			jFrame.setVisible(true);
		}
	}
	
	private void setSelected(JComboBox box, String sel){
		for(int i=0; i<box.getItemCount() ;i++){
			if(((String)box.getItemAt(i)).equals(sel)){
				box.setSelectedIndex(i);
			}
		}
	}
	
	private Properties loadProp(){
	    Properties prop = new Properties();
	    FileInputStream fis;
		try {
			fis = new FileInputStream("window.ini");
			prop.load(fis);
		} catch (Exception e) {
			prop = null;
		}
		return prop;
	}
	
	private void saveProp(Properties p){
	    FileOutputStream fos;
		try {
			fos = new FileOutputStream("window.ini");
			p.store(fos, "LWJGLWindow");
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}	    
	}
	
	public void actionPerformed(ActionEvent event) {
		if(event.getSource() == ok){
			DisplayMode dm = findDisplay();
			if(dm != null){
				jFrame.setVisible(false);
				jFrame = null;
				Properties settings = new Properties();
				settings.setProperty("Res",dm.getWidth()+"x"+dm.getHeight());
				settings.setProperty("Pixel",""+dm.getBitsPerPixel());
				settings.setProperty("Freq",""+dm.getFrequency());
				settings.setProperty("Fullscreen",""+fullscreen.isSelected());
				saveProp(settings);
				window.createWindow(dm, fullscreen.isSelected());
			}
		}
		else if(event.getSource() == cancel){
			System.exit(0);
		}
	}
	
	private DisplayMode findDisplay(){
		for(int i=0; i<display.length ;i++){
			if(((String)resulution.getSelectedItem()).equals(display[i].getWidth()+"x"+display[i].getHeight()) &&
					Integer.parseInt((String)pixelres.getSelectedItem()) == display[i].getBitsPerPixel() &&
					Integer.parseInt((String)freq.getSelectedItem()) == display[i].getFrequency()){
				return display[i];
			}
		}
		return null;
	}
	
	/**
	 * This method initializes jFrame	
	 * 	
	 * @return javax.swing.JFrame	
	 */
	private JFrame getJFrame(DisplayMode[] d) {
		if (jFrame == null) {
			jFrame = new JFrame();			
			jFrame.setContentPane(getJPanel(d));
			jFrame.pack();
			Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
			Dimension size = jFrame.getSize();
			jFrame.setLocation(dim.width/2-size.width/2,dim.height/2-size.height/2);
		}
		return jFrame;
	}
	/**
	 * This method initializes fullscreen	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getFullscreen() {
		if (fullscreen == null) {
			fullscreen = new JCheckBox();
			fullscreen.setText("Fullscreen");
			fullscreen.setName("fullscreen");
		}
		return fullscreen;
	}
	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel(DisplayMode[] d) {
		if (jPanel == null) {
			jLabel2 = new JLabel();
			jLabel2.setText("Freq: ");
			jLabel1 = new JLabel();
			jLabel1.setText("Pixel");
			jLabel = new JLabel();
			jLabel.setText("Resulution:");
			jPanel = new JPanel();
			jPanel.setLayout(new FlowLayout());
			jPanel.add(jLabel, null);
			jPanel.add(getResulutionButton(d), null);
			jPanel.add(jLabel1, null);
			jPanel.add(getPixelresButton(d), null);
			jPanel.add(jLabel2, null);
			jPanel.add(getFreqButton(d), null);
			jPanel.add(getFullscreen(), null);
			jPanel.add(getOk(), null);
			jPanel.add(getCancel(), null);
		}
		return jPanel;
	}
	/**
	 * This method initializes resulution	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getResulutionButton(DisplayMode[] d) {
		if (resulution == null) {
			resulution = new JComboBox();
			resulution.setName("resulution");
			setList(resulution, getResulution(display));
			resulution.addActionListener(this);
		}
		return resulution;
	}
	
	/**
	 * This method initializes pixelres	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getPixelresButton(DisplayMode[] d) {
		if (pixelres == null) {
			pixelres = new JComboBox();
			pixelres.setName("pixelres");
			setList(pixelres, getPixelres(display));
			pixelres.addActionListener(this);
		}
		return pixelres;
	}
	/**
	 * This method initializes freq	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getFreqButton(DisplayMode[] d) {
		if (freq == null) {
			freq = new JComboBox();
			freq.setName("freq");
			setList(freq, getFreq(display));
			freq.addActionListener(this);
		}
		return freq;
	}
	/**
	 * This method initializes ok	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getOk() {
		if (ok == null) {
			ok = new JButton();
			ok.setText("OK");
			ok.addActionListener(this);
		}
		return ok;
	}
	/**
	 * This method initializes cancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getCancel() {
		if (cancel == null) {
			cancel = new JButton();
			cancel.setText("Cancel");
			cancel.addActionListener(this);
		}
		return cancel;
	}
	
	
	private String[] getResulution(DisplayMode[] d){
		ArrayList<String> list = new ArrayList<String>();
		for(int i=0; i<d.length ;i++){
			if(!list.contains(d[i].getWidth()+"x"+d[i].getHeight())){
				list.add(d[i].getWidth()+"x"+d[i].getHeight());
			}
		}
		
		String[] res = new String[list.size()];
		for(int i=0; i<list.size() ;i++){
			res[i] = list.get(i);
		}
		sortString(res);
		return res;
	}
	
	private String[] getPixelres(DisplayMode[] d){
		ArrayList<String> list = new ArrayList<String>();
		for(int i=0; i<d.length ;i++){
			if(!list.contains(""+d[i].getBitsPerPixel())){
				list.add(""+d[i].getBitsPerPixel());
			}
		}
		
		String[] pixel = new String[list.size()];
		for(int i=0; i<list.size() ;i++){
			pixel[i] = list.get(i);
		}
		sortString(pixel);
		return pixel;
	}
	
	private String[] getFreq(DisplayMode[] d){
		ArrayList<String> list = new ArrayList<String>();
		for(int i=0; i<d.length ;i++){
			if(!list.contains(""+d[i].getFrequency())){
				list.add(""+d[i].getFrequency());
			}
		}
		
		String[] freq = new String[list.size()];
		for(int i=0; i<list.size() ;i++){
			freq[i] = list.get(i);
		}
		sortString(freq);
		return freq;
	}
	
	private void setList(JComboBox box, String[] list){
		box.removeAllItems();
		for(int i=0; i<list.length ;i++){
			box.addItem(list[i]);
		}
	}
	
	private void sortString(String[] list){
		for(int i=0; i<list.length ;i++){
			for(int j=i; j<list.length ;j++){
				if(Integer.parseInt(list[i].replace("x", "")) > Integer.parseInt(list[j].replace("x", ""))){
					String temp = list[i];
					list[i] = list[j];
					list[j] = temp;
				}
			}
		}
	}
	
}
