import subprocess
import re
import numpy as np

from libqtile.config import Screen

from Bars import top_bar, left_bar, main_bar, secondary_bar
from defines import main_screen_res, top_screen_res

screens = []


cmd = ['xrandr']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
resolution_string, junk = p.communicate()
p.stdout.close()
screen_resolutions = [np.array(screen_res.split('x')).astype(int) for screen_res in re.findall('[0-9]+x[0-9]+(?=[^\\\\n]*\*)',str(resolution_string))]
number_of_screens = len(screen_resolutions)
max_width = max(screen_resolutions, key=lambda res: res[0])[0]
defined_main_window = False
for width, height in screen_resolutions:
    if width == main_screen_res[0] and height == main_screen_res[1]:
        screens.append(Screen(top=main_bar, wallpaper= "~/Pictures/Wallpapers/gruvbox-like.jpg", wallpaper_mode="fill"))
        defined_main_window = True
    elif width == top_screen_res[0] and height == top_screen_res[1]:
        screens.append(Screen(bottom=top_bar, wallpaper="~/Pictures/Wallpapers/wallpaperflare.com_wallpaper.jpg", wallpaper_mode="fill"))
    elif width < height:
        screens.append(Screen(top=left_bar, wallpaper="~/Pictures/Wallpapers/gruvbox-like-left.jpg", wallpaper_mode="fill"))
    elif width == max_width and not defined_main_window:
        screens.append(Screen(top=main_bar, wallpaper= "~/Pictures/Wallpapers/gruvbox-like.jpg", wallpaper_mode="fill"))
        defined_main_window = True
    else:
        screens.append(Screen(top=secondary_bar, wallpaper= "~/Pictures/Wallpapers/gruvbox-like.jpg", wallpaper_mode="fill"))
