from machine import Pin, I2C, ADC
from dht import DHT22
from time import sleep
from images import snowflake, fire, sun, moon, likeHand, humiditySimbol
import uasyncio as asyncio
import ssd1306
import math
import framebuf

minValue = 65007
maxValue = 512

i2c = I2C(0, scl=Pin(9), sda=Pin(8))

button = Pin(3, Pin.IN)

oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)

dht = DHT22(Pin(26))

adc = ADC(Pin(27))

lastTemp = ""
lastHum = ""
content = 0
lastButtonInteract = button.value()

def calculator():
    brightnessCheck = adc.read_u16()
    totalValue = (brightnessCheck - minValue) / (maxValue - minValue)
    percentageValue = totalValue * 100
    return percentageValue

def display_snow(oled):
    buffer = bytearray(snowflake)
    fb = framebuf.FrameBuffer(buffer, 32, 32, framebuf.MONO_HLSB) 
    oled.blit(fb, 50, 26)
    oled.show()

def display_fire(oled):
    buffer = bytearray(fire)
    fb = framebuf.FrameBuffer(buffer, 32, 32, framebuf.MONO_HLSB) 
    oled.blit(fb, 50, 26)
    oled.show()

def display_sun(oled):
    buffer = bytearray(sun)
    fb = framebuf.FrameBuffer(buffer, 32, 32, framebuf.MONO_HLSB) 
    oled.blit(fb, 50, 26)
    oled.show()

def display_moon(oled):
    buffer = bytearray(moon)
    fb = framebuf.FrameBuffer(buffer, 32, 32, framebuf.MONO_HLSB) 
    oled.blit(fb, 50, 26)
    oled.show()

def display_likeHand(oled):
    buffer = bytearray(likeHand)
    fb = framebuf.FrameBuffer(buffer, 32, 32, framebuf.MONO_HLSB) 
    oled.blit(fb, 50, 26)
    oled.show()

def display_humiditySimbol(oled):
    buffer = bytearray(humiditySimbol)
    fb = framebuf.FrameBuffer(buffer, 64, 64, framebuf.MONO_HLSB) 
    oled.blit(fb, 8, 6)
    oled.show()


async def buttonInteract():
    global content, lastButtonInteract

    while True:
        if lastButtonInteract != button.value():
            lastButtonInteract = button.value()
            if content == 3:
                content = 1
            elif content < 3:
                content = content + 1
        await asyncio.sleep_ms(500)

async def display():
    global lastTemp, lastHum, content

    while True:
        dht.measure()
        temp = dht.temperature()
        hum = dht.humidity()

        if content == 0:
            oled.fill(0)
            oled.text("---------------", 4, 17)
            oled.text("Welcome", 35, 25)
            oled.text("---------------", 4, 33)
        elif content == 1 and lastTemp != temp:
            oled.fill(0)
            lastTemp = temp
            oled.text("| Temperature |", 3, 0)
            if dht.temperature() < -9:
                oled.text(f"{temp} C", 36, 12)
                display_snow(oled)
            elif dht.temperature() >= 10 and dht.temperature() < 32:
                oled.text(f"{temp} C", 40, 12)
                display_likeHand(oled)
            elif dht.temperature() >= 32:
                oled.text(f"{temp} C", 40, 12)
                display_fire(oled)
            elif dht.temperature() > -10 and dht.temperature() < 0:
                oled.text(f"{temp} C", 40, 12)
                display_snow(oled)
            elif dht.temperature() > 0 and dht.temperature() < 10:
                oled.text(f"{temp} C", 44, 12)
                display_snow(oled)
        elif content == 2 and lastHum != hum:
            oled.fill(0)
            lastTemp = ""
            lastHum = hum
            oled.text("| Humidity |", 15, 0)
            display_humiditySimbol(oled)
            if dht.humidity() == 100:
                oled.text(f"{hum}%", 60, 32)
            elif dht.humidity() > 9 and dht.humidity() < 100:
                oled.text(f"{hum}%", 64, 32)
            elif dht.humidity() < 9:
                oled.text(f"{hum}%", 69, 32)
        elif content == 3:
            oled.fill(0)
            lastHum = ""
            percentageValue =  calculator()
            if percentageValue <= 50:
                oled.text("| Night |", 28, 12)
                display_moon(oled)
                await asyncio.sleep(0.500)
            elif percentageValue > 50:
                oled.text("| Day |", 36, 12)
                display_sun(oled)
                await asyncio.sleep(0.500)

        oled.show()
        await asyncio.sleep_ms(500)

loop = asyncio.get_event_loop()
loop.create_task(buttonInteract())
loop.create_task(display())
loop.run_forever()