import display
import utime
import math
import leds

def minValue(n1,n2):
    if (n1 < n2):
        return n1
    else:
        return n2

def maxValue(n1,n2):
    if (n1 > n2):
        return n1
    else:
        return n2

class FairyDust:
    def __init__(self, x = 0, y = 0):
        self.raw = []
        try:
            with open('/apps/tree/fairydust.bin', 'rb') as fh:
                self.raw = bytearray(fh.read())
        except:
            print('Errr on reading file\n')
        self.x = x
        self.y = y

    def draw(self, displ):
        for counter in range(0,len(self.raw),3):
            rgb = (self.raw[counter], self.raw[counter+1], self.raw[counter+2])
            xp = int(int(counter / 3) / 64)
            yp = int(int(counter / 3) % 64)
            try:
                if rgb[0] != 255 and rgb[1] != 0 and rgb[2] != 215:
                    displ.pixel(xp + self.x, yp + self.y, col = rgb)
            except:
                pass

class Tree:
    def __init__(self, x = 0, y = 0):
        self.treeRaw = []
        try:
            with open('/apps/tree/tree.bin', 'rb') as fh:
                self.treeRaw = bytearray(fh.read())
        except:
            print('Errr on reading file\n')
        self.x = x
        self.y = y

    def draw(self, displ):
        for counter in range(0,len(self.treeRaw),3):
            rgb = (self.treeRaw[counter], self.treeRaw[counter+1], self.treeRaw[counter+2])
            xp = int(int(counter / 3) / 48)
            yp = int(int(counter / 3) % 48)
            try:
                if rgb[0] != 255 and rgb[1] != 0 and rgb[2] != 215:
                    displ.pixel(xp + self.x, yp + self.y, col = rgb)
            except:
                pass

class Ground:
    def __init__(self):
        pass

    def draw(self, displ):
        try:
            displ.rect(0, 40, 160, 80, col=(140, 159, 74), filled=True, size=1)
        except:
            pass

class Heaven:
    def __init__(self):
        pass

    def draw(self, displ):
        try:
            displ.rect(0, 0, 160, 80, col=(0, 0, 0), filled=True, size=1)
        except:
            pass

class Laser:
    def __init__(self, color, targetX, targetY, radius, steps):
        self.color = color
        self.positionDeg = 0
        self.targetX = targetX
        self.targetY = targetY
        self.radius = radius
        self.positionDeg = 180
        self.lastLED = 0
        self.steps = steps

    def update(self):
        self.positionDeg = (self.positionDeg + self.steps) % 360

        self.currentX = int(self.radius * math.cos(self.positionDeg * math.pi / 180) + self.targetX)
        self.currentY = int(self.radius * math.sin(self.positionDeg * math.pi / 180) + self.targetY)

        varX = maxValue(0, minValue(159, self.currentX))
        varY = maxValue(0, minValue(79, self.currentY))

        if (varY <= 0):
            ledPos = 10 - (int(varX / 14.5) % 11)
            print(ledPos)
            try:
                leds.set(ledPos, (self.color))
                if (self.lastLED != ledPos):
                    leds.set(self.lastLED, (0,0,0))
            except Exception as e:
                print(e)
            self.lastLED = ledPos
        else :
            try:
                leds.set(self.lastLED, (0,0,0))
                self.lastLED = 0
            except Exception as e:
                print(e)

        self.currentX = varX
        self.currentY = varY

    def draw(self, displ):
        try:
            displ.line(79, 79, self.currentX, self.currentY, col=self.color)
        except Exception as e:
            print(e)


ground = Ground()
heaven = Heaven()
tree = Tree(-5, 15)
tree2 = Tree(26, -2)
tree3 = Tree(120, 7)
fairydust = FairyDust(54,0)
laser1 = Laser((255,0,0), 80, 5, 25, 30)
laser2 = Laser((0,255,0), 20, 20, 45, -20)
laser3 = Laser((0,0,255), 90, -5, 15, 10)


while True:
    with display.open() as d:
        laser1.update()
        laser2.update()
        laser3.update()

        heaven.draw(d)
        ground.draw(d)
        tree.draw(d)
        tree2.draw(d)
        tree3.draw(d)
        fairydust.draw(d)
        laser1.draw(d)
        laser2.draw(d)
        laser3.draw(d)

        current = utime.localtime()
        hour = current[3]
        min = current[4]
        hourStr = ''
        minStr = ''
        if hour < 10:
            hourStr = ' ' + str(hour)
        else:
            hourStr = str(hour)
        if min < 10:
            minStr = '0' + str(min)
        else:
            minStr = str(min)

        time = hourStr + ':' + minStr
        d.print(time, fg = (0,0,0), bg = (140, 159, 74), posx = 90, posy = 60)
        d.update()
        d.close()
    #utime.sleep_ms(33) # Feed watch doge