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, suffix=None):
    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
    if suffix != None:
        return f"{suffix}_{uuid.uuid4().hex}_{now}{ext}"
    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)
 

