#!/usr/bin/env python
import time
import subprocess
import re
import logging

DELAY = 10
SCREEN_HEIGHT = 1080
PERCENT_LIMIT = 15
SHOW_CMD = 'xdo show -N Polybar && bspc config top_padding 60'
HIDE_CMD = 'xdo hide -N Polybar && bspc config top_padding 0'

logging.basicConfig(level=logging.INFO)


def get_y_position():
    xdotool_output = subprocess.check_output(
        'xdotool getmouselocation --shell',
        shell=True).decode('utf-8')
    return int(re.search(r'Y=(\d{1,})', xdotool_output).groups()[0]) + 1


hidden = True
subprocess.call(HIDE_CMD, shell=True)
while True:
    y = get_y_position()
    if hidden:
        on_edge = y < 10
    else:
        on_edge = y < 80
    logging.info("Got height %d. On edge: %s", y, str(on_edge))
    if on_edge and hidden:
        subprocess.call(SHOW_CMD, shell=True)
        hidden = False
    if (not on_edge) and (not hidden):
        subprocess.call(HIDE_CMD, shell=True)
        hidden = True
time.sleep(DELAY)
