/* TODO: make sense */

function make_move(slice, pow) {
    function  f(s,d,t) { return { side: s, depth: d, times: t } }
    return slice === "U"  ? [f("U", 0,     pow)] :
           slice === "D"  ? [f("U", 2, 4 - pow)] :
           slice === "F"  ? [f("F", 0,     pow)] :
           slice === "B"  ? [f("F", 2, 4 - pow)] :
           slice === "R"  ? [f("R", 0,     pow)] :
           slice === "L"  ? [f("R", 2, 4 - pow)] :
           slice === "Uw" ? [f("U", 0,     pow), f("U", 1, pow)] :
           slice === "Dw" ? [f("D", 0,     pow), f("D", 1, pow)] :
           slice === "Fw" ? [f("F", 0,     pow), f("F", 1, pow)] :
           slice === "Bw" ? [f("B", 0,     pow), f("B", 1, pow)] :
           slice === "Rw" ? [f("R", 0,     pow), f("R", 1, pow)] :
           slice === "Lw" ? [f("L", 0,     pow), f("L", 1, pow)] :
           slice === "E"  ? [f("U", 1, 4 - pow)] :
           slice === "S"  ? [f("F", 1,     pow)] :
           slice === "M"  ? [f("R", 1, 4 - pow)] :
           slice === "y"  ? [f("U", 0, pow), f("U", 1, pow), f("U", 2, pow)] :
           slice === "z"  ? [f("F", 0, pow), f("F", 1, pow), f("F", 2, pow)] :
           slice === "x"  ? [f("R", 0, pow), f("R", 1, pow), f("R", 2, pow)] :
           null }

var move_strings = { U: [["", "U" , "U2", "U'"],
                         ["", "E'", "E2", "E" ],
                         ["", "D" , "D2", "D'"]],
                     F: [["", "F" , "F2", "F'"],
                         ["", "S" , "S2", "S'"],
                         ["", "B'", "B2", "B" ]],
                     R: [["", "R" , "R2", "R'"],
                         ["", "M'", "M2", "M" ],
                         ["", "L'", "L2", "L" ]] };

function move_to_string(move) {
    return move.length === 1 ?
             move_strings[move[0].side][move[0].depth][move[0].times] :
           null }

function moves_to_string(moves) {
    var strings = new Array(moves.length);
    for (var i = 0; i < moves.length; i++) {
        strings[i] = move_to_string(moves[i]) }
    return strings.join(" ") }

function parse_moves(string) {
    var moves = [];
    while (string.length > 0) {
        if (string.match(/^[UDFBRLESMyzx]/)) {
            var slice = string.charAt(0);
            string = string.slice(1);
            if (string.match(/^w/)) {
                slice += "w";
                string = string.slice(1) }
            var pow = 1;
            if (string.match(/^['2]/)) {
                pow = string.charAt(0) === "'" ? 3 : 2;
                string = string.slice(1) }
            moves[moves.length] = make_move(slice, pow) }
        else {
            string = string.slice(1) } }
    return moves }

function lame_scramble(len) {
    function pick(xs) { return xs[Math.floor(Math.random() * xs.length)] }
    var out = new Array(len);
    for (var i = 0; i < out.length; i++) {
        out[i] = [{ side: pick(["U","R","F"])
                  , depth: pick([0,1,2])
                  , times: pick([1,2,3]) }] }
    return out }

function gen_scramble(len) {
	function pick(xs) { return xs[Math.floor(Math.random() * xs.length)] }

	var out = [];

	var add_move = function (slice,depth) { out.push([{ side:slice, depth:depth, times:pick([1,2,3]) }]); };

	prev_slice0 = null;
	prev_depth0 = null;
	prev_slice1 = null;
	prev_depth1 = null;

	for (var i = 0; i < len; ++i) {
		var next_slice = null;
		var next_depth = null;
		do {
			next_slice = pick(['U','F','R']);
			next_depth = pick([0,2]);
		} while (next_slice === prev_slice1 && next_depth === prev_depth1 || prev_slice0 === prev_slice1 && prev_slice1 === next_slice);

		add_move(next_slice, next_depth);

		prev_slice0 = prev_slice1;
		prev_depth0 = prev_depth1;
		prev_slice1 = next_slice;
		prev_depth1 = next_depth;
	}

	return out;
}


