/*
    bits:
    0 = snake
    1 = food
    2 = checked (for searching algorithms)
*/

// Bit functions for ease
function isBitSet(value: number, bit: number): boolean {
    return ((value >> bit) % 2 != 0)
}

function bitSet(value: number, bit: number): number {
    return value | 1 << bit;
}

function bitClear(value: number, bit: number): number {
    return value & ~(1 << bit);
}

class Point {
    x: number;
    y: number;

    constructor(x = 0, y = 0) {
        this.x = x;
        this.y = y;
    }

    add(other: Point) {
        var result = new Point;
        result.x = this.x + other.x;
        result.y = this.y + other.y;
        return result;
    }

    subtract(other: Point) {
        var result = new Point;
        result.x = this.x - other.x;
        result.y = this.y - other.y;
        return result;
    }
}

class SnakeCore {
    canvas: HTMLCanvasElement;
    context: CanvasRenderingContext2D;
    grid: number;
    speed: number;
    width: number;
    height: number;
    board: number[][];
    body: Point[];
    gameover: boolean;
    foodAte: boolean;

    constructor() {
        // TODO: Add CSS stuff for page
        this.canvas = document.getElementById('snake') as HTMLCanvasElement;
        this.context = this.canvas.getContext('2d') as CanvasRenderingContext2D;
        this.grid = 25; // size of grid squares
        this.speed = 10; // speed in ms
        this.width = 25;
        this.height = 15;
        this.board = [];
        for (let i = 0; i < this.height; i++) {
            this.board.push(new Array(this.width));
            for (let j = 0; j < this.width; j++) {
                this.board[i][j] = 0;
            }
        }
        this.body = [];
        this.gameover = false;
        this.foodAte = true;
        this.body.push(new Point(12, 8));
        this.board[8][12] = 1;
    }

    get head(): Point {
        return this.body[this.body.length - 1];
    }

    reset() {
        snake.gameover = false;
        snake.foodAte = true;
        for (let i = 0; i < this.height; i++) {
            for (let j = 0; j < this.width; j++) {
                this.board[i][j] = 0;
            }
        }
    }

    foodRegen() {
        if (!this.foodAte)
            return;
        this.foodAte = false;
        while (true) {
            let tmp = new Point(Math.floor(Math.random() * this.width), Math.floor(Math.random() * this.height));
            if (isBitSet(this.board[tmp.y][tmp.x], 0))
                continue;
            this.board[tmp.y][tmp.x] = bitSet(this.board[tmp.y][tmp.x], 1);
            return;
        }
    }
}

class Bot {
    pathUntrimmed: Point[];
    path: Point[];

    constructor() {
        this.pathUntrimmed = [];
        this.path = [];
    }

    bfs() {
        var search: Point[] = [snake.head];
        while (search.length !== 0) {
            let current: Point = search.shift() as Point;
            if (isBitSet(snake.board[current.y][current.x], 2))
                continue;
            this.pathUntrimmed.push(current);
            let locals: Point[] = new Array[4];
            for (var i = 0; i < 4; i++)
                locals[i] = new Point(current.x, current.y);
            locals[0].y += 1;
            locals[1].x += 1;
            locals[2].y -= 1;
            locals[3].x -= 1;
            for (const local of locals) {
                if (local.x < 0 || local.x > snake.width - 1 || local.y < 0 || local.y > snake.height - 1) {
                    continue;
                }
                let value = snake.board[local.y][local.x];
                if (isBitSet(value, 1)) {
                    this.pathUntrimmed.push(local);
                    return;
                }
                if (isBitSet(value, 2))
                    continue;
                if (isBitSet(value, 0))
                    continue;
                search.push(local);
            }
            snake.board[current.y][current.x] = bitSet(snake.board[current.y][current.x], 2);
        }
    }
    
    // TODO: Fix trim function
    trim() {
        let reachedSnake = false;
        this.path.push(this.pathUntrimmed.pop() as Point); // Push food location
        while (this.pathUntrimmed.length !== 0) {
            if (!reachedSnake) {
                let location = this.pathUntrimmed[this.pathUntrimmed.length - 1];
                if (isBitSet(snake.board[location.y][location.x], 0))
                    reachedSnake = true;
                var delta = new Point;
                let connection = this.path[this.path.length - 1];
                delta.x = location.x - connection.x;
                delta.y = location.y - connection.y;
                if ((Math.abs(delta.x) + Math.abs(delta.y)) === 1)
                    this.path.push(location);
            }
            this.pathUntrimmed.pop();
        }
    }

    unvisit() {
        for (let i = 0; i < snake.height; i++) {
            for (let j = 0; j < snake.width; j++) {
                snake.board[i][j] = bitClear(snake.board[i][j], 2);
            }
        }
    }

    /*
        values:
        0 = left
        1 = up
        2 = right
        4 = down
    */
    nextMove() {
        var next: Point = this.path.pop() as Point;
        var head: Point = snake.head;
        var delta = new Point(0, 0);
        delta = next.subtract(head);
        if (delta.x > 1) {
            delta.x = 1;
            console.log("[ERR] delta.x > 1, clamping");
        } else if (delta.x < -1) {
            delta.x = -1;
            console.log("[ERR] delta.x < 1, clamping");
        }
        if (delta.y > 1) {
            delta.y = 1;
            console.log("[ERR] delta.y > 1, clamping");
        } else if (delta.y < -1) {
            delta.y = -1;
            console.log("[ERR] delta.y < 1, clamping");
        }
        return delta;
    }

    autoplay() {
        if (this.path.length === 0)
            this.bfs();
            this.trim();
            this.unvisit();
        return this.nextMove();
        /*
        let delta = this.headPos - this.path.pop();
        if (delta.x > 0)
            return 0;
        if (delta.y > 0)
            return 1;
        if (delta.x < 0)
            return 2;
        if (delta.y < 0)
            return 4;
        return 0; // Safety guard if above doesn't return
        */
    }
}

const snake: SnakeCore = new SnakeCore(); // Singleton for snake game
const bot: Bot = new Bot(); // Singleton for bot playing

function snakeloop() {
    snake.context.clearRect(0, 0, snake.canvas.width, snake.canvas.height);
    // Reset of needed
    if (snake.gameover)
        snake.reset();
    // Input
    bot.autoplay();
    
    // Move snake
    // TODO: Fix teleporting snake
    let next = bot.nextMove().add(snake.head);
    if (next.x < 0 || next.x > snake.width || next.y < 0 || next.y > snake.height)
        snake.gameover = true;
    if (isBitSet(snake.board[next.y][next.x], 0) && (snake.body.length > 1))
        snake.gameover = true; // Game should end (Snake touching snake)

    snake.board[next.y][next.x] = bitSet(snake.board[next.y][next.x], 0);
    snake.body.push(next);
    if (!isBitSet(snake.board[next.y][next.x], 1)) {
        let old: Point = snake.body.shift() as Point;
        snake.board[old.y][old.x] = bitClear(snake.board[old.y][old.x], 0);
    } else {
        snake.board[next.y][next.x] = bitClear(snake.board[next.y][next.x], 1);
        snake.foodAte = true;
        while (bot.path.length > 0)
            bot.path.pop();
    }

    // Regenerate food if needed
    snake.foodRegen();

    // Draw game
    for (let i = 0; i < snake.height; i++) {
        for (let j = 0; j < snake.width; j++) {
            if (isBitSet(snake.board[i][j], 0))
                snake.context.fillStyle = "green";
            else if (isBitSet(snake.board[i][j], 1))
                snake.context.fillStyle = "red";
            else
                snake.context.fillStyle = "black";
            snake.context.fillRect(j * snake.grid, i * snake.grid, snake.grid, snake.grid);
        }
    }
}

// start the game
setInterval(snakeloop, snake.speed);