package ac.essex.ooechs.music.util;

/**
 * <p/>
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version,
 * provided that any use properly credits the author.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details at http://www.gnu.org
 * </p>
 *
 * @author Olly Oechsle, University of Essex, Date: 06-Jul-2007
 * @version 1.0
 */
public class Key {

    public static String names[] = new String[]{"e1", "f1", "g1", "a2", "b2", "c2", "d2", "e2", "f2", "g2", "a3", "b3", "c3", "d3", "e3", "f3", "g3", "a4", "b4", "c4", "d4", "e4", "f4", "g4", "a5", "b5", "c5"};
    private static int pitches[];   

    public static void reset() {
        pitches = new int[]{40, 41, 43, 45, 47, 48, 50, 52, 53,55,57,59,60,62,64,65,67,69,71,72,74,76,77,79,81,83,84};
    }

    public static void setFlat(String key) {
        for (int i = 0; i < names.length; i++) {
            String name = names[i];
            if (name.startsWith(key)) {
                pitches[i]--;
            }
        }
    }

    public static void setSharp(String key) {
        for (int i = 0; i < names.length; i++) {
            String name = names[i];
            if (name.startsWith(key)) {
                pitches[i]++;
            }
        }
    }

    public static int getPitch(int line, Staff c) {

        String name = getNoteName(line, c);

        System.out.println(name);

        for (int i = 0; i < names.length; i++) {
            String s = names[i];
            if (s.equals(name)) return pitches[i];
        }

        return 0;

    }

    private static String getNoteName(int line, Staff c) {
        if (c.type == Staff.TREBLE) {
            switch (line) {
                case 1: return "c3";
                case 2: return "d3";
                case 3: return "e3";
                case 4: return "f3";
                case 5: return "g3";
                case 6: return "a4";
                case 7: return "b4";
                case 8: return "c4";
                case 9: return "d4";
                case 10: return "e4";
                case 11: return "f4";
                case 12: return "g4";
                case 13: return "a5";
                case 14: return "b5";
            }
        } else {
            switch (line) {
                case 1: return "e1";
                case 2: return "f1";
                case 3: return "g1";
                case 4: return "a2";
                case 5: return "b2";
                case 6: return "c2";
                case 7: return "d2";
                case 8: return "e2";
                case 9: return "f2";
                case 10: return "g2";
                case 11: return "a3";
                case 12: return "b3";
                case 13: return "c3";
                case 14: return "d3";
            }
        }
        return "";
    }

}
