# -*- coding: utf-8 -*-
import requests
import json
import os
import glob
import time
from typing import Tuple
import os
from openai import OpenAI
import re
import base64

"""
总： 时间 概念 解释

1. 去重 & 删掉噪声单词
2. 概念标号，重新添加格式： ID,时间戳,概念,解释

======== 图像对齐（服务器）=====
* 对每一个文件进行遍历，因为每一个txt文件，对应几张照片
* 如果文件中的概念在最后的概念列表中，加入到对齐列表中
* 便利文件中的每一个图，以base64和prompt的形式传入gpt4o进行对齐
* 建立互索引，将图像和概念进行互索引，保存分别到以追加方式保存在json文件中
* 注意，每对齐一次写一次，图片的命名为images+数字，概念的命名为json中的index，为concept+数字


"""
concept = []
#count_global = 3890
def imageAnalysis(image_path: str, subject: str, concepts: list ,model="gpt-4o-2024-11-20") -> str:
    prompt = f"""
        You are an AI  {subject} image analyst. Follow this strict protocol:
        1. Candidate Verification:
           Within the {subject} concepts {concepts},
           identify and extract the concepts that appear in the image or are highly related to it. 
           Use these extracted concepts to form the C4 .
        2. Multilevel Analysis:
        a) OCR Extraction: Capture ALL {subject} pure concept  in  → C1
        b) Regional Analysis: Identify specific  {subject} components → C2
        c) Holistic Analysis: Derive tissue/system-level {subject} concepts → C3
        3. Deduplication Protocol:
        Priority: C4 > C3 > C2 > C1
        - Remove duplicates ONLY for identical terms
        - Preserve hierarchical terms (e.g., keep both "上皮组织" and "小肠上皮细胞")
        4. Output Decision:
        - If any valid terms remain: List ALL in bullet format
        - If completely empty: Output "none"
        Mandatory Format:
        - concept1
        - concept2
        ...

        OR
        "none"
        Absolute constraints:
        1. No term suppression except exact duplicates
        2. No explanations, categories, or formatting variations.
    """
    with open(image_path, "rb") as image_file:
        base64_image = base64.b64encode(image_file.read()).decode("utf-8")   
    url = "https://api.chatfire.cn/v1/chat/completions"
    payload = json.dumps({
    "model": model,
    "messages": [
        {
                "role": "user",
                "content": [
                    {"type": "text", "text": f"{prompt}"},
                    {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{base64_image}"}},
                ],
            }
    ]
    })
    headers = {
        'Accept': 'application/json',
        'Authorization': 'Bearer sk-ygAo2alTCCo1i0CHXe6fPd2xlyKouDKxlui2PGDGo7d150pA',
        'Content-Type': 'application/json'
    }
    while True:
        try:
            response = requests.request("POST", url, headers=headers, data=payload)
            response_json = response.json()
            return response_json["choices"][0]["message"]["content"]
        except:
            print("error")
            print(response)
            time.sleep(65)
            continue

def mainImages(file_path: str, jsonFile:str, subject: str) -> None:
    # 以追加和读取模式打开文件
    imageC = {}
    with open(f"imageConceptIndex_{subject}", "a+", encoding="utf-8") as files:
        files.seek(0)  # 移动到文件开头以读取内容
        content = files.read()
        #cnt = 4492
        
        # obtain all txt
        txt_files = glob.glob(os.path.join(file_path, '**', '*Intergrationner'), recursive=True)
        for txt_file in txt_files:
            print("*"*10)
            print(txt_file)
            print("*"*10)
            concepts_list = []
            data = {}
            with open(txt_file, 'r', encoding='utf-8') as file:
                for line in file:
                    match = re.match(r'(\d{2}:\d{2}-\d{2}:\d{2})\s+(.+)', line.strip())
                    if match:
                        time_range, concepts = match.groups()
                        for con in concepts.split():
                            if con not in concepts_list:
                                concepts_list.append(con) 

            #==========================================================      
            folder_path = os.path.dirname(txt_file)
            allfiles = os.listdir(folder_path)
            png_files = []
            for ele in allfiles:
                if "png" in ele and "0-00000" in ele.strip('.'):
                    png_files.append(folder_path + "/" + ele)
            #png_files = glob.glob(os.path.join(folder_path,"*png"))

            add_concepts = []
            for png in png_files:
                print(png,cnt)
                imageC[str(cnt)+".png"] = []
                time.sleep(2)
                result1  = imageAnalysis(png, subject, concepts_list, model="gpt-4o-2024-11-20")
                time.sleep(3)
                result2 = imageAnalysis(png, subject, concepts_list, model="gpt-4o-2024-11-20")
                time.sleep(3)
                result3 = imageAnalysis(png, subject, concepts_list, model="gpt-4o-2024-11-20")
                result1 = re.findall(r"-\s*(\w+)", result1)
                result2 = re.findall(r"-\s*(\w+)", result2)
                result3 = re.findall(r"-\s*(\w+)", result3)
                result = set(result1) | set(result2) | set(result3)
                print("*"*10)
                print(f" png {png}   ok  cnt{cnt}\n")
                print("*"*10)
                
                if result == "none":     
                    continue   
                else:
                    #result =  re.findall(r"-\s*(\w+)", result)
                    for r in result:
                        try:
                            index = concept.index(r)
                            imageC[str(cnt)+".png"].append(index)
                        except:
                            concept.append(r)
                            add_concepts.append(r)
                            imageC[str(cnt)+".png"].append(len(concept)-1)
                directory = os.path.dirname(png)
                new_path = os.path.join(directory, str(cnt)+".png")
                os.rename(png, new_path)
                files.write(f"{str(cnt)}.png ")
                for i in imageC[str(cnt)+".png"]:
                    files.write(str(i)+" ")
                files.write("\n")
                
                
                cnt += 1

                                           
def readConcepts(conceptPath: str) -> None:
    """
    读取概念文件，返回概念列表
    """
    global concept
    with open(conceptPath, "r", encoding="utf-8") as f:
        # 读取已有的数据
        existing_data = json.load(f)
    for item in existing_data:
        concept.append(item['concept'])
    f.close()
    
if __name__ == "__main__":
    subject = "biology"
    fileList = [r"/home/bnukde/DISK/tonglu/TEAM-KG/Alignment/data/Course/Biology/bx2",r"/home/bnukde/DISK/tonglu/TEAM-KG/Alignment/data/Course/Biology/xuanxiu1",r"/home/bnukde/DISK/tonglu/TEAM-KG/Alignment/data/Course/Biology/xuanxiu2",r"/home/bnukde/DISK/tonglu/TEAM-KG/Alignment/data/Course/Biology/xuanxiu3"]
    jsonFile = r"/home/bnukde/DISK/tonglu/TEAM-KG/Alignment/Tools/explanationBiology.json"
    # 读取概念文件
    readConcepts(jsonFile)
    # 图像对齐
    for ele in fileList:
        mainImages(ele, jsonFile, subject)