# Path: interface/messages.py
# def print_error(*args, **kwargs):
#     print(Color.RED + Color.BOLD + "[-]" + Color.ENDC, *args, **kwargs)
# 
# def print_info(*args, **kwargs):
#     print(Color.BLUE + Color.BOLD + "[*]" + Color.ENDC, *args, **kwargs)


import subprocess
import time
import re
import os
import sys
import interface.utils
import core.globals
from interface.messages import print_error, print_info
# Pull REXT from git repo
def update_rext():
    subprocess.Popen("git pull", shell=True).wait()
    time.sleep(4)


# Reset HEAD to discard local changes and pull
def update_rext_force():
    subprocess.Popen("git reset --hard", shell=True).wait()
    subprocess.Popen("git pull", shell=True).wait()
    time.sleep(4)


# Download OUI file, and recreate DB
def update_oui():
    if interface.utils.file_exists("./databases/oui.db") and core.globals.ouidb_conn is not None:
            connection = core.globals.ouidb_conn
            cursor = connection.cursor()
            # Truncate database
            print_info("Truncating oui table")
            cursor.execute("""DROP TABLE oui""")
            cursor.execute("""CREATE TABLE oui (
                             id INTEGER PRIMARY KEY NOT NULL,
                             oui TEXT UNIQUE,
                             name TEXT)""")
            # This is very important, sqlite3 creates transaction for every INSERT/UPDATE/DELETE
            # but can handle only dozen of transactions at a time.
            # BEGIN guarantees that only one transaction will be used.
            # Now the DB rebuild should take only seconds
            cursor.execute('begin')

# Path: interface/messages.py
# def print_error(*args, **kwargs):
#     print(Color.RED + Color.BOLD + "[-]" + Color.ENDC, *args, **kwargs)
# 
# def print_info(*args, **kwargs):
#     print(Color.BLUE + Color.BOLD + "[*]" + Color.ENDC, *args, **kwargs)


# Path: core/updater.py
import subprocess
import time
import re
import os
import sys
import interface.utils
import core.globals
from interface.messages import print_error, print_info

# Pull REXT from git repo
def update_rext():
    subprocess.Popen("git pull", shell=True).wait()
    time.sleep(4)


# Reset HEAD to discard local changes and pull
def update_rext_force():
    subprocess.Popen("git reset --hard", shell=True).wait()
    subprocess.Popen("git pull", shell=True).wait()
    time.sleep(4)


# Download OUI file, and recreate DB
def update_oui():
    if interface.utils.file_exists("./databases/oui.db") and core.globals.ouidb_conn is not None:
            connection = core.globals.ouidb_conn
            cursor = connection.cursor()
            # Truncate database
            print_info("Truncating oui table")
            cursor.execute("""DROP TABLE oui""")
            cursor.execute("""CREATE TABLE oui (
                             id INTEGER PRIMARY KEY NOT NULL,
                             oui TEXT UNIQUE,
                             name TEXT)""")
            # This is very important, sqlite3 creates transaction for every INSERT/UPDATE/DELETE
            # but can handle only dozen of transactions at a time.
            # BEGIN guarantees that only one transaction will be used.
            # Now the DB rebuild should take only seconds
            cursor.execute('begin')

            print_info("Downloading new OUI file")