from jsonpath_ng.ext import parse
import json

# 파일 경로 설정
project_json_path = './sample/제2410회 코딩활용능력 2급 B형 정답/project.json'
scoring_json_path = './scoring.json'

# JSON 파일 읽기
def read_json(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        return json.load(file)

# 요소 탐색 함수
def find_element(project_data, jsonpath_expr):
    jsonpath_expr = parse(jsonpath_expr)
    return [match.value for match in jsonpath_expr.find(project_data)]

# 요소 탐색 함수
def find_script_element(project_data, jsonpath_expr):
    jsonpath_expr = parse(jsonpath_expr)
    match = jsonpath_expr.find(project_data)
    
    return match[0].value

    


# main 함수
def main():
    project_data = read_json(project_json_path)
    scoring_data = read_json(scoring_json_path)

    total_points = 0
    
    for key, value in scoring_data.items():
        ele = value.get('ele')
        type = value.get('type')
        blocks = value.get('blocks')        
        
        if type == "scene":
            exists = find_element(project_data, ele)
            if exists:
                # print(f"No elements found for {ele}")
                total_points += value.get('points')
            # else:
            #     print(f"Element '{ele}' exists in project.json: {exists}")
        
        if type == "script":            
            exists = find_script_element(project_data, ele)        
            temp = json.loads(exists)
            for block in blocks:
                block_exists = find_element(temp, block.get('ele'))
                
                # 블록에 따라 params 값이나 statements 값이 있는 경우 처리 추가 필요
                
                if block_exists:
                    total_points += block.get('points')
                # else:
                #     print(f"No elements found for {block.get('ele')}")
    
    print(f"Total Points: {total_points}")
            

if __name__ == "__main__":
    main()