"""
        Изменяет цветовую тему приложения и иконку

"color_theme": {
  "enabled": true,
  "colors": {
    "primary": "#ccff00",
    "secondary": "#ffffd700",
    "background": "#ffffff",
    "text": "#000000"
  },
  "gradient": {
    "angle": "135.0",
    "from": "#ffff6060",
    "to": "#ffccff00"
  }
}
"""

priority = 0

# imports
from lxml import etree
from utils.public import (
    insert_after_public,
    insert_after_color,
    change_color,
)


# Patch
def apply(config: dict) -> bool:
    main_color = config["colors"]["primary"]
    splash_color = config["colors"]["secondary"]
    gradient_angle = config["gradient"]["angle"]
    gradient_from = config["gradient"]["from"]
    gradient_to = config["gradient"]["to"]

    # No connection alert coolor
    with open("./decompiled/assets/no_connection.html", "r", encoding="utf-8") as file:
        file_contents = file.read()

    new_contents = file_contents.replace("#f04e4e", main_color)

    with open("./decompiled/assets/no_connection.html", "w", encoding="utf-8") as file:
        file.write(new_contents)

    # For logo
    drawable_types = ["", "-night"]

    for drawable_type in drawable_types:
        # Application logo gradient colors
        file_path = f"./decompiled/res/drawable{drawable_type}/$logo__0.xml"

        parser = etree.XMLParser(remove_blank_text=True)
        tree = etree.parse(file_path, parser)
        root = tree.getroot()

        # Change attributes with namespace
        root.set(f"{{{config['xml_ns']['android']}}}angle", gradient_angle)
        root.set(f"{{{config['xml_ns']['android']}}}startColor", gradient_from)
        root.set(f"{{{config['xml_ns']['android']}}}endColor", gradient_to)

        # Save back
        tree.write(file_path, pretty_print=True, xml_declaration=True, encoding="utf-8")

        # Application logo anim color
        file_path = f"./decompiled/res/drawable{drawable_type}/$logo_splash_anim__0.xml"

        parser = etree.XMLParser(remove_blank_text=True)
        tree = etree.parse(file_path, parser)
        root = tree.getroot()

        # Finding "path"
        for el in root.findall("path", namespaces=config["xml_ns"]):
            name = el.get(f"{{{config['xml_ns']['android']}}}name")
            if name == "path":
                el.set(f"{{{config['xml_ns']['android']}}}fillColor", splash_color)

        # Save back
        tree.write(file_path, pretty_print=True, xml_declaration=True, encoding="utf-8")

    for filename in ["$ic_launcher_foreground__0", "$ic_banner_foreground__0"]:
        file_path = f"./decompiled/res/drawable-v24/{filename}.xml"

        parser = etree.XMLParser(remove_blank_text=True)
        tree = etree.parse(file_path, parser)
        root = tree.getroot()

        # Change attributes with namespace
        root.set(f"{{{config['xml_ns']['android']}}}angle", gradient_angle)
        items = root.findall("item", namespaces=config['xml_ns'])
        assert len(items) == 2
        items[0].set(f"{{{config['xml_ns']['android']}}}color", gradient_from)
        items[1].set(f"{{{config['xml_ns']['android']}}}color", gradient_to)

        # Save back
        tree.write(file_path, pretty_print=True, xml_declaration=True, encoding="utf-8")

    insert_after_public("carmine", "custom_color")
    insert_after_public("carmine_alpha_10", "custom_color_alpha_10")
    insert_after_color("carmine", "custom_color", main_color[0]+'ff'+main_color[1:])
    insert_after_color("carmine_alpha_10", "custom_color_alpha_10", main_color[0]+'1a'+main_color[1:])

    change_color("accent_alpha_10", main_color[0]+'1a'+main_color[1:])
    change_color("accent_alpha_20", main_color[0]+'33'+main_color[1:])
    change_color("accent_alpha_50", main_color[0]+'80'+main_color[1:])
    change_color("accent_alpha_70", main_color[0]+'b3'+main_color[1:])

    change_color("colorAccent", main_color[0]+'ff'+main_color[1:])
    change_color("link_color", main_color[0]+'ff'+main_color[1:])
    change_color("link_color_alpha_70", main_color[0]+'b3'+main_color[1:])
    change_color("refresh_progress", main_color[0]+'ff'+main_color[1:])

    change_color("ic_launcher_background", "#ff000000")
    change_color("bottom_nav_indicator_active", "#ffffffff")
    change_color("bottom_nav_indicator_icon_checked", main_color[0]+'ff'+main_color[1:])
    change_color("bottom_nav_indicator_label_checked", main_color[0]+'ff'+main_color[1:])

    return True
