from app.web.wlan import Wlan
import app.utils.colors as colors
from app.display.fonts.font_3x5 import font_3x5
from app.display.fonts.font_5x7 import font_5x7


def weather_check(display, test_mode: bool = False):
	wlan = Wlan('WOKWI-Guest', '12345678')

	try:
		display.clear()
		display.write_text('search wlan', 0, 0, color=colors.WHITE)

		display.clear_row(0, effect=False)

		display.clear()
		display.set_font(font_3x5)
		display.write_text('wlan connected', 0, 0, color=colors.RAINBOW[0])
		display.write_text('querying', 0, 8, color=colors.RAINBOW[1])
		display.write_text('weather data', 0, 16, color=colors.RAINBOW[2])

		w_resp = wlan.actual_weather(lang='en', test_mode=test_mode)

		display.clear()

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

		ypos += 3 * delta
		display.write_text('ready.', 30, ypos, color=colors.WHITE)

	except OSError as e:
		print(f'Error: connection closed - {e}')
	finally:
		print('finally done.')
