/**
Khalid
*/
package org.sikuli.slides.guis;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.sikuli.slides.utils.Constants;
import org.sikuli.slides.utils.MyFileFilter;

public class MainUI extends JFrame implements ActionListener, ChangeListener, KeyListener {
	private static final long serialVersionUID = -839784598366545263L;
    private JButton btn_open;
    private JComboBox runningModeList;
    private JSlider perciseControlSlider;
    private JLabel statusLabel;
    
    
    public MainUI(){
        super("sikuli-slides");
        initUI();
    }
    
    private void initUI() {
    	JToolBar horizontal_toolbar=new JToolBar();
        horizontal_toolbar.setFloatable(false); // to make a tool bar immovable
        horizontal_toolbar.setMargin(new Insets(2, 5, 5, 5));
        horizontal_toolbar.setBackground(Color.decode("#e5e5e5"));
        horizontal_toolbar.setFocusable(false);
        add(horizontal_toolbar, BorderLayout.NORTH);
        
        ImageIcon open_file_icon=new ImageIcon(MainUI.class.getResource(Constants.RESOURCES_ICON_DIR+"powerpoint-icon.png"));
        btn_open= new JButton("Open",open_file_icon);
        btn_open.setToolTipText("Open PowerPoint file (*.pptx");
        btn_open.addActionListener(this);
        btn_open.setFocusable(false);
        btn_open.setMaximumSize(btn_open.getPreferredSize());
        horizontal_toolbar.add(btn_open);
        
        // running modes
        JLabel runningModeLabel=new JLabel("  Running Mode:");
        horizontal_toolbar.add(runningModeLabel);
        
        String[]validModes={"Action","Help","Tutorial"};
        runningModeList=new JComboBox(validModes);
        
        runningModeList.addActionListener(this);
        runningModeList.setMaximumSize(runningModeList.getPreferredSize());
        runningModeList.addActionListener(this);
        horizontal_toolbar.add(runningModeList);
        
        // precise control
        JLabel perciseControlLabel=new JLabel("  Precision Value:");
        horizontal_toolbar.add(perciseControlLabel);
        
        int initPreciseVal=(int)(Constants.MinScore*10);
        perciseControlSlider=new JSlider(JSlider.HORIZONTAL,1,10,initPreciseVal);
        perciseControlSlider.setPaintLabels(true);
        perciseControlSlider.setPaintTicks(true);
        perciseControlSlider.setMajorTickSpacing(1);
        perciseControlSlider.setMinorTickSpacing(1);
        perciseControlSlider.setMaximumSize(perciseControlSlider.getPreferredSize());
        perciseControlSlider.addChangeListener(this);
        //perciseControlSlider.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), "Precision"));
        horizontal_toolbar.add(perciseControlSlider);
        
        // status label
        statusLabel = new JLabel("");
        statusLabel.setPreferredSize(new Dimension(-1, 22));
        statusLabel.setBorder(LineBorder.createGrayLineBorder());
        add(statusLabel, BorderLayout.SOUTH);

        addKeyListener(this);
        setFocusable(true);
        
        setTitle("Sikuli-Slides");
        pack();
        setResizable(false);
        setLocationRelativeTo(null); //center the window on the screen
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
	@Override
	public void keyPressed(KeyEvent e) {
		if ((e.getKeyCode() == KeyEvent.VK_O) && 
				((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
            // open the .pptx file
            File file=openFile();
            runSikuli(file);
		}

	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		  // open .pptx file
        if (e.getSource() == btn_open) {
                // open the .pptx file
                File file=openFile();
                runSikuli(file);
        }
        else if(e.getSource()==runningModeList){
        	if(runningModeList.getSelectedIndex()==0){
        		Constants.ACTION_MODE=true;
        		Constants.HELP_MODE=false;
        		Constants.TUTORIAL_MODE=false;
        	}
        	else if(runningModeList.getSelectedIndex()==1){
        		Constants.ACTION_MODE=false;
        		Constants.HELP_MODE=true;
        		Constants.TUTORIAL_MODE=false;
        	}
        	else if(runningModeList.getSelectedIndex()==2){
        		Constants.ACTION_MODE=false;
        		Constants.HELP_MODE=false;
        		Constants.TUTORIAL_MODE=true;
        	}
        }
	}
	
	@Override
	public void stateChanged(ChangeEvent e) {
		if(e.getSource()==perciseControlSlider){
			int precision=perciseControlSlider.getValue();
			Constants.MinScore=(double)precision/10;
		}
	}
	
	private File openFile(){
        // create file chooser object
        JFileChooser fc = new JFileChooser();
        MyFileFilter myFileFilter=new MyFileFilter();
        fc.addChoosableFileFilter(myFileFilter);
        fc.setAcceptAllFileFilterUsed(false);
        fc.setFileFilter(myFileFilter);
        
        // show the open file dialog
        int returnVal = fc.showOpenDialog(MainUI.this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
                return fc.getSelectedFile();
        }
        else return null;
	}

	private static void runUI(){        
        SwingUtilities.invokeLater(new Runnable() {
                public void run(){
                        MainUI mainUI=new MainUI();
                        mainUI.setVisible(true);
                }
        });
	}
	
	private void runSikuli(File file){
		// Minimize the running JFrame window
		setState(JFrame.ICONIFIED);
		
		Main main=new Main();
		if(file!=null){
			main.doSikuliPowerPoint(file);
		}
	}
	
	public static void runGuiTool(){
		runUI();
	}

}

@@ -680,32 +680,64 @@
 .ChangeListener;
+%0A%0Aimport org.sikuli.slides.Main;
 %0Aimport org.siku
@@ -1179,26 +1179,8 @@
 %22);%0A
-        initUI();%0A
     
@@ -1203,20 +1203,29 @@
 te void 
-init
+createAndShow
 UI() %7B%0A 
@@ -3866,16 +3866,42 @@
 CLOSE);%0A
+        setVisible(true);%0A
     %7D%0A  
@@ -5937,267 +5937,8 @@
 %09%7D%0A%0A
-%09private static void runUI()%7B        %0A        SwingUtilities.invokeLater(new Runnable() %7B%0A                public void run()%7B%0A                        MainUI mainUI=new MainUI();%0A                        mainUI.setVisible(true);%0A                %7D%0A        %7D);%0A%09%7D%0A
 %09%0A%09p
@@ -6167,16 +6167,204 @@
 ()%7B%0A
-%09%09runUI(
+        SwingUtilities.invokeLater(new Runnable() %7B%0A            public void run()%7B%0A                    MainUI mainUI=new MainUI();%0A                    mainUI.createAndShowUI();%0A            %7D%0A    %7D
 );%0A%09

refactor Main UI class