/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unit.test;

import com.nsm.GuessMappingUI;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import java.util.concurrent.TimeUnit;
import javax.swing.KeyStroke;
import org.fest.swing.annotation.GUITest;
import org.fest.swing.core.Robot;
import org.fest.swing.edt.FailOnThreadViolationRepaintManager;
import org.fest.swing.finder.WindowFinder;
import org.fest.swing.fixture.FrameFixture;
import org.fest.swing.timing.Pause;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.fest.swing.launcher.ApplicationLauncher.*;
import static org.fest.swing.core.BasicRobot.robotWithCurrentAwtHierarchy;

/**
 *
 * @author Olivier
 */
public class GuessMapping {

    private static Robot robot;
    private static FrameFixture frameFixture;
    private ArrayList<MappedChar> mapped;

    @BeforeClass
    public static void setUpOnce() {
        FailOnThreadViolationRepaintManager.install();
        application(GuessMappingUI.class).start();
        robot = robotWithCurrentAwtHierarchy();
        frameFixture = WindowFinder.findFrame("GUESS_MAPPING").withTimeout(20, TimeUnit.SECONDS).using(robot);
    }

    @Before
    public void setUp() {
    }

    @Test
    @GUITest
    public void guessMapping() {        
        mapped = new ArrayList<MappedChar>();
        boolean found = false;
        KeySet ks = new KeySet();
        frameFixture.textBox("GUESS_MAPPING_FIELD").focus();
        frameFixture.textBox("GUESS_MAPPING_FIELD").setText("Starting");        
        for (char k : ks.keys_fr) {
            System.out.println("Looking for " + k);
            frameFixture.textBox("GUESS_MAPPING_FIELD").setText("");
            found = false;
            KeyCombi kc = new KeyCombi();
            for (int ksk : kc.secondKey) {
                for (int kk : kc.keys) {
                    if (ksk != -1) {
                        frameFixture.textBox("GUESS_MAPPING_FIELD").pressKey(ksk);
                    }
                    frameFixture.textBox("GUESS_MAPPING_FIELD").pressKey(kk);

                    String res = frameFixture.textBox("GUESS_MAPPING_FIELD").text();
                    if (res != null && res.length() > 0 && res.charAt(0) == k) {
                        System.out.println("Got it " + k + " - " + res.charAt(0) + " - " + kk + " - " + Integer.toHexString(kk) + " - " + ksk + " - " + Integer.toHexString(ksk));
                        mapped.add(new MappedChar(k, kk, ksk));
                        found = true;
                        break;
                    }
                    frameFixture.textBox("GUESS_MAPPING_FIELD").setText("");
                    Pause.pause(50);
                }
                frameFixture.textBox("GUESS_MAPPING_FIELD").releaseKey(ksk);
                if (found) {
                    break;
                }
                frameFixture.textBox("GUESS_MAPPING_FIELD").setText("");
            }
            if (!found) {
                System.out.println("-------- Missing " + k);
            }
        }
        System.out.println("------------ Show Results ---------------");
        for(MappedChar map : mapped){
            System.out.println(map.getChar() + " - " + reverseMapping(map.getKey1()) + " - " + reverseMapping(map.getKey2()) );
        }
    }

    @After
    public void tearDown() {
    }

    @AfterClass
    public static void tearDownClass() {
        robot.cleanUp();
    }

    private String reverseMapping(int i){
        switch(i){
            case -1:
                return "";
            case 0x10:
                return "VK_SHIFT";
            case 0x12:
                return "VK_ALT";
            case 0X2C:
                return "VK_COMMA";            
            case 0x30:
                return "VK_0";
            case 0x31:
                return "VK_1";
            case 0x32:
                return "VK_2";
            case 0x33:
                return "VK_3";
            case 0x34:
                return "VK_4";
            case 0x35:
                return "VK_5";
            case 0x36:
                return "VK_6";
            case 0x37:
                return "VK_7";
            case 0x38:
                return "VK_8";
            case 0x39:
                return "VK_9";
            case 0X3B:
                return "VK_SEMICOLON";
            case 0x41:
                return "VK_A";
            case 0x42:
                return "VK_B";
            case 0x43:
                return "VK_C";
            case 0x44:
                return "VK_D";
            case 0x45:
                return "VK_E";
            case 0x46:
                return "VK_F";
            case 0x47:
                return "VK_G";
            case 0x48:
                return "VK_H";
            case 0x49:
                return "VK_I";
            case 0x4A:
                return "VK_J";
            case 0x4B:
                return "VK_K";
            case 0x4C:
                return "VK_L";
            case 0x4D:
                return "VK_M";
            case 0x4E:
                return "VK_N";
            case 0x4F:
                return "VK_O";
            case 0x50:
                return "VK_P";
            case 0x51:
                return "VK_Q";
            case 0x52:
                return "VK_R";
            case 0x53:
                return "VK_S";
            case 0x54:
                return "VK_T";
            case 0x55:
                return "VK_U";
            case 0x56:
                return "VK_V";
            case 0x57:
                return "VK_W";
            case 0x58:
                return "VK_X";
            case 0x59:
                return "VK_Y";
            case 0x5A:
                return "VK_Z";
        }

        return "UNKNOWN";
    }

    class KeySet {

        public char[] keys_fr2 = {'a', 'z', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'q', 's', 'd', 'f', 'g', 'h', 'j', 'k',
            'l', 'm', 'w', 'x', 'c', 'v', 'b', 'n', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'A', 'B', 'C', 'D', 'E', 'F','G', 'H', 'I', 'J', 'K', 'L'};
        public char[] keys_fr = {'M', 'Q', 'Z','A','N','C','W'};//,'z', 'w', 'x', 'm', '1','ç'};
    }

    class KeyCombi {

        public int[] keys = {KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_C, KeyEvent.VK_D, KeyEvent.VK_E,
            KeyEvent.VK_F, KeyEvent.VK_G, KeyEvent.VK_H, KeyEvent.VK_I, KeyEvent.VK_J,
            KeyEvent.VK_K, KeyEvent.VK_L, KeyEvent.VK_M, KeyEvent.VK_N, KeyEvent.VK_O,
            KeyEvent.VK_P, KeyEvent.VK_Q, KeyEvent.VK_R, KeyEvent.VK_S, KeyEvent.VK_T,
            KeyEvent.VK_U, KeyEvent.VK_V, KeyEvent.VK_W, KeyEvent.VK_X, KeyEvent.VK_Y,
            KeyEvent.VK_Z, KeyEvent.VK_0, KeyEvent.VK_1, KeyEvent.VK_2, KeyEvent.VK_3,
            KeyEvent.VK_4, KeyEvent.VK_5, KeyEvent.VK_6, KeyEvent.VK_7, KeyEvent.VK_8,
            KeyEvent.VK_9, KeyEvent.VK_COMMA, KeyEvent.VK_PERIOD, KeyEvent.VK_QUOTE, KeyEvent.VK_SEMICOLON,
            };
        public int[] secondKey = {-1, KeyEvent.VK_SHIFT, KeyEvent.VK_ALT, KeyEvent.VK_ALT_GRAPH};
    }
    class MappedChar{
        char c;
        int key1;
        int key2;

        public MappedChar(char cc, int kk1,int kk2){
            c = cc;
            key1 = kk1;
            key2 = kk2;
        }

        public char getChar(){
            return c;
        }

        public int getKey1(){
            return key1;
        }

        public int getKey2(){
            return key2;
        }
    }
}

