#!/usr/bin/python3

import requests
from colorama import Fore
from urllib.parse import quote

def split_command(full_command: str):
	quoted = False
	word = ''
	words = []
	for char in full_command:
		if char == ' ' and not quoted:
			words.append(word)
			word = ''
		elif char == "'":
			quoted = not quoted
		else:
			word += char
	if word != '':
		words.append(word)
	return words

class Dest:
	def __init__(self, url):
		self.url = url

	def send(self, req):
		print(requests.get(f'{self.url}{req}').content.decode('UTF-8'))

def main():
	command = None
	lcd = Dest('http://lizard.local')
	while True:
		full_command = input('>')
		words = split_command(full_command)
		command = words[0]
		args = words[1:]
		if command == 'clear':
			lcd.send('/clear')
		elif command == 'print':
			lcd.send(f'/print?text={quote(" ".join(args))}')
		elif command == 'goto':
			lcd.send(f'/set_cursor_pos?line={quote(args[0])}&col={quote(args[1])}')
		elif command == 'display':
			visible = '0'
			cursor = '0'
			blink = '0'
			for arg in args:
				if arg == 'visible':
					visible = '1'
				elif arg == 'cursor':
					cursor = '1'
				elif arg == 'blink':
					blink = '1'
			lcd.send(f'/display?visible={quote(visible)}&cursor={quote(cursor)}&cursor_blink={quote(blink)}')
		elif command == 'exit':
			break
		else:
			print(Fore.LIGHTRED_EX + 'Command not found' + Fore.RESET)

if __name__ == '__main__':
	main()
