# THIS FILE IS A PART OF VCStudio
# PYTHON 3


import os
w, h = os.get_terminal_size() 

from settings import settings
from settings import talk

def cls():
    #cleaning the terminal
    os.system("clear")
    
    global w
    global h
    
    w, h = os.get_terminal_size()
    if (w % 2) == 0:
        w = w - 1


def output(form, text=""):
    #Basically a fancy print() function
    while len(text) < w:
        text = text + " "
        
    print(form + text)
    
    


def autofix(Modules):
    output("\033[1;41m", "Autofix will try to install")
    output("\033[1;41m", "all missing modules. Some may")
    output("\033[1;41m", "request a sudo password.")
    print("\033[1;m")
    print("Press ENTER to continue")
    input()

    print("\033[1;43m")

    # Let's install the GI repository. ( Grpahical Interface )
    # This is going to be different depending on the system
    # for more info look at https://pygobject.readthedocs.io/en/latest/getting_started.html

    system = "debian"
    
    import platform
    ostype = platform.system()
    if ostype == "Linux":

        print("Select GNU / Linux fork: debian, arch, fedora, opensuse\033[1;m")
        system = input(": ")
        
    
        command = "sudo apt install libgirepository1.0-dev gcc libcairo2-dev pkg-config python3-dev gir1.2-gtk-3.0"
        if system == "arch":
            command = "sudo pacman -S python cairo pkgconf gobject-introspection gtk3"
        elif system == "fedora":
            command = "sudo dnf install gcc gobject-introspection-devel cairo-devel pkg-config python3-devel gtk3"
        elif system == "opensuse":
            command = "sudo zypper install cairo-devel pkg-config python3-devel gcc gobject-introspection-devel"
        elif system == "debian":
            pass
            
        else:
            # If not any of them
            print("You are running an unknown GNU / Linux distro.")
            print("Please refer to: https://pygobject.readthedocs.io/en/latest/getting_started.html")
            print("Autofix can't help you here.")

        print("\033[1;43m")
        print("Executing:", command , "\033[1;m")
        os.system(command)
        
        print("\033[1;43m")
        command = "python3 -m pip install pycairo"
        print("Executing:", command , "\033[1;m")
        os.system(command)

        print("\033[1;43m")
        command = "python3 -m pip install PyGObject"
        print("Executing:", command , "\033[1;m")
        os.system(command)

    elif ostype == "Darwin": # MacOS

        print("\033[1;43m")
        command = "brew install pygobject3 gtk+3"
        print("Executing:", command , "\033[1;m")
        os.system(command)
        
    else:
        # If it's not linux.
        print("You are running non a GNU / Linux distro.")
        print("Please refer to: https://pygobject.readthedocs.io/en/latest/getting_started.html")
        print("Autofix can't help you here.")
    
    # Let's install the PIL module

    print("\033[1;43m")
    command = "python3 -m pip install Pillow"
    print("Executing:", command , "\033[1;m")
    os.system(command)
    
    output("\033[1;42m", "Done!")
    print("\033[1;m")
    print("If any issues are still there. Please report them to:")
    print("https://notabug.org/jyamihud/VCStudio/issues")
    print("\033[1;m")
    print("Press ENTER to continue")
    input()

    import sys
    os.execl(sys.executable, sys.executable, *sys.argv)