#!/usr/bin/python
from gui import *
from random import randint

BACKGROUND_COLOUR = (55, 55, 55)
GREEN = (0, 255,0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
LIGHT_BLUE = (0, 100, 255)
#create a screen to draw on
#(Horizontal Resolution, Vertical Resolution, Red Fill Value, Green Fill Value, Blue Fill Value)
screen = screen_init(800, 480, (BACKGROUND_COLOUR))
DIAL_WIDTH = 200
DIAL_HEIGHT = 240



#Load in all the images that will be utilised for the warning panel
i_warning_on        = pygame.image.load('resources/warning_on.png')
i_warning_off       = pygame.image.load('resources/warning_off.png')
i_extinguish_on     = pygame.image.load('resources/extinguisher_on.png')
i_extinguish_off    = pygame.image.load('resources/extinguisher_off.png')
i_fuelpump_on       = pygame.image.load('resources/fuel_pumps_on.png')
i_fuelpump_off      = pygame.image.load('resources/fuel_pumps_off.png')
i_flameout_on       = pygame.image.load('resources/flameout_on.png')
i_flameout_off      = pygame.image.load('resources/flameout_off.png')
i_vibration_on      = pygame.image.load('resources/vibration_on.png')
i_vibration_off    = pygame.image.load('resources/vibration_off.png')
i_surge_on          = pygame.image.load('resources/surge_on.png')
i_surge_off         = pygame.image.load('resources/surge_off.png')
i_ice_on            = pygame.image.load('resources/ice_on.png')
i_ice_off           = pygame.image.load('resources/ice_off.png')

# Initialise Dials.
#(X Co-od, Y Co-od, Width, Heigth, image resource)
throttle = Specific_Dial(0,0,DIAL_WIDTH, DIAL_HEIGHT, 'resources/percentage.png')
pressure = Specific_Dial(0,240,DIAL_WIDTH, DIAL_HEIGHT,'resources/pressure.png')
speed = Specific_Dial(200,240,DIAL_WIDTH, DIAL_HEIGHT, 'resources/percentage.png')
temp = Specific_Dial(400,240,DIAL_WIDTH, DIAL_HEIGHT, 'resources/temperature.png')
ambPressure = Specific_Dial(600,240,DIAL_WIDTH, DIAL_HEIGHT, 'resources/pressure.png')
clock = pygame.time.Clock()

#Font for text
myFont = pygame.font.SysFont("monospace", 36)
myFont.set_bold(True)

#Set locations for each of the warning panel images. All off to begin with.
screen.blit(i_warning_off,(205, 5))
screen.blit(i_extinguish_off,(400, 5))
screen.blit(i_flameout_off, (205, 164))
screen.blit(i_fuelpump_off,(600, 5))
screen.blit(i_vibration_off,(205, 85))
screen.blit(i_surge_off,(400, 85))
screen.blit(i_ice_off,(600, 85))

#Label to display Channel A and Channel B
chanA = myFont.render("Channel A", 5, GREEN, BACKGROUND_COLOUR)
chanB = myFont.render("Channel B", 1, LIGHT_BLUE, BACKGROUND_COLOUR)

chan_Rect = chanA.get_rect()
chan_Rect.centerx = 520
chan_Rect.centery = 195

test = True
label = None
counter = 0

# Main program loop.
while 1:
    #Check if close button has been pressed
    for event in pygame.event.get():
        if event.type == QUIT:
            print "Exiting...."
            sys.exit()   # end program.
    #Second test for quitting in fullscreen
    if pygame.mouse.get_pressed():
        counter = counter + 1
        print counter
        if counter > 10:
            print "Mouse Exit..."
            sys.exit() #end program
    

    #Sets FPS, number of times this will run per second.
    #Lower number is slower
    clock.tick(5)


    #Demo of how to activate the warning notices, simply switching on and off
    if test:
        screen.blit(i_warning_on,(205,5))
        test = False
    else:
        screen.blit(i_warning_off,(205,5))
        test = True
    
    if test:
       screen.blit(chanA, chan_Rect)
    else:
        screen.blit(chanB, chan_Rect)

#Temporary default values, to be replace with calculations and data reads
    throttle_demand = randint(0,100)
    pressure_reading = randint(0,100)
    n1_speed = randint(0,100)
    temp_reading = randint(0,100)
    ambient_pressure = randint(0,100)

    #Call to update all the dials with the new readings
    throttle.update(screen, throttle_demand)
    pressure.update(screen, pressure_reading)
    speed.update(screen, n1_speed)
    temp.update(screen, temp_reading)
    ambPressure.update(screen, ambient_pressure)
    #Call to redraw the screen to show the updates
    pygame.display.update()
