#!/usr/bin/env python3

import os
import yaml


"""
документ = набор диктов

набор диктов = текстовые элементы + список

список может содержать текстовые элементы или дикт
"""

def deep_deep(group: dict, all_elements: bool = True):
    """Рекурсивный обход структуры yaml

    Args:
        group (dict): _description_
        all_elements (bool, optional): _description_. Defaults to True.

    Returns:
        _type_: _description_
    """

    packages = []
    packages_x86_64_new = []

    if isinstance(group, dict):

        for item in group:

            if all_elements:
                pass

            else:
                if 'selected' in group:
                    if group['selected'] == True:
                        pass

                    else:
                        continue

            if isinstance(group[item], list):
                if 'name' in group:
                    packages_x86_64_new.append(f"\n## {group['name']}")

                if 'packages' in group:
                    for package in sorted(group['packages']):
                        package = package.replace("$LOCALE", LOCALE)
                        packages.append(package)
                        packages_x86_64_new.append(package)

                for element in group[item]:
                    if isinstance(element, dict):
                        res = deep_deep(element, all_elements)
                        packages += res[0]
                        packages_x86_64_new += res[1]

    return packages, packages_x86_64_new


def deep(doc: dict, all_elements: bool = True):
    """Функция проверки объекта что он словарь и запуск рекурсии для получения данных

    Args:
        doc (dict): _description_
        all_elements (bool, optional): _description_. Defaults to True.

    Returns:
        _type_: _description_
    """

    packages = []
    packages_x86_64_new = []

    for group in doc:
        if isinstance(group, dict):
            res = deep_deep(group, all_elements)
            packages += res[0]
            packages_x86_64_new += res[1]

    return packages, packages_x86_64_new


def get_packages(filename):
    """Функция для packages.x86_64

    Args:
        filename (_type_): _description_

    Returns:
        _type_: _description_
    """

    with open(filename, mode="rt") as f:
        lines = f.readlines()

        packages = []

        for line in lines:
            if line.startswith("#") or len(line) <= 1:
                continue
            else:
                package = line.strip().replace("\n", "")
                packages.append(package)

        packages = set(packages)

        packages = sorted(packages)

        return packages


def get_packages_v2(filename):
    """Функция для netinstall.yaml

    Args:
        filename (_type_): _description_

    Returns:
        _type_: _description_
    """
    with open(filename, 'rt') as f:
        doc = yaml.safe_load(f)

        res = deep(doc, all_elements = False)
        packages = res[0]
        packages_x86_64_new = res[1]

        packages = set(packages)

        packages = sorted(packages)

        packages_v2 = []
        for package in packages:
            packages_v2.append(package)

        packages = packages_v2

        return packages, packages_x86_64_new


def write_new_packages_x86_64(filename, packages_x86_64_new: list):

    with open(filename, mode="wt+") as f:

        if isinstance(packages_x86_64_new, list):
            packages_x86_64_new[0] = packages_x86_64_new[0].replace("\n#", "")
            for item in packages_x86_64_new:
                if "## ARCHISO PACKAGES" in item:
                    item = f"\n{item}"
                f.write(f"{item}\n")

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

LOCALE = os.environ.get('LANG')[0:2]
filename1='packages.x86_64'
filename2='netinstall.yaml'
filename3='packages.x86_64'


list_of_packages = get_packages(filename1)
length_list_of_packages = len(list_of_packages)

result_netinstall = get_packages_v2(filename2)

list_of_packages_v2 = result_netinstall[0]
length_list_of_packages_v2 = len(list_of_packages_v2)

list_of_packages_v3 = result_netinstall[1]


packages_extend = """
## ARCHISO PACKAGES
archiso
clonezilla
ddrescue
gpart
grsync
melawy-calamares-qt5
memtest86+
mkinitcpio-archiso
mkinitcpio-nfs-utils
os-prober
partclone
parted
partimage
rate-mirrors
syslinux
"""

packages_extend = packages_extend.splitlines()

list_of_packages_v3.extend(packages_extend)
list_of_packages_v3_1 = []

for item in list_of_packages_v3:
    if len(item) <= 1:
        continue
    else:
        list_of_packages_v3_1.append(item)

list_of_packages_v3 = list_of_packages_v3_1


write_new_packages_x86_64(filename3, list_of_packages_v3)


set_list_of_packages_v3 = set()

for item in list_of_packages_v3:
    if item.startswith("\n#") or item.startswith("#") or len(item) <= 1:
        continue
    else:
        set_list_of_packages_v3.add(item)

length_list_of_packages_v3 = len(set_list_of_packages_v3)


difference1 = sorted(list(set(list_of_packages_v2).difference(list_of_packages)))

difference2 = sorted(list(set(list_of_packages).difference(list_of_packages_v2)))

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

print('-------------------------------------------')
print("Разница в списках")
print('-------------------------------------------')
print(f"Количество пакетов в {filename1}: {length_list_of_packages}")

print(f"Количество пакетов в {filename2}: {length_list_of_packages_v2}")

print(f"Количество пакетов сгенерированных для {filename1}: {length_list_of_packages_v3}")

print('-------------------------------------------')
print(f"Пакеты, отсутствующие в {filename1}:")
print('-------------------------------------------')

for item in difference1:
    print(item)

print('-------------------------------------------')
print(f"Пакеты, отсутствующие в {filename2}:")
print('-------------------------------------------')

for item in difference2:
    print(item)

print('-------------------------------------------')
