#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Personal functions to aid on multiple scripts"""
import sys
import fileinput
import re
import os
import logging
import shlex
import subprocess
import shutil
from typing import Sequence
from pathlib import Path
import yaml

VERBOSE_G = False

LOG = logging.getLogger()
HANDLER = logging.StreamHandler()
FORMATTER = logging.Formatter(
    "[%(filename)s][%(levelname)s] %(funcName)s '%(message)s'"
)
HANDLER.setFormatter(FORMATTER)
LOG.addHandler(HANDLER)
LOG.setLevel(logging.INFO)


def validate_x_link(line: str) -> str:
    """returns a fixed link, which ends with /media"""
    # if url contains /media at the end just write the line
    if re.search(r"\/media$", line):
        return line
    # if does not contain /media at the end then add /media
    return f"{line}/media"


def parse_link(link: str) -> str:
    """Fixes links"""
    if not re.search(r"(x\.com\/\w+(\/)?(?!.*status))", link):
        LOG.debug("No modifications needed for the link %s", link)
        return link
    # if url contains /media at the end just write the line
    fixed_link = validate_x_link(link)
    LOG.debug("Processed link %s", fixed_link)
    return fixed_link


def load_config_variables():
    """Loads all the variables from the config file"""
    config_file = Path("~/.config/jawz/config.yaml")
    with open(config_file.expanduser(), encoding="utf-8") as open_file:
        return yaml.safe_load(open_file)


def clean_cache(directory: Path):
    """Recursively deletes all the content of a directory,
    including the directory itself."""
    if not directory.is_dir():
        return
    shutil.rmtree(directory)


def run(command: str | Sequence[str], verbose: bool, cwd: Path | None = None) -> None:
    """Run command in a subprocess"""
    # pylint: disable=subprocess-run-check
    # This toggle allows for a really wasy debug when using -v
    if verbose:
        if isinstance(command, (list, tuple)):
            print(shlex.join(command))
        else:
            print(command)
        return

    if isinstance(command, str):
        expanded = os.path.expandvars(command)
        args = shlex.split(expanded)
    else:
        args = list(command)

    subprocess.run(args, check=False, cwd=cwd)


def list_lines(i: int, line: str) -> str:
    """Create a numbered list"""
    return f"{i}) {line}"


def quote(line: str) -> str:
    """Quote the line"""
    return f'"{line}"'


def sort_txt_file(file_path: Path):
    """Sort every line alphabetically
    remove duplicated and empty lines"""
    file = str(file_path.resolve())
    run(["sort", "-u", file, "-o", file], VERBOSE_G)
    run(["sed", "-i", "/^$/d", file], VERBOSE_G)
    run(["sed", "-i", "-e", "s,http:,https:,", file], VERBOSE_G)
    # fix this using strip on python
    # line.strip("/")
    run(["sed", "-i", "-e", "s,/$,,", file], VERBOSE_G)  # trailing /


def randomize_txt_file(file_path: Path):
    """Randomize the order of the
    lines of the txt file"""
    file = str(file_path.resolve())
    run(["sort", "-R", file, "-o", file], VERBOSE_G)


def parse_list(file):
    """Replace http with https and remove trailing /"""
    for line in fileinput.input(file, inplace=True):
        sys.stdout.write(str(line).replace("http://", "https://"))
    with open(file, "r+", encoding="utf-8") as open_file:
        lines = [ln.rstrip("\n").rstrip("/") + "\n" for ln in open_file]
        open_file.seek(0)
        open_file.truncate()
        open_file.writelines(lines)
    sort_txt_file(file)
