#!/usr/bin/python3
"""pyhud is an application enabling the use of microcomputers to become a heads up display in vehicles supporting the OBDII standard"""
#####################
#    Imports        #
#####################

import tkinter
import obd

##################
#    Code        #
##################

class GUI(object):
    """This class contains all of the modules for using tkinter as implemented in this program"""
    def __init__(self):
        """Initializes variables"""
        self.rpm = 0
        self.rpmpct = 0
        self.spd = 0
        self.spdpct = 0
        self.TEST = False
        self.DEBUG = False
        self.canvas = tkinter.Canvas(ROOT, bd=0, bg="black", cursor="dot", height=HEIGHT, width=WIDTH)

    def draw(self):
        """Provides logic regarding drawing a single frame"""
        self.canvas.delete("all")
        #RPM Section
        coordrpm = 5, 5, 100, 100
        coordrpmtext = 52.5, 80
        coordrpmlabel = 52.5, 105
        if self.rpm < 3000:
            colorrpm = "#66ff66"# Light Green
        elif self.rpm < 4500:
            colorrpm = "#ffff00"# Yellow
        else:
            colorrpm = "#ff4d4d"# Red
        self.canvas.create_arc(coordrpm, start=-180, extent=-(self.rpmpct * 180), fill=colorrpm)
        self.canvas.create_text(coordrpmtext, fill="#66ff66", font="Monospace 25", text=self.rpm)
        self.canvas.create_text(coordrpmlabel, fill="#66ff66", font="Monospace 10", text="RPM")
        #Speed Section
        coordspd = 105, 5, 200, 100
        coordspdtext = 152.5, 80
        coordspdlabel = 152.5, 105
        if self.spd < 60:
            colorspd = "#66ff66"# Light Green
        elif self.spd < 75:
            colorspd = "#ffff00"# Yellow
        else:
            colorspd = "#ff4d4d"# Red
        self.canvas.create_arc(coordspd, start=-180, extent=-(self.spdpct * 180), fill=colorspd)
        self.canvas.create_text(coordspdtext, fill="#66ff66", font="Monospace 25", text=self.spd)
        self.canvas.create_text(coordspdlabel, fill="#66ff66", font="Monospace 10", text="MPH")
        self.canvas.grid()
        self.canvas.update()

    def convert_obdvals(self):
        """Converts the values in self.rpm and self.spd into percentages of an arc to draw in draw"""
        rpm_min = 0
        rpm_max = 6000
        spd_min = 0
        spd_max = 100

        self.rpmpct = self.rpm / (rpm_max - rpm_min)
        self.spdpct = self.spd / (spd_max - spd_min)

    def obdvals(self):
        """Gets OBD values for specified variables (or fakes them)"""
        if not self.TEST:
            if CONNECTION.is_connected():
                self.rpm = int(CONNECTION.query(obd.commands['RPM']).value)
                self.spd = int(CONNECTION.query(obd.commands['SPEED']).value)
            else:
                if self.rpm != 5000:
                    self.rpm = 5000
                    self.spd = 70
                else:
                    self.rpm = 1000
                    self.spd = 30

    def update(self):
        """Called to update the window"""
        self.obdvals()
        self.convert_obdvals()
        self.draw()
        if self.DEBUG:
            print("Update Run")
            print("RPM:")
            print(self.rpm)
            print("SPD:")
            print(self.spd)
        ROOT.after(250, self.update)

#init tkinter
ROOT = tkinter.Tk()
# Open Full Screen
# NOTE: This takes into account the total size of the current workspace
WIDTH, HEIGHT = ROOT.winfo_screenwidth(), ROOT.winfo_screenheight()
ROOT.overrideredirect(1)
ROOT.geometry("%dx%d+0+0" % (WIDTH, HEIGHT))

# init GUI obj
GUI = GUI()

#init obd
CONNECTION = obd.OBD()

GUI.update()
ROOT.mainloop()

CONNECTION.close()
