import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

import java.io.IOException;
//import com.fazecast.jSerialComm.*;

 class Gui  {
	public static void main(String[] args)  {    

Scherm scherm = new Scherm();    
scherm.Login();
scherm.FrameInstellingen();
scherm.Paneel1();  
scherm.Paneel2();
scherm.Paneel3();
scherm.Paneel4();
scherm.ToevoegenBijPaneel();


    }
}

class Scherm  extends JFrame implements ActionListener  {//KeyListener

private JFrame f1;
private JPanel p1, p2,p3,p4;
private JButton btnNaarP2,btnNaarP1,btnNaarP3,btnLogin,btnSKM;
private JPasswordField passwordField;
private JLabel label1,label2,labelWW,labelPas;
private char getWW;
private int count=0;
private boolean isCorrect = false;
//private char[] correctPass = { '0', '0', '0' ,'0'};
private String correctPass="0000";
private ImageIcon image;
private String empty="";
private Boolean blocked=false;

Scherm() {
}

public void Login(){
passwordField = new JPasswordField(4);
passwordField.setBounds(800,350,140, 30);
passwordField.addActionListener(this);

labelWW = new JLabel();
labelWW.setText("ww: ");
labelWW.setBounds(690,340,100, 40);
labelWW.setFont(new Font("Serif",Font.BOLD,30));

 image= new ImageIcon(getClass().getResource("passcan.jpg"));
labelPas= new JLabel(image);
labelPas.setBounds(300,350,1200,530);


}

public void FrameInstellingen(){
f1 = new JFrame("GUI");
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setSize(1900,1000);
f1.setVisible(true);

}


public void Paneel1(){ 
p1= new JPanel();
p1.setBackground(Color.CYAN);
f1.add(p1);
//button gemaakt met een coordinaat,grootte,style.
btnNaarP2 = new JButton("Verder");
btnNaarP2.setBounds(800,350,140, 40);   
btnNaarP2.setFont(new Font("Calibri",Font.PLAIN,30));
btnNaarP2.addActionListener(this);


//label gemaakt met een coordinaat,grootte,style.
label1=new JLabel();     
label1.setText("Welkom bij de bank");
label1.setBounds(700, 300, 500, 50);
label1.setFont(new Font("Serif",Font.ITALIC,40));

}


public void Paneel2(){
    p2 = new JPanel();
p2.setBackground(Color.CYAN);
f1.add(p2);

//button gemaakt met een coordinaat,grootte,style.
btnNaarP1= new JButton("HOME");   
btnNaarP1.setBounds(100,900,140,40);   
btnNaarP1.setFont(new Font("Calibri",Font.PLAIN,30));
btnNaarP1.addActionListener(this);


//button gemaakt met een coordinaat,grootte,style.
btnNaarP3= new JButton("Volgende");   
btnNaarP3.setBounds(800,900,180,40);   
btnNaarP3.setFont(new Font("Calibri",Font.PLAIN,30));
btnNaarP3.addActionListener(this);

label2=new JLabel("Scan uw pas");     
label2.setBounds(800, 300, 500, 50);
label2.setFont(new Font("Serif",Font.ITALIC,40));





}


public void Paneel3(){  //Paneel 3

    p3 = new JPanel();
p3.setBackground(Color.CYAN);


btnLogin= new JButton("Login");
btnLogin.setBounds(1000,340,140, 40);
btnLogin.setFont(new Font("Serif",Font.BOLD,30));
btnLogin.addActionListener(this);
}



public void Paneel4(){
p4= new JPanel();
p4.setBackground(Color.CYAN);

btnSKM= new JButton("50 euro opnemen");
btnSKM.setBounds(100,840,340, 40);
btnSKM.setFont(new Font("Serif",Font.BOLD,30));
btnSKM.addActionListener(this);

}



public void ToevoegenBijPaneel(){ //alles wordt hier bij de panelen toegevoegd.
    
p1.setLayout(null);
p2.setLayout(null);
p3.setLayout(null);
p4.setLayout(null);
p1.add(label1);
p1.add(btnNaarP2);
p2.add(btnNaarP3);
p2.add(label2);
p2.add(labelPas);
p3.add(passwordField); 
p3.add(labelWW);
p3.add(btnLogin);
p4.add(btnSKM);


}



    public void actionPerformed(ActionEvent ae) {// In deze method zorgt je ervoor dat de knoppen iets doen als je erop drukt.
        String action = ae.getActionCommand();



//String getWW = String.valueOf(passwordField.getPassword());
char []passwordLezer=passwordField.getPassword();
String getWW= new String(passwordLezer);// van char array naar string



if (ae.getSource()== btnNaarP2) {  

p1.setVisible(false);
p3.setVisible(false);
f1.validate();
f1.repaint();
f1.invalidate();
p2.add(btnNaarP1);
p2.setVisible(true);
}


if (ae.getSource()== btnNaarP1) {  

p2.setVisible(false);
p3.setVisible(false);
p4.setVisible(false);
f1.validate();
f1.repaint();
f1.invalidate();
p1.setVisible(true);
}

if (ae.getSource()== btnNaarP3) {  

p1.setVisible(false);
p2.setVisible(false);
f1.validate();
f1.repaint();
f1.invalidate();
p3.add(btnNaarP1);
f1.getContentPane().add(p3);
p3.setVisible(true);
}


 if(ae.getSource()==btnLogin) {  
passwordField.setText("");
   System.out.print("\n\njouw ingvoerde pincode: ");
     System.out.println(getWW);

        if(getWW.equals(correctPass) && blocked==true ){// dit is na het blokeren van de pas
                 JOptionPane.showMessageDialog(null, "Incorrect password");
            }

          if(getWW.equals(correctPass) && blocked==false){//als de WW goed is en de pas is niet geblokeerd.
        count=0;
        System.out.println("Incorrecte inlogpogingen: "+ count);
    JOptionPane.showMessageDialog(null, "Correct password");
    p1.setVisible(false);
    p2.setVisible(false);
    p3.setVisible(false);
    f1.validate();
    f1.repaint();
    f1.invalidate();
    p4.add(btnNaarP1);
    f1.getContentPane().add(p4);
    p4.setVisible(true);
                } 

                if(getWW.equals(empty)){
                    count=count-1;
                     JOptionPane.showMessageDialog(null, "ww mag niet leeg zijn");
                }
                if(!getWW.equals(correctPass)) {
                    count+=1;
                    JOptionPane.showMessageDialog(null, "Incorrect password");
                    System.out.println("Incorrecte inlogpogingen: "+ count);


              if(count==3){
                  JOptionPane.showMessageDialog(null, "PAS GEBLOKEERD");
                  //correctPass="%%%%";
                  blocked=true;
              }
          

           }
        



/*
 //  if(performCheck(getWW)==true){
   if(getWW.equals(correctPass)){
        count=0;
        System.out.println("Incorrecte inlogpogingen: "+ count);
    JOptionPane.showMessageDialog(null, "Correct password");
    p1.setVisible(false);
    p2.setVisible(false);
    p3.setVisible(false);
    f1.validate();
    f1.repaint();
    f1.invalidate();
    p4.add(btnNaarP1);
    f1.getContentPane().add(p4);
    p4.setVisible(true);



            if(performCheck(getWW)==false){
            count=count+1;
            System.out.println("Incorrecte inlogpogingen: "+ count);
                  }
            if(count==3){// na 3 verkeerde pogingen, wordt de pas geblokeerd.
             JOptionPane.showMessageDialog(null, "PAS GEBLOKEERD");
             correctPass[0] ='%';
             correctPass[1] ='%';
             correctPass[2] ='%';
             correctPass[3] ='%';
  
                 }


    if(performCheck(getWW)){

    JOptionPane.showMessageDialog(null, "Correct password");
    p1.setVisible(false);
    p2.setVisible(false);
    p3.setVisible(false);
    f1.validate();
    f1.repaint();
    f1.invalidate();
    p4.add(btnNaarP1);
    f1.getContentPane().add(p4);
    p4.setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(null, "Incorrect password");
              
 
}
   System.out.println("ww klopt:"+ performCheck(getWW));//print of het wachtwoord true of false is




*/

}


if(ae.getSource()==btnSKM){
     JOptionPane.showMessageDialog(null, "U krijgt nu 50 euro");
}




    }




/*
  boolean performCheck(char[] input) {
       

 
        
        if (input.length != correctPass.length ) {
            isCorrect = false;
        }
        if (Arrays.equals(input, correctPass)) {
            isCorrect = true;
        }


        return isCorrect;
    }
*/



}




/*
public class Serial {

    public static void listenSerial() {
		
		SerialPort comPort = SerialPort.getCommPort("COM4");
		
		//set the baud rate to 9600 (same as the Arduino)
		comPort.setBaudRate(9600);
		
		//open the port
		comPort.openPort();
		
		//create a listener and start listening
		comPort.addDataListener(new SerialPortDataListener() {
			@Override
			public int getListeningEvents() { 
				return SerialPort.LISTENING_EVENT_DATA_AVAILABLE; 
			}
			@Override
			public void serialEvent(SerialPortEvent event)
			{
				if (event.getEventType() != SerialPort.LISTENING_EVENT_DATA_AVAILABLE)
				return; //wait until we receive data
			
				byte[] newData = new byte[comPort.bytesAvailable()]; //receive incoming bytes
				comPort.readBytes(newData, newData.length); //read incoming bytes
				String serialData = new String(newData); //convert bytes to string
			  
				//print string received from the Arduino
				System.out.println(serialData);
			}
		});
	}

}*/




