from unidecode import unidecode
import os
import re
import uuid
import datetime 

import logging
logger = logging.getLogger(__file__)

from multiprocessing import Pool
from functools import partial
from bs4 import BeautifulSoup
import re

STATIC_FILES = 'static/subtitles/'


def filename_to_regex(filename):
    filename = unidecode(filename)
    filename = re.sub(r'[^0-9a-zA-Z._-]+', '', filename)
    return "^[0-9a-zA-Z._-]+" + filename + "$"

def generate_name(filename):
    filechar,ext = os.path.splitext(filename)
    # # rewrite file name and remove any unwanted character
    # filechar = filechar.replace(".", "").replace(" ", "_").replace("(", "_").replace(")", "_").replace("&", "AND")
    # add date and time to video name to avoide conflect by name
    now = (str(datetime.datetime.now()).split('.')[0].replace(' ','_').replace(':','_').replace('-',"_"))
    # return new filename
    return f"{uuid.uuid4().hex}_{now}{ext}"

def handle_uploaded_file(file, path,filename):
    # generate file name
    # with open(path+"/"+filename, 'wb+') as destination:
    with open(path+str(filename), 'wb+') as destination:
        # for chunk in file.chunks():
        #     destination.write(chunk)

        #with open(image_path, 'wb+') as destination:
        if type(file) == str:
            destination.write(file.encode())   # or whatever you actually want to do if it's a string
        else:
            for chunk in file.chunks():
                destination.write(chunk)
 

def remove_duplicate_lines(html_content):
    lines = html_content.split('\n')
    deduplicated_lines = []
    for i in range(len(lines)):
        if i > 0 and lines[i] == lines[i - 1]:
            continue
        deduplicated_lines.append(lines[i])
    deduplicated_html = '\n'.join(deduplicated_lines)
    return deduplicated_html

def list_to_vtt(lst, file_path):
    """Convert a list of subtitle strings to a Webvtt (.vtt) file.

    Args:
        subtitles (list): A list of subtitle strings in the format
            "00:00:00.000 subtitle text".
        file_path (str): The file path to write the SubRip file to.
    """
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write('WEBVTT\n\n')
        f.write('')
        for i, item in enumerate(lst):
            start_time = item[:13] 
            f.write(f'{i}\n{start_time.replace(" .", ".")} --> {lst[(i+1)][:13].replace(" .", ".") if (i+1)<len(lst) else start_time.split()[0].replace(" .", ".")}')
            f.write(f'\n{item[14:]}\n\n')

def list_to_srt(lst, file_path):
    """Convert a list of subtitle strings to a SubRip (.srt) file.

    Args:
        subtitles (list): A list of subtitle strings in the format
            "00:00:00.000 subtitle text".
        file_path (str): The file path to write the SubRip file to.
    """
    with open(file_path, 'w', encoding='utf-8') as f:
        for i, item in enumerate(lst):
            start_time = item[:13].replace(" .", ",")
            next_start_time = lst[i+1][:13].replace(" .", ",") if i+1 < len(lst) else start_time.split()[0]
            f.write(f'{i}\n{start_time} --> {next_start_time}\n{item[14:]}\n\n')

def process_caption(request, key, caption):
    """
    Process a single caption file, converting it to both SRT and VTT formats.
    """
    srt_path = os.path.join(
        STATIC_FILES, "srt", f"{os.path.splitext(os.path.basename(caption))[0]}.srt"
    )
    vtt_path = os.path.join(
        STATIC_FILES, "vtt", f"{os.path.splitext(os.path.basename(caption))[0]}.{key}.vtt"
    )
    html_file = request.data.get(f"file_srt_{key}")
    
    # Add condition To Check if object HTML is Not None or == ""
    if html_file is not None and html_file != "":
        # Then Remove Deplucate From HTML Object
        remove_duplicate_lines(html_file)
        # Then Start Using BeautifulSoup To Transform The Html To A String List
        soup = BeautifulSoup(html_file, 'html.parser')
        p_tags = soup.find_all('p')
        text_list = [p.text.strip() for p in p_tags]
        # Convert it To srt
        list_to_srt(text_list, srt_path) 
        # Convert it To vtt
        list_to_vtt(text_list, vtt_path)
    else:
        print("HTML file is None.") # add appropriate error handling if required 

def download_captions(instance, request):
    """
    Download all caption files for a video instance in parallel.
    """
    srt_files = {
        'fr': str(instance.path_srt_fr) if instance.path_srt_fr else None,
        'es': str(instance.path_srt_es) if instance.path_srt_es else None,
        'en': str(instance.path_srt_en) if instance.path_srt_en else None,
        'po': str(instance.path_srt_po) if instance.path_srt_po else None,
    }
    
    for key, caption in srt_files.items():
        process_caption(request, key, caption)
    # with Pool() as pool:
    #     # Use partial to pass instance and request arguments to process_caption
    #     func = partial(process_caption, request)
    #     # Iterate over SRT_FILES keys and values in parallel using map
    #     pool.starmap(func, srt_files.items())

# def cap_download(instance, request):
#     STATIC_FILES = 'static/subtitles/'
#     SRT_FILES = {
#         'fr': str(instance.path_srt_fr) if instance.path_srt_fr else None,
#         'es': str(instance.path_srt_es) if instance.path_srt_es else None,
#         'en': str(instance.path_srt_en) if instance.path_srt_en else None,
#         'po': str(instance.path_srt_po) if instance.path_srt_po else None,
#     }
    
#     for key, caption in SRT_FILES.items():
#         # 
#         srt_path = os.path.join(
#             STATIC_FILES, "srt_1", f"{os.path.splitext(os.path.basename(caption))[0]}.srt"
#         )
#         # 
#         vtt_path = os.path.join(
#             STATIC_FILES, "vtt_1", f"{os.path.splitext(os.path.basename(caption))[0]}.{key}.vtt"
#         )
#         # 
#         html_file = request.data.get(f"file_srt_{key}")   
#         # 
#         soup = BeautifulSoup(html_file, 'html.parser')
#         p_tags = soup.find_all('p')
#         # 
#         text_list = []
#         for p in p_tags:
#             text_list.append(p.text.strip())
#         # 
#         list_to_srt(text_list, srt_path) 
#         list_to_vtt(text_list, vtt_path)  