from app.display.neopixel_64x64 import NeoPixel_64x64
from app.web.wlan import Wlan
import app.utils.colors as colors
import app.display.fonts as fonts

class Weather_Checker():
    wlan: Wlan
    
    def __init__(self, display: NeoPixel_64x64):
        self.display = display
        
    def setup_wlan(self):
        self.wlan = Wlan('WOKWI-Guest', '12345678')


    def check(self, test_mode: bool = False):
        try:
            self.display.clear()
            self.display.write_text('search wlan', 0, 0, color=colors.WHITE)
            
            self.setup_wlan()
            
            self.display.clear()
            self.display.set_font(fonts.font_3x5)
            ypos = 0
            self.display.write_text('wlan connected', 0, ypos, color=colors.RAINBOW[0])
            ypos += self.display.font_height + 1
            self.display.write_text('querying', 0, ypos, color=colors.RAINBOW[1])
            ypos += self.display.font_height + 1
            self.display.write_text('weather data', 0, ypos, color=colors.RAINBOW[2])

            w_resp = self.wlan.actual_weather(lang='de', test_mode=test_mode)

            self.display.clear()

            ypos = 0
            self.display.write_text(f'{str(w_resp.location.name)}', 0, ypos, color=colors.RAINBOW[0])
            ypos += self.display.font_height + 1
            # self.display.set_font(fonts.font_8x8)
            self.display.set_font(fonts.font_5x7)
            self.display.write_text(f'{str(w_resp.current.temp_c)}°C', 0, ypos, color=colors.RAINBOW[1])
            ypos += self.display.font_height + 1
            self.display.write_text(f'{str(w_resp.current.condition.text)}', 0, ypos, color=colors.RAINBOW[2])
            ypos += self.display.font_height + 1
            self.display.write_text(f'upd:{str(w_resp.current.last_updated)[-5:]}', 0, ypos, color=colors.RAINBOW[3])
            ypos += self.display.font_height + 1
            self.display.write_text(f'cur:{str(w_resp.location.localtime)[-5:]}', 0, ypos, color=colors.RAINBOW[4])

            # unten rechts in die Ecke
            ypos = self.display.MATRIX_HEIGHT - self.display.font_height - 1

            self.display.write_text('done.', 38, ypos, color=colors.NEON_GREEN)
        except OSError as e:
            print(f'Error: connection closed - {e}')
        finally:
            print('finally done.')