import html
import os
import re
import time
from pathlib import Path
import markdown
from PIL import Image, ImageOps
from modules.utils import get_available_chat_styles
from modules import shared
# This is to store the paths to the thumbnails of the profile pictures
image_cache = {}
with open(Path(__file__).resolve().parent / '../css/html_readable_style.css', 'r') as f:
    readable_css = f.read()
with open(Path(__file__).resolve().parent / '../css/html_4chan_style.css', 'r') as css_f:
    _4chan_css = css_f.read()
with open(Path(__file__).resolve().parent / '../css/html_instruct_style.css', 'r') as f:
    instruct_css = f.read()
# Custom chat styles
chat_styles = {}
for k in get_available_chat_styles():
    chat_styles[k] = open(Path(f'css/chat_style-{k}.css'), 'r').read()
# Handle styles that derive from other styles
for k in chat_styles:
    lines = chat_styles[k].split('\n')
    input_string = lines[0]
    match = re.search(r'chat_style-([a-z\-]*)\.css', input_string)
    if match:
        style = match.group(1)
        chat_styles[k] = chat_styles.get(style, '') + '\n\n' + '\n'.join(lines[1:])
def fix_newlines(string):
    string = string.replace('\n', '\n\n')
    string = re.sub(r"\n{3,}", "\n\n", string)
    string = string.strip()
    return string
def replace_blockquote(m):
    return m.group().replace('\n', '\n> ').replace('\\begin{blockquote}', '').replace('\\end{blockquote}', '')
def convert_to_markdown(string):
    # Blockquote
    string = re.sub(r'(^|[\n])>', r'\1>', string)
    pattern = re.compile(r'\\begin{blockquote}(.*?)\\end{blockquote}', re.DOTALL)
    string = pattern.sub(replace_blockquote, string)
    # Code
    string = string.replace('\\begin{code}', '```')
    string = string.replace('\\end{code}', '```')
    string = re.sub(r"(.)```", r"\1\n```", string)
    result = ''
    is_code = False
    for line in string.split('\n'):
        if line.lstrip(' ').startswith('```'):
            is_code = not is_code
        result += line
        if is_code or line.startswith('|'):  # Don't add an extra \n for tables or code
            result += '\n'
        else:
            result += '\n\n'
    result = result.strip()
    if is_code:
        result += '\n```'  # Unfinished code block
    # Unfinished list, like "\n1.". A |delete| string is added and then
    # removed to force a 
 or  to be generated instead of a .
    if re.search(r'(\n\d+\.?|\n\*\s*)$', result):
        delete_str = '|delete|'
        if re.search(r'(\d+\.?)$', result) and not result.endswith('.'):
            result += '.'
        result = re.sub(r'(\n\d+\.?|\n\*\s*)$', r'\g<1> ' + delete_str, result)
        html_output = markdown.markdown(result, extensions=['fenced_code', 'tables'])
        pos = html_output.rfind(delete_str)
        if pos > -1:
            html_output = html_output[:pos] + html_output[pos + len(delete_str):]
    else:
        html_output = markdown.markdown(result, extensions=['fenced_code', 'tables'])
    # Unescape code blocks
    pattern = re.compile(r']*>(.*?)', re.DOTALL)
    html_output = pattern.sub(lambda x: html.unescape(x.group()), html_output)
    return html_output
def generate_basic_html(string):
    string = convert_to_markdown(string)
    string = f'
{string}
'
    return string
def process_post(post, c):
    t = post.split('\n')
    number = t[0].split(' ')[1]
    if len(t) > 1:
        src = '\n'.join(t[1:])
    else:
        src = ''
    src = re.sub('>', '>', src)
    src = re.sub('(>>[0-9]*)', '\\1', src)
    src = re.sub('\n', '
\n', src)
    src = f'{src}\n'
    src = f'Anonymous  No.{number}\n{src}'
    return src
def generate_4chan_html(f):
    posts = []
    post = ''
    c = -2
    for line in f.splitlines():
        line += "\n"
        if line == '-----\n':
            continue
        elif line.startswith('--- '):
            c += 1
            if post != '':
                src = process_post(post, c)
                posts.append(src)
            post = line
        else:
            post += line
    if post != '':
        src = process_post(post, c)
        posts.append(src)
    for i in range(len(posts)):
        if i == 0:
            posts[i] = f'{posts[i]}
\n'
        else:
            posts[i] = f'{posts[i]}
\n'
    output = ''
    output += f''
    for post in posts:
        output += post
    output += '
|))', r'\1', output[i])
        output[i] = re.sub(r'^(>(.*?)(
|))', r'\1', output[i])
    output = '\n'.join(output)
    return output
def make_thumbnail(image):
    image = image.resize((350, round(image.size[1] / image.size[0] * 350)), Image.Resampling.LANCZOS)
    if image.size[1] > 470:
        image = ImageOps.fit(image, (350, 470), Image.LANCZOS)
    return image
def get_image_cache(path):
    cache_folder = Path(shared.args.disk_cache_dir)
    if not cache_folder.exists():
        cache_folder.mkdir()
    mtime = os.stat(path).st_mtime
    if (path in image_cache and mtime != image_cache[path][0]) or (path not in image_cache):
        img = make_thumbnail(Image.open(path))
        old_p = Path(f'{cache_folder}/{path.name}_cache.png')
        p = Path(f'{cache_folder}/cache_{path.name}.png')
        if old_p.exists():
            old_p.rename(p)
        output_file = p
        img.convert('RGB').save(output_file, format='PNG')
        image_cache[path] = [mtime, output_file.as_posix()]
    return image_cache[path][1]
def generate_instruct_html(history):
    output = f''
    for i, _row in enumerate(history):
        row = [convert_to_markdown(entry) for entry in _row]
        if row[0]:  # don't display empty user messages
            output += f"""
                  
                """
        output += f"""
              
            """
    output += "
'
    # We use ?character and ?time.time() to force the browser to reset caches
    img_bot = f'

' if Path("cache/pfp_character_thumb.png").exists() else ''
    img_me = f'
 if reset_cache else )
' if Path("cache/pfp_me.png").exists() else ''
    for i, _row in enumerate(history):
        row = [convert_to_markdown(entry) for entry in _row]
        if row[0]:  # don't display empty user messages
            output += f"""
                  
                """
        output += f"""
              
            """
    output += "
'
    for i, _row in enumerate(history):
        row = [convert_to_markdown(entry) for entry in _row]
        if row[0]:  # don't display empty user messages
            output += f"""
              
            """
        output += f"""
          
        """
    output += "