import sys
import os
import re
import shutil
try:
    from BeautifulSoup import BeautifulSoup, formatter
except ImportError:
    from bs4 import BeautifulSoup, formatter as bs4formatter

PROJECT_NAMES = {
    "inero": "ИнЭро",
    "eroportal": "Эропортал",
}

def get_page_js_initialization_script(landing_tag):
    return \
"""window.addEventListener("DOMContentLoaded", function () {
      initPage('subscribe', 'vmXX')
      initPage('unsubscribe', 'vmXX')
      initPage('subscribeFailed', 'vmXX')
      initPage('subscribeSuccess', 'vmXX')
   })""".replace("vmXX", landing_tag)

def get_hidden_vars_html():
    return \
"""<div style="display: none">
    <p>SUBNAME</p>
    <p>SUBPRICE</p>
    <p>ACCOUNT</p>
    <p>PROVIDER</p>
    <a class="disable-btn" href="#<TARIF_NO_URL>">Закрыть</a>
    <a href="#<LK_LINK_URL>">Личный кабинет</a>
</div>"""

def get_footer_html(project_name):
    return \
f"""
    Нажатием на кнопку «Смотреть», Вы соглашаетесь с
    <a href="https://podpiskimf.ru/col/mds4/web/jsp/files/oferta.pdf">
        «Условиями мобильных подписок» и принимаете условия
    </a>
    <a href="https://videomir18.arboost.ru/user-agreement.pdf">
        «Пользовательского соглашения» сервиса «{PROJECT_NAMES[project_name]}».
    </a>
    Стоимость услуги: 35 руб. с учетом НДС за 1 день. Оплата производится со счета
    телефона. Услугу предоставляет контент-провайдер ООО "Арбуст".
"""

def check_args(affimob_path, project_name, landing_tag):
    if not project_name in PROJECT_NAMES:
        raise Exception(f"Неизвестное имя проекта. На данный момент поддерживаются только {', '.join(PROJECT_NAMES.keys())}.")

    if not os.path.isdir(affimob_path):
        raise Exception("Неверно указан путь к лендингу аффимоба")

    contents = os.listdir(affimob_path)
    if len(contents) == 0:
        raise Exception("Лендинг аффимоба пустой")

    if not os.path.isfile(f"{affimob_path}/index.html"):
        raise Exception("Не найден файл index.html")

    if not os.path.isfile(f"{affimob_path}/js/main.js"):
        raise Exception("Не найден файл main.js")

    if os.path.isdir(f"archive/{project_name}/{landing_tag}"):
        raise Exception("Такой лендинг в архиве уже существует")


def flatten_directory(root_dir):
    files_map = {}

    # Обходим дерево снизу вверх, чтобы потом проще удалить пустые папки
    for current_dir, dirs, files in os.walk(root_dir, topdown=False):
        if current_dir == root_dir:
            continue

        if files:
            files_map[current_dir[2:]] = files[:]

            for file in files:
                src = os.path.join(current_dir, file)
                dst = os.path.join(root_dir, file)

                if os.path.exists(dst):
                    base, ext = os.path.splitext(file)
                    i = 1
                    while os.path.exists(dst):
                        dst = os.path.join(root_dir, f"{base}_{i}{ext}")
                        i += 1

                shutil.move(src, dst)

        if not os.listdir(current_dir):
            os.rmdir(current_dir)
    return files_map

# BS поганит спецтеги #<SPECIAL_TAG>, заменяя < и > на &lt; и &gt;
# Надо вернуть их обратно
def replace_placeholders(html_content):
    matches = re.findall(r'"#&lt;([_A-Z]+)&gt;"', html_content)
    for m in matches:
        html_content = html_content.replace(f'"#&lt;{m}&gt;"', f'"#<{m}>"')
    return html_content

def copy_files(affimob_path, project_name, landing_tag):
    landing_path = f"archive/{project_name}/{landing_tag}"

    shutil.copytree(affimob_path, landing_path)
    os.chdir(landing_path)
    child_files_dict = flatten_directory(".")

    os.chdir("../../../")
    shutil.copyfile("archive/page.js", f"{landing_path}/page.js")
    os.chdir(landing_path)

    return child_files_dict

def edit_files(project_name, landing_tag, child_files_dict):
    print("Searching for CSS files")
    css_files = list(filter(lambda x: x.endswith('.css'), [item for sublist in list(child_files_dict.values()) for item in sublist]))
    print("CSS files found: ", ", ".join(css_files))
    links_found = False
    for css_file in css_files:
        print(f"checking {css_file}")
        with open(css_file, 'r', encoding='utf-8') as f:
            css_lines = f.read().splitlines()
            for index, line in enumerate(css_lines):
                for directory, filenames in child_files_dict.items():
                    for filename in filenames:
                        if f"{directory}/{filename}" in line:
                            print(f"wrong link found in line {index}")
                            links_found = True

                            pattern = rf'url\(["\'].*?{re.escape(directory)}/{re.escape(filename)}["\']\)'
                            replacement = f'url("{filename}")'
                            new_line = re.sub(pattern, replacement, line)
                            print(f"replacing {line.strip()} with {new_line.strip()}")
                            css_lines[index] = new_line
        with open(css_file, "w", encoding='utf-8') as file:
            file.write("\n".join(css_lines))

    if links_found:
        print("Successful")
    else:
        print("No wrong links found. Please check manually. Maybe there are none")

    print("Fixing footer text in main.js")
    some_substring_not_found = False
    with open("main.js", 'r', encoding='utf-8') as f:
        js_lines = f.read().splitlines()
        var_init_found = False
        var_usage_found = False
        for index, line in enumerate(js_lines):
            if "const footerTextAgree" in line:
                js_lines[index] = ""
                var_init_found = True
            if "footerTextAgree.innerHTML" in line:
                js_lines[index] = line.replace("footerTextAgree.innerHTML", "const abc")
                var_usage_found = True
        if not var_init_found or not var_usage_found:
            some_substring_not_found = True
        if not some_substring_not_found:
            print("Successful")
        else:
            print("Changes in main.js are not completed successfully. Please check manually")

        with open("main.js", "w", encoding='utf-8') as file:
            file.write("\n".join(js_lines))

    print("Checking for wrong links index.html")
    links_found = False
    with open("index.html", 'r', encoding='utf-8') as f:
        html_content = f.read()
        html_lines = html_content.splitlines()
        for index, line in enumerate(html_lines):
            for directory, filenames in child_files_dict.items():
                for filename in filenames:
                    wrong_links = [f"./{directory}/{filename}", f"{directory}/{filename}"]
                    for wrong_link in wrong_links:
                        if wrong_link in line:
                            print(f"wrong link found in line {index}")
                            links_found = True
                            new_line = line.replace(wrong_link, filename)
                            print(f"replacing {line.strip()} with {new_line.strip()}")
                            html_lines[index] = new_line
                            line = new_line
    if links_found:
        print("Successful")
    else:
        print("No wrong links found. Please check manually")

    print("Updating service price in index.html")
    subprice_found = False
    for index, line in enumerate(html_lines):
        if "SUBPRICE" in line:
            print(f"SUBPRICE found in line {index}")
            new_line = line.replace("SUBPRICE", "35 руб.")
            print(f"replacing {line.strip()} with {new_line.strip()}")
            subprice_found = True
            html_lines[index] = new_line
    if subprice_found:
        print("Successful")
    else:
        print("No SUBPRICE tag found. Please check manually")

    html_content = "\n".join(html_lines)

    soup = BeautifulSoup(html_content, "html.parser")
    print("Detecting trash imports index.html")
    trash_found = False
    scripts = soup.find_all("script")
    for script in scripts:
        if "src" in script.attrs:
            if "http://" in script.attrs["src"] or "https://" in script.attrs["src"] or "cdn." in script.attrs["src"]:
                print(f"Removing trash import: {script.attrs['src']}")
                trash_found = True
                script.decompose()
    if trash_found:
        print("Successful")
    else:
        print("No trash links found. Please check manually")

    print("add close class to body in index.html")
    page_js_tag = soup.new_tag("script")
    page_js_tag["src"] = "page.js"
    soup.body.append(page_js_tag)
    page_js_initialization_tag = soup.new_tag("script")
    page_js_initialization_tag.string = get_page_js_initialization_script(landing_tag)
    soup.body.append(page_js_initialization_tag)
    print("Successful")

    print("Inserting page.js and initialization script into index.html")
    body_classes = soup.body.get("class", [])
    body_classes.append("close")
    soup.body["class"] = body_classes
    print("Successful")

    print("Updating footer text in index.html")
    footer = soup.find("footer", attrs={"class": "footer__agree"})
    inner_footer = footer.find("p", attrs={"class": "footer__text"})
    footer_content = BeautifulSoup(get_footer_html(project_name), "html.parser")
    if not inner_footer:
        print("Warning! Footer not found, please check manually")
    else:
        inner_footer.clear()
        inner_footer.append(footer_content)
        print("Successful")

    print("check if TARIF_YES_URL is present in index.html")
    if "TARIF_YES_URL" not in html_content:
        print("TARIF_YES_URL not found, trying to add")
        button = soup.find("button", attrs={"class": "buy"})
        if not button:
            print("buy button not found, please add TARIF_YES_URL manually")
        else:
            if not button.parent.name == "a":
                print("adding link wrapper to buy button")
                link_tag = soup.new_tag("a")
                link_tag["href"] = "#<TARIF_YES_URL>"
                link_tag["style"] = "display:contents"
                button.wrap(link_tag)
    print("Successful")


    print("Adding hidden vars block to index.html")
    hidden_vars_content = BeautifulSoup(get_hidden_vars_html(), "html.parser")
    soup.body.append(hidden_vars_content)
    print("Successful")

    print("Adding subscription button id in index.html")
    subscription_button = soup.find("button", attrs={"class": "confirm"})
    if not subscription_button:
        print("Warning! Button not found, please check manually")
    else:
        subscription_button.attrs["id"] = "agree_btn"
        print("Successful")

    with open("index.html", "w", encoding='utf-8') as file:
        html_content = soup.prettify()
        html_content = replace_placeholders(html_content)
        file.write(html_content)


def convert_affimob_landing(affimob_path, project_name, landing_tag):
    print("affimob path: ", affimob_path)
    print("project name: ", project_name)
    print("landing tag: ", landing_tag)
    check_args(affimob_path, project_name, landing_tag)
    child_files_dict = copy_files(affimob_path, project_name, landing_tag)

    edit_files(project_name, landing_tag, child_files_dict)


if __name__ == "__main__":
    affimob_path = sys.argv[1]
    project_name = sys.argv[2]
    landing_tag = sys.argv[3]
    convert_affimob_landing(affimob_path, project_name, landing_tag)