import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class MenuBarControll implements ActionListener {

	private JMenuItem open;
	private JMenuItem log_in;
	
	private DB_Interface di;
	
	MenuBarControll(JMenuItem open, JMenuItem log_in)
	{
		this.open = open;
		this.log_in = log_in;
		
		System.out.println("MenuBar Controller »ý¼º ¿Ï·á..");
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == open)
			try {
				open_file();
			} catch (HeadlessException | SQLException e1) {
				e1.printStackTrace();
			}
		else if(e.getSource() == log_in)
			log_in();
	}
	
	private void log_in() {
		// ·Î±×ÀÎ Ã¢ ¶ç¿ò
		JFrame frame = new JFrame();
		JPanel panel = new JPanel();
		JLabel idLabel = new JLabel("»ç¿ø¸í");
		JLabel pwdLabel = new JLabel("»ç¿ø¹øÈ£");
		JTextField idInput = new JTextField();
		JPasswordField pwdInput = new JPasswordField();
		JButton loginButton = new JButton("·Î±×ÀÎ");

		panel.setLayout(null);
		idLabel.setBounds(20, 10, 60, 30);
		pwdLabel.setBounds(20, 50, 60, 30);
		idInput.setBounds(100, 10, 80, 30);
		pwdInput.setBounds(100, 50, 80, 30);
		loginButton.setBounds(200, 25, 80, 35);

		panel.add(idLabel);
		panel.add(pwdLabel);
		panel.add(idInput);
		panel.add(pwdInput);
		panel.add(loginButton);

		frame.add(panel);
		frame.setTitle("Á÷¿ø ·Î±×ÀÎ");
		frame.setSize(320, 130);
		frame.setVisible(true);

		// ·Î±×ÀÎ ¹öÆ°¿¡ ¸®½º³Ê µî·Ï
		loginButton.addActionListener(new ActionListener() {
			@SuppressWarnings("static-access")
			public void actionPerformed(ActionEvent e) {
				
				if (e.getSource() == loginButton) {
					
					String id, pass;
					id = idInput.getText();
					pass = new String(pwdInput.getPassword());

					// Á¢¼Ó ¼º°ø,°ü·Ã Å×ÀÌºíÀÌ ÀÌ¹Ì Á¸ÀçÇÑ´Ù¸é, ¸Þ´ºÇöÈ² ÃÊ±âÈ­
					if (di.log_in(id, pass)) {
						frame.dispose();
						
						String query = "select count(*) from ALL_TABLES where TABLE_NAME = 'MENU'";
						ResultSet rs = di.executeQuery(query);
						int n = 0 ;						
						try {
							rs.next();
							n = rs.getInt("count(*)");
							if (n == 1)
							{
								di.initMenuPanel();							
							}
						} catch (SQLException e1) {

							di.closeStatementResultSet();
							System.out.println(e1.getMessage());
						}
						di.closeStatementResultSet();
	
					} else // Á¢¼Ó ½ÇÆÐ
					{
						JOptionPane warning = new JOptionPane();
						warning.showMessageDialog(null, "¾ø´Â °èÁ¤ÀÌ°Å³ª ºñ¹Ð¹øÈ£°¡ Æ²·È½À´Ï´Ù.", "·Î±×ÀÎ ½ÇÆÐ",
								warning.WARNING_MESSAGE);
					}
				}
				
			}
		});
	}
		
	@SuppressWarnings({ "static-access" })
	private void open_file() throws HeadlessException, SQLException {
		
		File file;
		JFileChooser chooser;
		
		chooser = new JFileChooser();
		
		int n = chooser.showOpenDialog(null);
		
		if (n == JFileChooser.APPROVE_OPTION) {
			
			file = chooser.getSelectedFile(); // ÆÄÀÏÅ½»ö±â¿¡ ¼±ÅÃµÈ ÆÄÀÏ.

			// ¼±ÅÃµÈ ÆÄÀÏÀÇ È®ÀåÀÚ°¡ txtÀÎÁö °Ë»ç
			
			String fileName = file.getName();
			String ext = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());

			// ·Î±×ÀÎ ÇØ¾ß ÆÄÀÏ ÀÎÇ² °¡´É. ·Î±×ÀÎ ¾ÈµÇ¾î ÀÖÀ¸¸é °æ°íÃ¢ ¶ç¿ò.
			if(di.getLoginStatus()){
	
				if (ext.equals("txt"))
				{
					
					if(di.readFile(file)){
							System.out.println("µ¥ÀÌÅÍº£ÀÌ½º Å×ÀÌºí¿¡ ¸ðµç µ¥ÀÌÅÍ ÀÔ·Â ¼º°ø..");
	
					}else{
						JOptionPane warning = new JOptionPane();
						warning.showMessageDialog(null, "ÀÔ·Â ½ÇÆÐ. \n1. ÆÄÀÏ ³»ÀÇ µ¥ÀÌÅÍ°¡ Á¤ÇØÁø Æ÷¸ËÀ» µû¸£Áö ¾Ê°Å³ª,"
								+ "\n2. Å×ÀÌºí°ú µ¥ÀÌÅÍ°¡ ÀÌ¹Ì µ¥ÀÌÅÍº£ÀÌ½º¿¡ Á¸ÀçÇÔ. ", 
								"warning", warning.WARNING_MESSAGE);
					}
					
				}
				else 
				{	
					
					JOptionPane warning = new JOptionPane();
					JOptionPane.showMessageDialog(null, "È®ÀåÀÚ°¡ txtÀÎ ÆÄÀÏ¸¸ ¿­ ¼ö ÀÖ½À´Ï´Ù.", "warning",
							warning.WARNING_MESSAGE);				
				}				
			}
			
			else{
				JOptionPane warning = new JOptionPane();
				JOptionPane.showMessageDialog(null, "¸ÕÀú ·Î±×ÀÎÀ» ÇÏ·Á¹«³ª.", "warning",
						warning.WARNING_MESSAGE);
			}//else
			
		}
		
	}

	public DB_Interface getDBInterface() {
		return this.di;
	}
	
	public void setDBInterface(DB_Interface di)
	{
		this.di = di;
	}
}
