import os
from pathlib import Path

from custom_apps.FEATURE_VECTOR_SIMILARITY_FAISS.const import *

def search_glass_parts(image_path_list):
    result = []
    
    for image_path in image_path_list:
        
        if image_path is None:
            result.append(None)
            continue
        elif not os.path.exists(image_path):
            result.append(None)
            continue
        
        parts_path = Path(os.path.join(os.path.dirname(os.path.dirname(image_path)),ImageDepths.parts))
        parts_files_generator = parts_path.rglob('*.png')

        parts_list = []
        # parts_list = [str(p.resolve()) for p in parts_files_generator] # 풀경로
        
        for p in parts_files_generator:
            # name,ext = os.path.splitext(p.name)
            parts_list.append(p.name)
        result.append(parts_list)
    
    return result

def file_name_to_parts(file_path):
    """
    파일경로로 파츠이름 파싱

    :param file_path: 파일경로
    :return: 파츠이름
    """
    filename, ext = os.path.splitext(os.path.basename(file_path))
    
    name_list = filename.split('_')
    
    result = name_list[2:][0]
        
    return result

if __name__ == '__main__':
    print(file_name_to_parts(os.path.join(FAISS_VECTOR_PATH,"Glass_001",ImageDepths.parts,"Glass_001_Temple_L.png")))