#!/usr/bin/python3

from argparse import ArgumentParser
from pathlib import Path
from subprocess import call, check_output
from threading import Thread
from time import sleep
import os
import re
import shutil
import sys

service_names = [
    "pt-desktop-message.service",
    "pt-device-manager.service",
    "pt-os-dashboard.service",
    "pt-poweroff-v1.service",
    "pt-poweroff-v2.service",
    "pt-splashscreen.service",
    "pt-update.service",
    "pt-xhost-fix.service"
]


class TerminalStyle:

    WHITE = "\033[97m"
    BLUE = "\033[94m"
    GREEN = "\033[92m"
    YELLOW = "\033[93m"
    RED = "\033[91m"
    DIM = "\033[2m"
    BOLD = "\033[1m"
    UNDERLINE = "\033[4m"
    CLEAR = "\033[0m"


class Service:

    def __init__(self):

        self._name = ""
        self._load_state = ""
        self._active_state = ""
        self._enabled = ""

    def set_name(self, value):
        self._name = value.strip(" \t\n\r")

    def set_load_state(self, value):
        self._load_state = value.strip(" \t\n\r")

    def set_active_state(self, value):
        self._active_state = value.strip(" \t\n\r")

    def set_enabled(self, value):
        self._enabled = value.strip(" \t\n\r")

    def name(self):
        return self._name

    def load_state(self):
        return self._load_state

    def active_state(self):
        return self._active_state

    def enabled(self):
        return self._enabled


class Formatter:
    @staticmethod
    def format_name(input_string):
        return TerminalStyle.BOLD + input_string + TerminalStyle.CLEAR

    @staticmethod
    def format_load_state(input_string):
        if (input_string.strip(" \t\n\r") == "loaded"):
            return TerminalStyle.GREEN + input_string + TerminalStyle.CLEAR
        return TerminalStyle.RED + input_string + TerminalStyle.CLEAR

    @staticmethod
    def format_active_state(input_string):
        if (input_string.strip(" \t\n\r") == "active"):
            return TerminalStyle.GREEN + input_string + TerminalStyle.CLEAR
        elif (input_string.strip(" \t\n\r") == "inactive"):
            return TerminalStyle.DIM + input_string + TerminalStyle.CLEAR
        return TerminalStyle.RED + input_string + TerminalStyle.CLEAR

    @staticmethod
    def format_enabled(input_string):
        if (input_string.strip(" \t\n\r") == "enabled"):
            return TerminalStyle.GREEN + input_string + TerminalStyle.CLEAR
        elif (input_string.strip(" \t\n\r") == "disabled"):
            return TerminalStyle.DIM + input_string + TerminalStyle.CLEAR
        return TerminalStyle.RED + input_string + TerminalStyle.CLEAR


def show_status():

    services = []

    for service_name in service_names:

        output = check_output(['systemctl', 'show', service_name, "--full", "--no-legend", "--lines=0", "--no-pager"])
        output = output.decode("UTF-8")

        lines = output.split("\n")

        service = Service()

        for line in lines:
            if ("Id=" in line):
                service.set_name(line.replace("Id=", ""))
            elif ("LoadState=" in line):
                service.set_load_state(line.replace("LoadState=", ""))
            elif ("ActiveState=" in line):
                service.set_active_state(line.replace("ActiveState=", ""))
            elif ("UnitFileState=" in line):
                service.set_enabled(line.replace("UnitFileState=", ""))

        services.append(service)

    longest_service_name = 0
    longest_load_state = 0
    longest_active_state = 0
    longest_enabled = 0

    for service in services:

        if (len(service.name()) > longest_service_name):
            longest_service_name = len(service.name())

        if (len(service.load_state()) > longest_load_state):
            longest_load_state = len(service.load_state())

        if (len(service.active_state()) > longest_active_state):
            longest_active_state = len(service.active_state())

        if (len(service.enabled()) > longest_enabled):
            longest_enabled = len(service.enabled())

    # print("Service".ljust(longest_service_name) + "Loaded".ljust(longest_load_state) + "Enabled".ljust(longest_enabled) + "Active".ljust(longest_active_state))

    for service in services:

        print(Formatter.format_name(service.name().ljust(longest_service_name)) + "  " +
              Formatter.format_load_state(service.load_state().ljust(longest_load_state)) + " " +
              Formatter.format_enabled(service.enabled().ljust(longest_enabled)) + " " +
              Formatter.format_active_state(service.active_state().ljust(longest_active_state)))

    # print("─" * (longest_service_name + longest_load_state + longest_active_state + longest_enabled))


####################################

args = sys.argv[1:]

enable = ("enable" in args)
disable = ("disable" in args)

commands_count = 0

if (enable):
    commands_count += 1
if (disable):
    commands_count += 1

if (commands_count > 1):
    print("Please use one command at a time")
else:
    for service_name in service_names:
        if (enable):
            call(['sudo', 'systemctl', 'enable', service_name, "--quiet"])
        elif (disable):
            call(['sudo', 'systemctl', 'disable', service_name, "--quiet"])

    show_status()
