import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import javax.swing.border.*;
import java.sql.*;
import oracle.jdbc.*;
import oracle.sql.*;
import java.util.*;


public class Teljesites extends JDialog implements ActionListener, ItemListener, ListSelectionListener {
	
	private final String strOK = "OK";
    private final String strMegsem = "M\u00e9gsem";
    private JButton okGomb;
    private JButton megsemGomb;
	private JPanel gombPanel= null;
	private JPanel adatPanel = null;
	private JPanel adatPanel1 = null;
	private JPanel adatPanel2 = null;
	private String m_loginName;
	private String m_neptunName;
	private JTextField m_loginNameBox;
    private JComboBox m_neptunBox;
    private JPanel kontener;
    private Menu1 szuloPanel;
    private JList lista1;
    private JList lista2;
	private JScrollPane listagorgeto;
	private JScrollPane listagorgeto1;
	private JButton hozzaadGomb;
    private JButton torolGomb;
    private JButton keresGomb;
	private JPanel gombPanel1= null;
	private JPanel gombPanel2= null;
	private JPanel gombPanel3= null;
	private DefaultListModel listModel1=null;
	private DefaultListModel listModel2=null;
	private int tr2length=0;
	String[] tr2start=null;
	String[] tr2=null;
	String kod;
	
	
    public Teljesites(JFrame parent) {
    	super(parent, "Teljes\u00edt\u00e9sek", true);
    	
    	szuloPanel = (Menu1)parent;
    	
    	
        
    	setDefaultCloseOperation(HIDE_ON_CLOSE);
        this.setBounds(285,220,470,370);
        listModel1 = new DefaultListModel();
	    listModel2 = new DefaultListModel();

  //---------------------  	
    	adatPanel = new JPanel();
    	adatPanel.add(new JLabel("Hallg.n\u00e9v:     "));
        m_loginNameBox = new JTextField(16);
	    adatPanel.add(m_loginNameBox);
	    
	    adatPanel.add(new JLabel("       Neptun k\u00f3d: "));
	    m_neptunBox = new JComboBox();
	    m_neptunBox.setPreferredSize(new Dimension(80, 20));
	    m_neptunBox.addItem("");
	  
	    try{
    	Statement statement = szuloPanel.con.createStatement();       
        ResultSet res = statement.executeQuery("SELECT N_kod FROM Hallgato Order by N_kod" );
        String s;
        while ( res.next() )
        {
            s = res.getString(1);
            m_neptunBox.addItem(s);
        }
    	}
    	catch(SQLException e){
    	
    		JOptionPane.showMessageDialog(this,e.getMessage(),"HIBA",JOptionPane.ERROR_MESSAGE);
    	}
    	m_neptunBox.addItemListener(this);
	    adatPanel.add(m_neptunBox);
	    adatPanel1 = new JPanel();
	    adatPanel1.add(new JLabel("          \u00d6sszes t\u00e1rgy:                       "));
	    
	    adatPanel2 = new JPanel();
	    adatPanel2.add(new JLabel("              Teljes\u00edtett t\u00e1rgy:             "));
//------------------------	    
    	okGomb = new JButton(strOK);
	    okGomb.addActionListener(this);
	    megsemGomb = new JButton(strMegsem);
	    megsemGomb.addActionListener(this);
	
      	gombPanel = new JPanel(new FlowLayout( FlowLayout.LEFT));
	    gombPanel.add(okGomb);
	    gombPanel.add(megsemGomb);
//-------------------------	
	    lista1 = new JList(listModel1);	
	    lista1.setSelectedIndex(0);			    
        lista1.setFixedCellWidth(110);
        lista1.setFixedCellHeight(25);
        lista1.addListSelectionListener(this);

		lista2 = new JList(listModel2);
		lista1.setSelectedIndex(0);			
	    lista2.setFixedCellWidth(110);
	    lista2.setFixedCellHeight(25);	 
    	lista2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    	lista1.addListSelectionListener(this);

//----------------------	    
	    hozzaadGomb = new JButton(" >> ");
	    hozzaadGomb.addActionListener(this);
	    HireListener hireListener = new HireListener(hozzaadGomb);
         hozzaadGomb.addActionListener(hireListener);

	    torolGomb = new JButton(" << ");
	    torolGomb.addActionListener(new FireListener());
	    gombPanel2 = new JPanel();
	    gombPanel2.setLayout(new BoxLayout(gombPanel2, BoxLayout.Y_AXIS));
	    gombPanel2.add( hozzaadGomb);
	    gombPanel2.add( Box.createRigidArea(new Dimension(0,5)));
	    gombPanel2.add(torolGomb);
//----------------------	

        gombPanel1 = new JPanel();
	    gombPanel1.setLayout(new FlowLayout());
        listagorgeto = new JScrollPane(lista1, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        listagorgeto1 = new JScrollPane(lista2, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
	    gombPanel3 = new JPanel();
	    gombPanel3.setLayout(new FlowLayout()); 
	    gombPanel1.add(listagorgeto);

			gombPanel3.add(listagorgeto1);

        kontener = new JPanel(new FlowLayout());
	    kontener.add(adatPanel);
	    kontener.add(adatPanel1);
	    kontener.add(adatPanel2);
	    kontener.add(gombPanel1);
	    kontener.add(gombPanel2);
	    kontener.add(gombPanel3);
	    kontener.add(gombPanel);
	    
	    
	    getContentPane().add(kontener);
	  
        setLocationRelativeTo(parent);
   }
    
      public void actionPerformed(ActionEvent ae){ 
    	String str = ae.getActionCommand();
    	if(str.equals(strMegsem)){
  		  szuloPanel.removePanel(this);
  	
 	    } 
 	    if(str.equals(strOK)){
 	       try{
 	       	Enumeration <String> e = (Enumeration <String>)listModel2.elements();
 	       	while( e.hasMoreElements() ){
 	       		String s=e.nextElement();
 	       		CallableStatement cs = szuloPanel.con.prepareCall("{call targy_beszur(?, ?)}");
                   cs.setString(1,s);
                   cs.setString(2,kod);
                   
                   
                   ResultSet rs = cs.executeQuery();
 	       	}

    	PreparedStatement statement1 = szuloPanel.con.prepareStatement("SELECT Teljesitesek from Leckekonyv l, hallgato h where l.n_kod=h.n_kod and h.n_kod=?" );       
	    statement1.setString(1,kod);
        
        ResultSet res1 = statement1.executeQuery();
          while ( res1.next() )
            {
            	ARRAY array = ((OracleResultSet)res1).getARRAY ("Teljesitesek");
             // System.out.println(array);

                String[] tr = (String[])array.getArray ();
             // System.out.println(tr.length);

            
            }
 	       	
 	       }	
 	       catch(SQLException exe){
 	    	
 	    	   JOptionPane.showMessageDialog(this,exe.getMessage(),"HIBA",JOptionPane.ERROR_MESSAGE);
 	        }
 	        szuloPanel.removePanel(this);
 	    }
    
    
    }
    public void itemStateChanged(ItemEvent e){
    //	System.out.println(e.getItem());
    	kod=(String)e.getItem();
    String[] tr=null;
    listModel1.removeAllElements();
    listModel2.removeAllElements();
    
        if(e.getStateChange()== ItemEvent.DESELECTED)
			return;
        if(e.getItem().equals(""))
        {
        	m_loginNameBox.setText("");
        	listModel1.removeAllElements();
            listModel2.removeAllElements();
        	return;
        }
    	else{
    	try{
    	PreparedStatement statement = szuloPanel.con.prepareStatement("SELECT Nev FROM Hallgato WHERE N_kod = ? " );       
        statement.setString(1, (String)e.getItem());
        ResultSet res = statement.executeQuery();
        
        String s;
        while ( res.next() )
        {
            s = res.getString(1);
            m_loginNameBox.setText(s);
        
    	}
    	}
    	catch(SQLException ex){
      	JOptionPane.showMessageDialog(this,ex.getMessage(),"HIBA",JOptionPane.ERROR_MESSAGE);
    	}
    	
    	//Lista1 feltöltése
    	
	   	try{
    	PreparedStatement statement2 = szuloPanel.con.prepareStatement("select Targyak from KotTantargy k where k.Szak_kod IN (select l.Szak_kod from Leckekonyv l where n_kod= ? )");       
	    statement2.setString(1,kod);
       
        ResultSet res2 = statement2.executeQuery();
        
            while ( res2.next() )
            {
            	ARRAY array2 = ((OracleResultSet)res2).getARRAY ("Targyak");
              //System.out.println(array2);

               tr2 = (String[])array2.getArray ();
               tr2start=tr2;
               tr2length=tr2.length;
          //     System.out.println(tr2length);
               for(int i= 0; i<tr2length; i++){
	    //            System.out.println(tr2[i]);
	                listModel1.addElement(tr2[i]);
               }
            
            }
	   	}
    	catch(SQLException exp){
    	  JOptionPane.showMessageDialog(this,exp.getMessage(),"HIBA",JOptionPane.ERROR_MESSAGE);
    	}
    	
 //Lista2 feltöltése
 
 	try{
    	PreparedStatement statement1 = szuloPanel.con.prepareStatement("SELECT Teljesitesek from Leckekonyv where n_kod = ? " );       
	    statement1.setString(1,kod);
      
        
        ResultSet res1 = statement1.executeQuery();
          while ( res1.next() )
            {
            	ARRAY array = ((OracleResultSet)res1).getARRAY ("Teljesitesek");
             // System.out.println(array);

                tr = (String[])array.getArray ();
                Arrays.sort(tr);
         //     System.out.println(tr);

               for(int i= 0; i<tr.length; i++){
	             //  System.out.println(tr[i]);
	               
	                listModel2.addElement(tr[i]);
	               
               }
            
            }
	   	}
    	catch(SQLException exx){
    	   JOptionPane.showMessageDialog(this,exx.getMessage(),"HIBA",JOptionPane.ERROR_MESSAGE);
    	}
    	
    	}
    }
    public void valueChanged(ListSelectionEvent le){
    	
    }
    
    class FireListener implements ActionListener {
    
        public void actionPerformed(ActionEvent e) {
           
            int index = lista2.getSelectedIndex();
            listModel2.remove(index);

            int size = listModel2.getSize();

            if (size == 0) { 
               torolGomb.setEnabled(false);

            } else { 
                if (index == listModel2.getSize()) {
                    index--;
                }

                lista2.setSelectedIndex(index);
                lista2.ensureIndexIsVisible(index);
            }
        }
    }
//-----------------------
    class HireListener implements ActionListener {
       private boolean alreadyEnabled = false;
        private JButton button;
        	
        public HireListener(JButton button) {
            this.button = button;
        }
     
        public void actionPerformed(ActionEvent ea) {
        	if(!torolGomb.isEnabled()){
        		torolGomb.setEnabled(true);

        	}
            
        	for (int index: lista1.getSelectedIndices()){
        		
        		String s = (String)listModel1.getElementAt(index);
        		if(alreadyInList(s))
        			Toolkit.getDefaultToolkit().beep();
        	    else{
        		listModel2.addElement(s);
        		lista2.setSelectedIndex(index);
                lista2.ensureIndexIsVisible(index);
        	    }
        		
        	}
        	
           }
       	}
       	
 

    protected boolean alreadyInList(String name) {
            return listModel2.contains(name);
    }

   
    
 

}