#!/usr/bin/env node
var bilby = require('bilby')

var TRANSFORM = {
    ONE: 1, // 90 degree
    TWO: 2, // 180 degree
    THREE: 3, // 270 degree
}

var TILE_TYPE = {
    // - this is the edge
    '0':    '     \n' +
            '     \n' +
            '     \n' +
            '     \n' +
            '     ',
    '1':    '  +  \n' +
            '  |  \n' +
            '  +-+\n' +
            '     \n' +
            '     ',
    '2':    '     \n' +
            '     \n' +
            '  +-+\n' +
            '  |  \n' +
            '  +  ',
    '3':    '     \n' +
            '     \n' +
            '+-+  \n' +
            '  |  \n' +
            '  +  ',
    '4':    '  +  \n' +
            '  |  \n' +
            '+-+  \n' +
            '     \n' +
            '     ',
    '5':    '  +  \n' +
            '  |  \n' +
            '+-+-+\n' +
            '     \n' +
            '     ',
    '6':    '  +  \n' +
            '  |  \n' +
            '  +-+\n' +
            '  |  \n' +
            '  +  ',
    '7':    '     \n' +
            '     \n' +
            '+-+-+\n' +
            '  |  \n' +
            '  +  ',
    '8':    '  +  \n' +
            '  |  \n' +
            '+-+  \n' +
            '  |  \n' +
            '  +  ',
    '9':    '  +  \n' +
            ' +-+ \n' +
            ' | | \n' +
            ' +-+ \n' +
            '     ',
    '10':   '     \n' +
            ' +-+ \n' +
            ' | |+\n' +
            ' +-+ \n' +
            '     ',
    '11':   '     \n' +
            ' +-+ \n' +
            ' | | \n' +
            ' +-+ \n' +
            '  +  ',
    '12':   '     \n' +
            ' +-+ \n' +
            '+| | \n' +
            ' +-+ \n' +
            '     ',
    '13':   '  +  \n' +
            '  |  \n' +
            '  |  \n' +
            '  |  \n' +
            '  +  ',
    '14':   '     \n' +
            '     \n' +
            '+---+\n' +
            '     \n' +
            '     ',
    '15':   '  +  \n' +
            '  |  \n' +
            '+-+-+\n' +
            '  |  \n' +
            '  +  ',
    '16':   '     \n' +
            '     \n' +
            '     \n' +
            '     \n' +
            '     ',
}

var TILE_NUM = 16;

var TILE_TRANSFORM = {
    '1':  [2,3,4],
    '2':  [3,4,1],
    '3':  [4,1,2],
    '4':  [1,2,3],
    '5':  [6,7,8],
    '6':  [7,8,5],
    '7':  [8,5,6],
    '8':  [5,6,7],
    '9':  [10,11,12],
    '10':  [11,12,9],
    '11':  [12,9,10],
    '12':  [9,10,11],
    '13':  [14],
    '14':  [13],
}

var DIRECTION = {
    UP: 0,
    RIGHT: 1,
    DOWN: 2,
    LEFT: 3,
}

// whether or not tile is compatible with UP, RIGHT, DOWN, LEFT tiles
var TILE_COMPATIBILITY = {
    '1': [
        [0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0],
        [0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    ],
    '2': [
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0],
        [0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    ],
    '3': [
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0],
        [0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0],
    ],
    '4': [
        [0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0],
    ],
    '5': [
        [0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0],
        [0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0],
    ],
    '6': [
        [0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0],
        [0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0],
        [0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    ],
    '7': [
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0],
        [0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0],
        [0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0],
    ],
    '8': [
        [0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0],
        [0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0],
    ],
    '9': [
        [0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    ],
    '10': [
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    ],
    '11': [
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    ],
    '12': [
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0],
    ],
    '13': [
        [0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    ],
    '14': [
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0],
    ],
    '15': [
        [0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0],
        [0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0],
        [0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0],
        [0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0],
    ],
    '16': [
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    ]
}



// for (var key in TILE_TYPE) {
//     console.log(TILE_TYPE[key])
// }

var SAMPLE_MAP = [
    [10 , 12 , 16 , 16 , 16] ,
    [16 , 16 , 16 , 16 , 16] ,
    [16 , 16 , 16 , 16 , 16] ,
    [16 , 16 , 14 , 16 , 16] ,
    [16 , 16 , 16 , 16 , 16] ,
];

var printMap = function(map) {
    var size = 5;
    for (var i = 0, l = map.length; i < l; i++) {
        var v = map[i];
        for (var k = 0; k < 5; k++) {
            for (var j = 0, l = v.length; j < l; j++) {
                var w = v[j];
                var lines = TILE_TYPE[w].split('\n');
                process.stdout.write(lines[k]);
            }
            process.stdout.write('\n');
        }
    }
}


/**
 * Check if a map is closed
 *   map - A 2d array, each cell can contain 1 of the 16 values.
 *         One exception is the the boundary cell which contains all zero
 */
var isMapClosed = function(map) {
    for (var i = 0; i < map.length; i++) {
        var v = map[i];
        for (var j = 0; j < v.length; j++) {
            if (!isClosedTile(map, i, j)) return false;
        }
    }
    return true;
}

var isClosedTile = function(map, i, j) {
    return isTileConnected(map[i][j], (i > 0)? map[i-1][j] : 0, DIRECTION.UP)
        && isTileConnected(map[i][j], map[i][j+1] || 0, DIRECTION.RIGHT)
        && isTileConnected(map[i][j], (i < map.length - 1) ? map[i+1][j] : 0, DIRECTION.DOWN)
        && isTileConnected(map[i][j], map[i][j-1] || 0, DIRECTION.LEFT);
}

var isValid = function(map) {
    if (map.length < 1) return false;

    var width = 0
    for (var i = 0, l = map.length; i < l; i++) {
        var v = map[i];
        if (v.length < 1) return false;
        if (width == 0) width = v.length;
        if (v.length != width) return false;

        for (var j = 1; j < v.length - 1; j++) {
            if (0 >= v[j] || v[j] > 16) return false;
        }
    }
    return true;
}

var isTileConnected = function(tile1, tile2, direction) {
    return TILE_COMPATIBILITY[tile1][direction][tile2];
}

var isTileConnectedManual = function(tile1, tile2, direction) {
    var tileData1 = TILE_TYPE[tile1]
    var tileData2 = TILE_TYPE[tile2]

    if (direction == DIRECTION.UP) {
        if (tileData1[0*6 + 2] == ' ') return 1;
        if (tileData2[4*6 + 2] == '+') return 1;
    } else if (direction == DIRECTION.RIGHT) {
        if (tileData1[2*6 + 4] == ' ') return 1;
        if (tileData2[2*6 + 0] == '+') return 1;
    } else if (direction == DIRECTION.DOWN) {
        if (tileData1[4*6 + 2] == ' ') return 1;
        if (tileData2[0*6 + 2] == '+') return 1;
    } else {
        if (tileData1[2*6 + 0] == ' ') return 1;
        if (tileData2[2*6 + 4] == '+') return 1;
    }
    return 0
}


// for (var i = 0; i < TILE_NUM; i++) {
//     for (var direction in DIRECTION) {
//         var a = [];
//         for (var j = 0; j <= TILE_NUM; j++) {
//             a.push(isTileConnected(i+1,j, DIRECTION[direction]));
//         }
//         console.log(a)
//     }
//     console.log()
// }


console.log(isValid(SAMPLE_MAP));
console.log(isMapClosed(SAMPLE_MAP));
printMap(SAMPLE_MAP)

var generateMap = function(w, h) {
    
}
