""" Classes Method and Functions for lx Apps """

import gettext
import locale
import os
import shutil
import signal
import subprocess
import sys
import tkinter as tk
import zipfile
from datetime import datetime
from pathlib import Path
from subprocess import check_call
from tkinter import ttk

import requests

APP = "wirepy"
LOCALE_DIR = "/usr/share/locale/"
locale.bindtextdomain(APP, LOCALE_DIR)
gettext.bindtextdomain(APP, LOCALE_DIR)
gettext.textdomain(APP)
_ = gettext.gettext

wg_set = Path(Path.home() / ".config/wire_py/settings")


class Create:
    """
    This class is for the creation of the folders and files
    required by Wire-Py, as well as for decryption
    the tunnel from the user's home directory
    """

    @staticmethod
    def dir_and_files():
        """
        check and create folders and files if not present
        """

        pth = Path.home() / ".config/wire_py"
        pth.mkdir(parents=True, exist_ok=True)
        sett = Path.home() / ".config/wire_py/settings"
        ks = Path.home() / ".config/wire_py/keys"

        if sett.exists():
            pass

        else:
            sett.touch()
            sett.write_text(
                "[UPDATES]\non\n[THEME]\nlight\n[TOOLTIP]\nTrue\n[AUTOSTART ON]\noff\n"
            )

        if ks.exists():
            pass

        else:
            ks.touch()

    @staticmethod
    def files_for_autostart():
        """
        check and create file for auto start if not present and enable the service
        """

        pth2 = Path.home() / ".config/systemd/user"
        pth2.mkdir(parents=True, exist_ok=True)
        wg_ser = Path.home() / ".config/systemd/user/wg_start.service"

        if wg_ser.exists():
            pass

        else:
            wg_ser.touch()
            wg_ser.write_text(
                "[Unit]\nDescription=Automatic Tunnel Start\nAfter=network-online.target"
                "\n\n[Service]\nType=oneshot\nExecStartPre=/bin/sleep 5\nExecStart=/usr/"
                "local/bin/start_wg.py\n[Install]\nWantedBy=default.target"
            )
            check_call(["systemctl", "--user", "enable", "wg_start.service"])

    @staticmethod
    def make_dir():
        """Dirname "tlecdewg" = Tunnel Encrypt Decrypt Wireguard"""

        dirname = Path("/tmp/tlecdcwg/")
        if dirname.exists():
            pass
        else:
            dirname.mkdir()

    @staticmethod
    def decrypt():
        """
        This start ssl_decrypt file
        """
        process = subprocess.run(
            ["pkexec", "/usr/local/bin/ssl_decrypt.py"],
            stdout=subprocess.PIPE,
            text=True,
            check=True,
        )
        path = Path.home() / ".config/wire_py/"
        file_in_path = list(path.rglob("*.dat"))
        if file_in_path:
            if process.returncode == 0:
                print("File successfully decrypted...")
            else:
                print(f"Error with the following code... {process.returncode}")
        else:
            print(_("Ready for import"))

    @staticmethod
    def encrypt():
        """
        this start ssl_encrypt file
        """
        process = subprocess.run(
            ["pkexec", "/usr/local/bin/ssl_encrypt.py"],
            stdout=subprocess.PIPE,
            text=True,
            check=True,
        )
        print(process.stdout)
        if process.returncode == 0:
            print("All Files successfully encrypted...")
        else:
            print(f"Error with the following code... {process.returncode}")


def uos():
    """

    uos = LOGIN USERNAME

    This method displays the user name of the logged-in user,
    even if you are rooted in a shell
    """
    logname = f"{Path.home()}"[6:]
    file = Path.home() / "/tmp/.loguser"
    with open(file, "w", encoding="utf-8") as f:
        f.write(logname)


class GiteaUpdate:
    """
    Calling download requests the download URL of the running script,
    the taskbar image for the “Download OK” window, the taskbar image for the
    “Download error” window and the variable res
    """

    @staticmethod
    def api_down(update_api_url, version):
        """
        Calling api_down requests the URL and the version of the running script.
        Example: version = 'v. 1.1.1.1' GiteaUpdate.api_down(http://example.de, version)
        """
        try:
            response = requests.get(update_api_url, timeout=10)
            response_dict = response.json()
            response_dict = response_dict[0]
            with open(wg_set, "r", encoding="utf-8") as set_file:
                set_file = set_file.read()
                if "on\n" in set_file:
                    if version[3:] != response_dict["tag_name"]:
                        req = response_dict["tag_name"]
                    else:
                        req = "No Updates"
                else:
                    req = "False"
                return req
        except requests.exceptions.RequestException:
            req = "No Internet Connection!"
            return req

    @staticmethod
    def download(urld, down_ok_image, down_not_ok_image, res):
        """
        this is for download new Version of wirepy
        """
        try:
            to_down = f"wget -qP {Path.home()} {" "} {urld}"
            result = subprocess.call(to_down, shell=True)
            if result == 0:
                shutil.chown(f"{Path.home()}/{res}.zip", 1000, 1000)

                # img_w, img_i, w_title, w_txt hand over
                iw = r"/usr/share/icons/lx-icons/64/info.png"
                ii = down_ok_image
                wt = _("Download Successful")
                msg_t = _("Your zip file is in home directory")
                msg_window(iw, ii, wt, msg_t)

            else:

                # img_w, img_i, w_title, w_txt hand over
                iw = r"/usr/share/icons/lx-icons/64/error.png"
                ii = down_not_ok_image
                wt = _("Download error")
                msg_t = _("Download failed! Please try again")
                msg_window(iw, ii, wt, msg_t)
        except subprocess.CalledProcessError:

            # img_w, img_i, w_title, w_txt hand over
            iw = r"/usr/share/icons/lx-icons/64/error.png"
            ii = down_not_ok_image
            wt = _("Download error")
            msg_t = _("Download failed! No internet connection!")
            msg_window(iw, ii, wt, msg_t)


def msg_window(img_w, img_i, w_title, w_txt, txt2=None, com=None):
    """
    Function for different message windows for the user. with 4 arguments to be passed.
    To create messages with your own images, icons, and titles.
    As an alternative to Python Messagebox.
    Paths to images must be specified: r'/usr/share/icons/lx-icons/64/info.png'
    img_w = Image for Tk Window
    img_i = Image for Icon
    w_title = Windows Title
    w_txt = Text for Tk Window
    txt2 = Text for Button two
    com = function for Button command
    """
    msg = tk.Toplevel()
    msg.resizable(width=False, height=False)
    msg.title(w_title)
    msg.configure(pady=15, padx=15)
    msg.img = tk.PhotoImage(file=img_w)
    msg.i_window = tk.Label(msg, image=msg.img)

    label = tk.Label(msg, text=w_txt)

    label.grid(column=1, row=0)

    if txt2 is not None and com is not None:
        label.config(font=("Ubuntu", 11), padx=15, justify="left")
        msg.i_window.grid(column=0, row=0, sticky="nw")
        button2 = ttk.Button(msg, text=f"{txt2}", command=com, padding=4)
        button2.grid(column=0, row=1, sticky="e", columnspan=2)
        button = ttk.Button(msg, text="OK", command=msg.destroy, padding=4)
        button.grid(column=0, row=1, sticky="w", columnspan=2)

    else:
        label.config(font=("Ubuntu", 11), padx=15)
        msg.i_window.grid(column=0, row=0)
        button = ttk.Button(msg, text="OK", command=msg.destroy, padding=4)
        button.grid(column=0, columnspan=2, row=1)

    img_i = tk.PhotoImage(file=img_i)
    msg.iconphoto(True, img_i)
    msg.columnconfigure(0, weight=1)
    msg.rowconfigure(0, weight=1)
    msg.winfo_toplevel()


class Tunnel:
    """
    Class of Methods for Wire-Py
    """

    @classmethod
    def con_to_dict(cls, file):
        """
        The config file is packed into a dictionary,
        to display the values Address , DNS and Peer in the labels
        """

        dictlist = []
        for lines in file.readlines():
            line_plit = lines.split()
            dictlist = dictlist + line_plit
        dictlist.remove("[Interface]")
        dictlist.remove("[Peer]")
        for items in dictlist:
            if items == "=":
                dictlist.remove(items)
            if items == "::/0":
                dictlist.remove(items)

        # Here is the beginning (Loop) of convert List to Dictionary
        for _ in dictlist:
            a = [dictlist[0], dictlist[1]]
            b = [dictlist[2], dictlist[3]]
            c = [dictlist[4], dictlist[5]]
            d = [dictlist[6], dictlist[7]]
            e = [dictlist[8], dictlist[9]]
            f = [dictlist[10], dictlist[11]]
            g = [dictlist[12], dictlist[13]]
            h = [dictlist[14], dictlist[15]]
            new_list = [a, b, c, d, e, f, g, h]
            final_dict = {}
            for elements in new_list:
                final_dict[elements[0]] = elements[1]

            # end... result a Dictionary

        address = final_dict["Address"]
        dns = final_dict["DNS"]
        if "," in dns:
            dns = dns[:-1]
        endpoint = final_dict["Endpoint"]
        pre_key = final_dict.get("PresharedKey")
        if pre_key is None:
            pre_key = final_dict.get("PreSharedKey")
        return address, dns, endpoint, pre_key

    @staticmethod
    def active():
        """
        Shows the Active Tunnel
        """
        active = (
            os.popen('nmcli con show --active | grep -iPo "(.*)(wireguard)"')
            .read()
            .split()
        )
        if not active:
            active = ""
        else:
            active = active[0]

        return active

    @staticmethod
    def list():
        """
        Shows all existing Wireguard tunnels a login user
        """
        dirname = Path("/tmp/tlecdcwg/")
        wg_s = os.listdir(dirname)

        return wg_s

    @staticmethod
    def export():
        """
        This will export the tunnels.
        A zipfile with current date and time is created
        in the user's home directory with correct right
        """
        now_time = datetime.now()
        now_datetime = now_time.strftime(f"wg-exp-%m-%d-%Y-%H:%M")
        tl = Tunnel.list()

        try:
            if len(tl) != 0:
                wg_tar = f"{Path.home()}/{now_datetime}"
                shutil.copytree("/tmp/tlecdcwg/", "/tmp/wire_py", dirs_exist_ok=True)
                source = Path("/tmp/wire_py")
                shutil.make_archive(wg_tar, "zip", source)
                shutil.rmtree(source)
                with zipfile.ZipFile(f"{wg_tar}.zip", "r") as zf:
                    if len(zf.namelist()) != 0:

                        # img_w, img_i, w_title, w_txt hand over
                        iw = r"/usr/share/icons/lx-icons/64/info.png"
                        ii = r"/usr/share/icons/lx-icons/48/wg_vpn.png"
                        wt = _("Export Successful")
                        msg_t = _("Your zip file is in home directory")
                        msg_window(iw, ii, wt, msg_t)

                    else:

                        # img_w, img_i, w_title, w_txt hand over
                        iw = r"/usr/share/icons/lx-icons/64/error.png"
                        ii = r"/usr/share/icons/lx-icons/48/wg_msg.png"
                        wt = _("Export error")
                        msg_t = _("Export failed! Please try again")
                        msg_window(iw, ii, wt, msg_t)

            else:

                # img_w, img_i, w_title, w_txt hand over
                iw = r"/usr/share/icons/lx-icons/64/info.png"
                ii = r"/usr/share/icons/lx-icons/48/wg_msg.png"
                wt = _("Select tunnel")
                msg_t = _("Please first import tunnel")
                msg_window(iw, ii, wt, msg_t)

        except TypeError:
            pass


def sigi(dirname):
    """
    function for clean up after break
    """

    def signal_handler(signum, frame):
        """
        Determine clear text names for signal numbers
        """
        signals_to_names_dict = dict(
            (getattr(signal, n), n)
            for n in dir(signal)
            if n.startswith("SIG") and "_" not in n
        )
        signame = signals_to_names_dict.get(signum, f"Unnamed signal: {signum}")

        # End program for certain signals, report to others only reception
        if signum in (signal.SIGINT, signal.SIGTERM):
            exit_code = 1
            print(
                f"\nSignal {signame} {(signum)} received. => Aborting with exit code {exit_code}."
            )
            shutil.rmtree(dirname)
            Path.unlink("/tmp/.loguser")
            print("Breakdown by user...")
            sys.exit(exit_code)
        else:
            print(f"Signal {signum} received and ignored.")
            shutil.rmtree(dirname)
            Path.unlink("/tmp/.loguser")
            print("Process unexpectedly ended...")

    signal.signal(signal.SIGINT, signal_handler)
    signal.signal(signal.SIGTERM, signal_handler)
    signal.signal(signal.SIGHUP, signal_handler)


def if_tip(path):
    """
    method that writes in file whether tooltip is displayed or not
    """
    with open(path, "r", encoding="utf-8") as set_file2:
        lines2 = set_file2.readlines()
        if "False\n" in lines2:
            tip = False
        else:
            tip = True
        return tip


class Tooltip:
    """
    class for Tooltip

    imoprt Tooltip

    example: Tooltip(label, "Show tooltip on label")
    examble: Tooltip(button, "Show tooltip on button")
    info: label and button is parrent.
    """

    def __init__(self, widget, text, tips=None):
        self.widget = widget
        self.text = text
        self.tooltip_window = None
        if tips:
            self.widget.bind("<Enter>", self.show_tooltip)
            self.widget.bind("<Leave>", self.hide_tooltip)

    def show_tooltip(self, event=None):
        """
        shows the tooltip
        """
        if self.tooltip_window or not self.text:
            return

        x, y, _, _ = self.widget.bbox("insert")
        x += self.widget.winfo_rootx() + 65
        y += self.widget.winfo_rooty() + 40
        self.tooltip_window = tw = tk.Toplevel(self.widget)
        tw.wm_overrideredirect(True)
        tw.wm_geometry(f"+{x}+{y}")

        label = tk.Label(
            tw,
            text=self.text,
            background="lightgreen",
            foreground="black",
            relief="solid",
            borderwidth=1,
            padx=5,
            pady=5,
        )
        label.grid()

    def hide_tooltip(self, event=None):
        """
        hide the tooltip
        """
        if self.tooltip_window:
            self.tooltip_window.destroy()
            self.tooltip_window = None