package View.MainFrame;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JFrame;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;

public class MyMainFrame extends JFrame implements KeyListener {

    private MyLayeredKeyboardPanel oKeyboardPanel;

    private String sApplicationTitle = "Learning is KEYboard";

    private File oSoundFile = new File("Learning_is_keyboard/sound/sound_error.wav");
    private AudioInputStream soundErrorAudioStream;
    private Clip clip;

    public MyMainFrame() {
        
        try {
            soundErrorAudioStream = AudioSystem.getAudioInputStream(oSoundFile);
            clip = AudioSystem.getClip();
            clip.open(soundErrorAudioStream);
        } catch (UnsupportedAudioFileException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (LineUnavailableException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        this.setTitle(sApplicationTitle);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        this.setResizable(true);
        this.setSize(1000,500);
        this.setVisible(true);
        this.setLayout(new GridBagLayout());
        this.setLocationRelativeTo(null);
        this.addKeyListener(this);

        oKeyboardPanel = new MyLayeredKeyboardPanel();        

        GridBagConstraints oConstraint = new GridBagConstraints();

        oConstraint.anchor = GridBagConstraints.NORTHWEST;
        oConstraint.weightx = 1;
        oConstraint.weighty = 1;

        oConstraint.gridx = 0;
        oConstraint.gridy = 0;
        oConstraint.insets = new Insets(0,0,0,0);

        this.add(oKeyboardPanel, oConstraint);
        this.pack();

        oKeyboardPanel.repaint();
    }

    @Override
    public void keyTyped(KeyEvent e) {
        //Invoked when a key is typed. Uses KeyChar, char output
        
    }

    @Override
    public void keyPressed(KeyEvent e) {
        //Invoked when a physical key is pressed down. Uses KeyCode, int output
        switch (e.getKeyChar()) {
            //Row 1
            case ('`'): {
                oKeyboardPanel.setImage(1, 1);
            }break;
            case ('~'): {
                oKeyboardPanel.setImage(1, 1);
            }break;
            case ('1'): {
                oKeyboardPanel.setImage(1, 2);
            }break;
            case ('!'): {
                oKeyboardPanel.setImage(1, 2);
            }break;
            case ('2'): {
                oKeyboardPanel.setImage(1, 3);
            }break;
            case ('@'): {
                oKeyboardPanel.setImage(1, 3);
            }break;
            case ('3'): {

            }break;
            case ('#'): {
                System.out.println("#");
            }break;
            case ('4'): {
                System.out.println("4");
            }break;
            case ('$'): {
                System.out.println("$");
            }break;
            case ('5'): {
                System.out.println("5");
            }break;
            case ('%'): {
                System.out.println("%");
            }break;
            case ('6'): {
                System.out.println("6");
            }break;
            case ('^'): {
                System.out.println("^");
            }break;
            case ('7'): {
                System.out.println("7");
            }break;
            case ('&'): {
                System.out.println("&");
            }break;
            case ('8'): {
                System.out.println("8");
            }break;
            case ('*'): {
                System.out.println("*");
            }break;
            case ('9'): {
                System.out.println("9");
            }break;
            case ('('): {
                System.out.println("(");
            }break;
            case ('0'): {
                System.out.println("0");
            }break;
            case (')'): {
                System.out.println(")");
            }break;
            case ('-'): {
                System.out.println("-");
            }break;
            case ('_'): {
                System.out.println("_");
            }break;
            case ('='): {
                System.out.println("=");
            }break;
            case ('+'): {
                System.out.println("+");
            }break;

            //Row 2
            case ('q'): {
                oKeyboardPanel.setImage(1, 15);
            }break;
            case ('Q'): {
                oKeyboardPanel.setImage(1, 15);
            }break;
            case ('w'): {
                System.out.println("w");
            }break;
            case ('W'): {
                System.out.println("W");
            }break;
            case ('e'): {
                System.out.println("e");
            }break;
            case ('E'): {
                System.out.println("E");
            }break;
            case ('r'): {
                System.out.println("r");
            }break;
            case ('R'): {
                System.out.println("R");
            }break;
            case ('t'): {
                System.out.println("t");
            }break;
            case ('T'): {
                System.out.println("T");
            }break;
            case ('y'): {
                System.out.println("y");
            }break;
            case ('Y'): {
                System.out.println("Y");
            }break;
            case ('u'): {
                System.out.println("u");
            }break;
            case ('U'): {
                System.out.println("U");
            }break;
            case ('i'): {
                System.out.println("i");
            }break;
            case ('I'): {
                System.out.println("I");
            }break;
            case ('o'): {
                System.out.println("o");
            }break;
            case ('O'): {
                System.out.println("O");
            }break;
            case ('p'): {
                System.out.println("p");
            }break;
            case ('P'): {
                System.out.println("P");
            }break;
            case ('['): {
                System.out.println("[");
            }break;
            case ('{'): {
                System.out.println("{");
            }break;
            case (']'): {
                System.out.println("]");
            }break;
            case ('}'): {
                System.out.println("}");
            }break;

            //Row 3
            case ('a'): {
                System.out.println("a");
            }break;
            case ('A'): {
                System.out.println("A");
            }break;
            case ('s'): {
                System.out.println("s");
            }break;
            case ('S'): {
                System.out.println("S");
            }break;
            case ('d'): {
                System.out.println("d");
            }break;
            case ('D'): {
                System.out.println("D");
            }break;
            case ('f'): {
                System.out.println("f");
            }break;
            case ('F'): {
                System.out.println("F");
            }break;
            case ('g'): {
                System.out.println("g");
            }break;
            case ('G'): {
                System.out.println("G");
            }break;
            case ('h'): {
                System.out.println("h");
            }break;
            case ('H'): {
                System.out.println("H");
            }break;
            case ('j'): {
                System.out.println("j");
            }break;
            case ('J'): {
                System.out.println("J");
            }break;
            case ('k'): {
                System.out.println("k");
            }break;
            case ('K'): {
                System.out.println("K");
            }break;
            case ('l'): {
                System.out.println("l");
            }break;
            case ('L'): {
                System.out.println("L");
            }break;
            case (';'): {
                System.out.println(";");
            }break;
            case (':'): {
                System.out.println(":");
            }break;
            case ('\''): {
                System.out.println("'");
            }break;
            case ('"'): {
                System.out.println("\"");
            }break;
            case ('\\'): {
                System.out.println("\\");
            }break;
            case ('|'): {
                System.out.println("|");
            }break;

            //Row 4
            case ('z'): {
                System.out.println("z");
            }break;
            case ('Z'): {
                System.out.println("Z");
            }break;
            case ('x'): {
                System.out.println("x");
            }break;
            case ('X'): {
                System.out.println("X");
            }break;
            case ('c'): {
                System.out.println("c");
            }break;
            case ('C'): {
                System.out.println("C");
            }break;
            case ('v'): {
                System.out.println("v");
            }break;
            case ('V'): {
                System.out.println("V");
            }break;
            case ('b'): {
                System.out.println("b");
            }break;
            case ('B'): {
                System.out.println("B");
            }break;
            case ('n'): {
                System.out.println("n");
            }break;
            case ('N'): {
                System.out.println("N");
            }break;
            case ('m'): {
                System.out.println("m");
            }break;
            case ('M'): {
                System.out.println("M");
            }break;
            case (','): {
                System.out.println(",");
            }break;
            case ('<'): {
                System.out.println("<");
            }break;
            case ('.'): {
                System.out.println(".");
            }break;
            case ('>'): {
                System.out.println(">");
            }break;
            case ('/'): {
                System.out.println("/");
            }break;
            case ('?'): {
                System.out.println("?");
            }break;
            
        }

        switch (e.getKeyCode()) {
            //Backspace
            //<--
            case (8): {
                System.out.println("Backspace");
            }break;
            //Line feed
            //  |
            //<--
            case (10): {
                System.out.println("Line feed");
            }break;
            //Data Link Escape
            //Shift
            case (16): {
                oKeyboardPanel.setImage(1, 41);
            }break;
            //Ctrl (Strg) Left / Right
            case (17): {
                System.out.println("Strg");
            }break;
            //Alt / Alt Gr
            case (18): {
                System.out.println("Alt");
            }break;
            //Space
            case (32): {
                System.out.println("Space");
            }break;
            //Windows Key Left / Right
            case (524): {
                System.out.println("Windows Key");
            }break;
            //Options
            case (525): {
                System.out.println("Options");
            }break;
        
            default:
                break;
        }

        oKeyboardPanel.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        //Called whenever a button is released

        

        switch (e.getKeyChar()) {
            //Row 1
            case ('`'): {
                oKeyboardPanel.setImage(0, 1);
                //clip.setMicrosecondPosition(0);
                //clip.start();
            }break;
            case ('~'): {
                oKeyboardPanel.setImage(0, 1);
                //clip.setMicrosecondPosition(0);
                //clip.start();
            }break;
            case ('1'): {
                oKeyboardPanel.setImage(0, 2);
                //clip.setMicrosecondPosition(0);
                //clip.start();
            }break;
            case ('!'): {
                oKeyboardPanel.setImage(0, 2);
                //clip.setMicrosecondPosition(0);
                //clip.start();
            }break;
            case ('2'): {
                oKeyboardPanel.setImage(0, 3);
                //clip.setMicrosecondPosition(0);
                //clip.start();
            }break;
            case ('@'): {
                oKeyboardPanel.setImage(0, 3);
            }break;
            case ('3'): {
                oKeyboardPanel.setImage(0, 4);
            }break;
            case ('#'): {
                oKeyboardPanel.setImage(0, 4);
            }break;
            case ('4'): {
                oKeyboardPanel.setImage(0, 5);
            }break;
            case ('$'): {
                oKeyboardPanel.setImage(0, 5);
            }break;
            case ('5'): {
                oKeyboardPanel.setImage(0, 6);
            }break;
            case ('%'): {
                oKeyboardPanel.setImage(0, 6);
            }break;
            case ('6'): {
                oKeyboardPanel.setImage(0, 7);
            }break;
            case ('^'): {
                oKeyboardPanel.setImage(0, 7);
            }break;
            case ('7'): {
                oKeyboardPanel.setImage(0, 8);
            }break;
            case ('&'): {
                oKeyboardPanel.setImage(0, 8);
            }break;
            case ('8'): {
                oKeyboardPanel.setImage(0, 9);
            }break;
            case ('*'): {
                oKeyboardPanel.setImage(0, 9);
            }break;
            case ('9'): {
                oKeyboardPanel.setImage(0, 10);
            }break;
            case ('('): {
                oKeyboardPanel.setImage(0, 10);
            }break;
            case ('0'): {
                oKeyboardPanel.setImage(0, 11);
            }break;
            case (')'): {
                oKeyboardPanel.setImage(0, 11);
            }break;
            case ('-'): {
                oKeyboardPanel.setImage(0, 12);
            }break;
            case ('_'): {
                oKeyboardPanel.setImage(0, 12);
            }break;
            case ('='): {
                oKeyboardPanel.setImage(0, 13);
            }break;
            case ('+'): {
                oKeyboardPanel.setImage(0, 13);
            }break;

            //Row 2
            case ('q'): {
                oKeyboardPanel.setImage(0, 15);
            }break;
            case ('Q'): {
                oKeyboardPanel.setImage(0, 15);
            }break;
            case ('w'): {
                oKeyboardPanel.setImage(0, 16);
            }break;
            case ('W'): {
                oKeyboardPanel.setImage(0, 16);
            }break;
            case ('e'): {
                System.out.println("e");
            }break;
            case ('E'): {
                System.out.println("E");
            }break;
            case ('r'): {
                System.out.println("r");
            }break;
            case ('R'): {
                System.out.println("R");
            }break;
            case ('t'): {
                System.out.println("t");
            }break;
            case ('T'): {
                System.out.println("T");
            }break;
            case ('y'): {
                System.out.println("y");
            }break;
            case ('Y'): {
                System.out.println("Y");
            }break;
            case ('u'): {
                System.out.println("u");
            }break;
            case ('U'): {
                System.out.println("U");
            }break;
            case ('i'): {
                System.out.println("i");
            }break;
            case ('I'): {
                System.out.println("I");
            }break;
            case ('o'): {
                System.out.println("o");
            }break;
            case ('O'): {
                System.out.println("O");
            }break;
            case ('p'): {
                System.out.println("p");
            }break;
            case ('P'): {
                System.out.println("P");
            }break;
            case ('['): {
                System.out.println("[");
            }break;
            case ('{'): {
                System.out.println("{");
            }break;
            case (']'): {
                System.out.println("]");
            }break;
            case ('}'): {
                System.out.println("}");
            }break;

            //Row 3
            case ('a'): {
                System.out.println("a");
            }break;
            case ('A'): {
                System.out.println("A");
            }break;
            case ('s'): {
                System.out.println("s");
            }break;
            case ('S'): {
                System.out.println("S");
            }break;
            case ('d'): {
                System.out.println("d");
            }break;
            case ('D'): {
                System.out.println("D");
            }break;
            case ('f'): {
                System.out.println("f");
            }break;
            case ('F'): {
                System.out.println("F");
            }break;
            case ('g'): {
                System.out.println("g");
            }break;
            case ('G'): {
                System.out.println("G");
            }break;
            case ('h'): {
                System.out.println("h");
            }break;
            case ('H'): {
                System.out.println("H");
            }break;
            case ('j'): {
                System.out.println("j");
            }break;
            case ('J'): {
                System.out.println("J");
            }break;
            case ('k'): {
                System.out.println("k");
            }break;
            case ('K'): {
                System.out.println("K");
            }break;
            case ('l'): {
                System.out.println("l");
            }break;
            case ('L'): {
                System.out.println("L");
            }break;
            case (';'): {
                System.out.println(";");
            }break;
            case (':'): {
                System.out.println(":");
            }break;
            case ('\''): {
                System.out.println("'");
            }break;
            case ('"'): {
                System.out.println("\"");
            }break;
            case ('\\'): {
                System.out.println("\\");
            }break;
            case ('|'): {
                System.out.println("|");
            }break;

            //Row 4
            case ('z'): {
                System.out.println("z");
            }break;
            case ('Z'): {
                System.out.println("Z");
            }break;
            case ('x'): {
                System.out.println("x");
            }break;
            case ('X'): {
                System.out.println("X");
            }break;
            case ('c'): {
                System.out.println("c");
            }break;
            case ('C'): {
                System.out.println("C");
            }break;
            case ('v'): {
                System.out.println("v");
            }break;
            case ('V'): {
                System.out.println("V");
            }break;
            case ('b'): {
                System.out.println("b");
            }break;
            case ('B'): {
                System.out.println("B");
            }break;
            case ('n'): {
                System.out.println("n");
            }break;
            case ('N'): {
                System.out.println("N");
            }break;
            case ('m'): {
                System.out.println("m");
            }break;
            case ('M'): {
                System.out.println("M");
            }break;
            case (','): {
                System.out.println(",");
            }break;
            case ('<'): {
                System.out.println("<");
            }break;
            case ('.'): {
                System.out.println(".");
            }break;
            case ('>'): {
                System.out.println(">");
            }break;
            case ('/'): {
                System.out.println("/");
            }break;
            case ('?'): {
                System.out.println("?");
            }break;
        }

        switch (e.getKeyCode()) {
            //Backspace
            //<--
            case (8): {
                System.out.println("Backspace");
            }break;
            //Line feed
            //  |
            //<--
            case (10): {
                System.out.println("Line feed");
            }break;
            //Data Link Escape
            //Shift
            case (16): {
                oKeyboardPanel.setImage(0, 41);
            }break;
            //Ctrl (Strg) Left / Right
            case (17): {
                System.out.println("Strg");
            }break;
            //Alt / Alt Gr
            case (18): {
                System.out.println("Alt");
            }break;
            //Space
            case (32): {
                System.out.println("Space");
            }break;
            //Windows Key Left / Right
            case (524): {
                System.out.println("Windows Key");
            }break;
            //Options
            case (525): {
                System.out.println("Options");
            }break;
        }
        oKeyboardPanel.repaint();
    }
}
