import os
import time
import requests

current_dir = os.path.dirname(os.path.abspath(__file__))
weshop_native_dir = os.path.join(current_dir, "..")
resources_dir = os.path.join(weshop_native_dir, "resources")
package_dir = os.path.join(weshop_native_dir, "package")
bin_dir = os.path.join(weshop_native_dir, "bin")
log_dir = os.path.join(weshop_native_dir, "logs")
err_dir = os.path.join(resources_dir, "err")
python_exe = os.path.join(bin_dir, "python","python.exe")
userData_dir = os.path.join(weshop_native_dir, "userData")
gpu_pid_file = os.path.join(weshop_native_dir, "bin", "gpu_service.txt")
electron_status_file = os.path.join(weshop_native_dir, "logs", "status.log")
lfs_lora_cover_dir = os.path.join(userData_dir, "Data", "lora_cover")

def file_monitor():
    lora_dir = os.path.join(weshop_native_dir,"models", "Lora")
    initial_files = set([file for file in os.listdir(lora_dir) if os.path.splitext(file)[1]  in [".png", ".jpg", ".jpeg"]])
    initial_lfs_files = set([file for file in os.listdir(lfs_lora_cover_dir) if os.path.splitext(file)[1]  in [".png", ".jpg", ".jpeg"]])
    initial_files = initial_files - initial_lfs_files
    lfs_url = 'http://localhost:60020/lora_cover/'  # Specify the appropriate upload endpoint URL
    for file_path in initial_files:
        with open(os.path.join(lora_dir,file_path), 'rb') as file:
            response = requests.post(lfs_url, files={'file': file})
    initial_files = set([file for file in os.listdir(lora_dir) if os.path.splitext(file)[1]  in [".png", ".jpg", ".jpeg"]])

    print("start monitoring")
    while True:
        time.sleep(1)
         # 从远端获取封面的逻辑
        lora_files = set([file for file in os.listdir(lora_dir) if os.path.splitext(file)[-1] in [".safetensors"]])
        for lora_file in lora_files:
            download_image(f"https://weshop-preproc-ai-sh-1322216870.cos.ap-shanghai.myqcloud.com/public/model_image/{lora_file}_1.png", os.path.join(lora_dir, lora_file.replace(".safetensors", ".png")))

        current_files = set([file for file in os.listdir(lora_dir) if os.path.splitext(file)[1]  in [".png", ".jpg", ".jpeg"]])
        uploaded_files = current_files - initial_files
        for file_path in uploaded_files:
            with open(os.path.join(lora_dir,file_path), 'rb') as file:
                print("start to upload a file to file server")
                response = requests.post(lfs_url, files={'file': file})
                print("upload done")

        initial_files = initial_files.union(uploaded_files)
        print("current cycle done")

def download_image(url, local_path):
    # 检查本地是否存在同名图片
    if os.path.exists(local_path):
        # 图片已存在，直接返回
        return
    # 检查远端图片是否存在
    response = requests.head(url)
    if response.status_code == 200:
        # 图片存在，下载到本地
        with open(local_path, 'wb') as f:
            response = requests.get(url, stream=True)
            f.write(response.content)
    else:
        # 图片不存在，不下载
        pass

if __name__ == "__main__":
    file_monitor()