import re
import os
import html

os.chdir(os.path.dirname(__file__))

class Warning:
    name: str
    description: str
    enabled: bool

    def __init__(self, name, description, enabled = True):
        self.name = name
        self.description = description
        self.enabled = enabled

    def __str__(self):
        return f'<tr><td>W{self.name}</td><td>{self.description}</td><td>{self.enabled}</td></tr>\n'

class Errors:
    name: str
    description: str

    def __init__(self, name, description):
        self.name = name
        self.description = description

    def __str__(self):
        return f'<tr><td>E{self.name}</td><td>{self.description}</td></tr>\n'


with open('asar/errors.cpp', 'r') as f:
    errors_text = f.readlines()

with open('asar/warnings.cpp', 'r') as f:
    warnings_text = f.readlines()

warnings: list[Warning] = []
errors: list[Errors] = []

warning_lines = []
error_lines = []
for line in warnings_text:
    if 'WRN(' in line and not line.startswith('#define'):
        warning_lines.append(line.strip())
for line in errors_text:
    if 'ERR(' in line and not line.startswith('#define'):
        error_lines.append(line.strip())

pre_text = """
<!DOCTYPE html>
<!-- generated by generate_manual_tables.py -->
<html lang="en">
  <head>
    <title>Asar User Manual - #TITLE#</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" href="../shared/highlight_js_asar/styles/default.css"/>
    <script src="../shared/highlight_js/highlight.min.js"></script>
    <script src="../shared/highlight_js_asar/highlight_js_asar.js"></script>
    <script src="../shared/common.js"></script>
  </head>
  <body>
    <table>
"""
pre_error_text = """
      <tr>
        <th>Error name</th>
        <th>Error message</th>
      </tr>
"""
pre_warning_text = """
      <tr>
        <th>Warning name</th>
        <th>Warning message</th>
        <th>Enabled by default</th>
      </tr>
"""
post_text = """
    </table>
  </body>
</html>
"""

warning_pattern = re.compile(r'WRN\((.*?)\),\s*"(.*)"\s*,?\s*(true|false)?\s*}')
error_pattern = re.compile(r'ERR\((.*?)\),\s*"(.*)"\s*}')
for warning in warning_lines:
    name, description, enabled = re.findall(warning_pattern, warning)[0]
    description = html.escape(description.replace(r'\"', '"'))
    warnings.append(Warning(name, description, enabled != 'false'))

for error in error_lines:
    name, description = re.findall(error_pattern, error)[0]
    description = html.escape(description.replace(r'\"', '"'))
    errors.append(Errors(name, description))

with open('../docs/manual/errors-list.html', 'w') as f:
    f.write(pre_text.replace('#TITLE#', "Error list"))
    f.write(pre_error_text)
    for error in errors:
        f.write(str(error))
    f.write(post_text)

with open('../docs/manual/warnings-list.html', 'w') as f:
    f.write(pre_text.replace('#TITLE#', "Warning list"))
    f.write(pre_warning_text)
    for warning in warnings:
        f.write(str(warning))
    f.write(post_text)
