#!/usr/bin/env python

from generator.actions import Actions
from random import randint
import random
import math
import numpy
import struct

VGF_MAGIC = 0x78330909
VGF_VERSION = 0x0001

VGF_OBJECT_RECT = 0
VGF_OBJECT_TRIANGLE = 1
VGF_OBJECT_LINE = 2
VGF_OBJECT_CIRCLE = 3
VGF_OBJECT_SQUARE = 4
VGF_OBJECT_SPRAY = 5
VGF_OBJECT_END = 100

VGF_OBJECT_SETTINGS_NOFILL = 0
VGF_OBJECT_SETTINGS_FILL = 1

VGF_MAX_X = 512
VGF_MAX_Y = 512

VGF_MAX_LAYER = 6

VGF_MAX_COLOR = 200

VGF_DEFAULT_COLOR_INDEX = 0xFFFF

# Do integer division and round (truncate) like C does
def c_divide_int( v1, v2 ):
    result = v1 / v2

    if ( result < 0 ):
        result2 = abs(v1) / abs(v2)
        return (-result2)
    else:
        return result

def rand_or_equal( start, end ):
    start = abs(start)
    end = abs(end)

    if ( start == end ):
        return start
    elif ( start > end ):
        return random.randint( end, start )
    else:
        return random.randint( start, end )

def get_xy( x_pos, y_pos, width ):
    return (x_pos + (y_pos * width))

class VGFLayerRender:
    def __init__( self, width, height, layer_count ):
        self.render_layers = [ [VGF_DEFAULT_COLOR_INDEX for i in range( width * height )] for j in range( layer_count )]

        self.width = width
        self.height = height
        self.layer_count = layer_count

        self.current_layer = 0

    def set_current_layer( self, current_layer ):
        self.current_layer = current_layer

    def draw_pixel( self, x, y, color_index ):
        self.render_layers[self.current_layer][ get_xy( x, y, self.width ) ] = color_index

    def get_width( self ):
        return self.width

    def get_height( self ):
        return self.height

    def render_to_single_layer( self ):
        single_layer = [VGF_DEFAULT_COLOR_INDEX for i in range( self.width * self.height )]

        for cur_layer in self.render_layers:
            for i in range(self.width * self.height):
                if ( cur_layer[i] is not VGF_DEFAULT_COLOR_INDEX ):
                    single_layer[i] = cur_layer[i]

        return single_layer

def vline_helper( x0, y0, line_len, render_obj, color_index ):
    if ( x0 >= render_obj.get_width() ):
        return

    if ( y0 >= render_obj.get_height() ):
        return

    cur = 0
    while ( cur < line_len ):
        if ( y0 + cur >= render_obj.get_height() ):
            break

        render_obj.draw_pixel( x0, (y0+cur), color_index )
        #layer_data[ get_xy( x0, (y0+cur), width ) ] = color_index
        cur += 1

def hline_helper( x0, y0, line_len, render_obj, color_index ):
    if ( x0 >= render_obj.get_width() ):
        return

    if ( y0 >= render_obj.get_height() ):
        return

    cur = 0
    while ( cur < line_len ):
        if ( x0 + cur >= render_obj.get_width() ):
            break

        render_obj.draw_pixel( (x0+cur), y0, color_index )
        cur += 1

def line_helper( x0, y0, x1, y1, render_obj, color_index ):
    dx = abs( x1 - x0 )
    dy = abs( y1 - y0 )

    if ( dx > dy ):
        dist = dx
    else:
        dist = dy

    if ( x0 < x1 ):
        sx = 1
    else:
        sx = -1

    if ( y0 < y1 ):
        sy = 1
    else:
        sy = -1

    x_pos = x0
    y_pos = y0

    errx = 0
    erry = 0

    count = 0
    while ( count <= dist+1 ):
        render_obj.draw_pixel( x_pos, y_pos, color_index )

        errx += dx
        erry += dy

        if ( errx > dist ):
            errx -= dist
            x_pos += sx

        if ( erry > dist ):
            erry -= dist
            y_pos += sy

        count += 1

class VGFObjLine():
    x_start = 0
    y_start = 0
    x_end = 0
    y_end = 0

    def __init__(self):
        self.x_start = 0
        self.y_start = 0
        self.x_end = 0
        self.y_end = 0

    def generate_random(self, max_x, max_y, bad_data = True):

        if ( random.randint( 0, 50 ) == 0 and bad_data == True ):
            self.x_start = random.randint( 0, 65000 )
        else:
            self.x_start = rand_or_equal( 0, max_x - 1 )

        if ( random.randint( 0, 50 ) == 0 and bad_data == True ):
            self.y_start = random.randint( 0, 65000 )
        else:
            self.y_start = rand_or_equal( 0, max_y - 1 )

        if ( random.randint( 0, 50 ) == 0 and bad_data == True ):
            self.x_end = random.randint( 0, 65000 )
        else:
            self.x_end = rand_or_equal( 0, max_x - 1 )

        if ( random.randint( 0, 50 ) == 0 and bad_data == True ):
            self.y_end = random.randint( 0, 65000 )
        else:
            self.y_end = rand_or_equal( 0, max_y - 1 )

    def generate_file_data(self):
        # Output data structure
        file_data = struct.pack( 'H', self.x_start )
        file_data += struct.pack( 'H', self.y_start )
        file_data += struct.pack( 'H', self.x_end )
        file_data += struct.pack( 'H', self.y_end )

        return file_data

    def render(self, render_obj, color_index, do_fill ):
        if ( self.x_start >= render_obj.get_width() or
             self.y_start >= render_obj.get_height() or
             self.x_end >= render_obj.get_width() or
             self.y_end >= render_obj.get_height() ):
            return

        if ( self.x_start > self.x_end ):
            return

        if ( self.y_start > self.y_end ):