#!/usr/bin/env python3
"""
GameplayEffect Blueprint 분석
"""

import json
import sys
from pathlib import Path

def extract_ge_name(ge_path):
    """GE 경로에서 이름 추출"""
    return ge_path.split('/')[-1].split('.')[0]

def analyze_ge_blueprint(ge_asset):
    """GE Blueprint에서 중요 정보 추출"""
    asset_name = ge_asset.get('AssetName', '')

    # Variables 섹션 찾기
    variables = []
    if 'Variables' in ge_asset:
        for var in ge_asset['Variables']:
            var_name = var.get('Name', '')
            var_value = var.get('Value', '')
            var_type = var.get('Type', '')
            variables.append({
                'name': var_name,
                'value': var_value,
                'type': var_type
            })

    # EventGraph 섹션에서 로직 확인
    event_graphs = []
    if 'EventGraphs' in ge_asset:
        for graph in ge_asset['EventGraphs']:
            graph_name = graph.get('Name', '')
            event_graphs.append(graph_name)

    return {
        'name': asset_name,
        'variables': variables,
        'event_graphs': event_graphs
    }

def main():
    if len(sys.argv) < 3:
        print("사용법: python analyze_ge_blueprints.py <Blueprint.json> <ultimate_ge_list.json>")
        sys.exit(1)

    bp_path = Path(sys.argv[1])
    ge_list_path = Path(sys.argv[2])

    # GE 목록 로드
    with open(ge_list_path, 'r', encoding='utf-8') as f:
        ge_data = json.load(f)

    target_ge_classes = ge_data['all_ge_classes']
    target_ge_names = [extract_ge_name(ge) for ge in target_ge_classes]

    print(f"Blueprint 로딩 중: {bp_path} (24MB, 시간 소요)")
    with open(bp_path, 'r', encoding='utf-8') as f:
        bp_data = json.load(f)

    assets = bp_data.get('Assets', [])
    print(f"총 {len(assets)}개 Blueprint Assets 로드 완료")

    print("\n" + "=" * 100)
    print("GameplayEffect Blueprint 상세 분석")
    print("=" * 100)

    results = {}

    for target_name in target_ge_names:
        # Blueprint에서 GE 찾기
        ge_asset = next((a for a in assets if a.get('AssetName', '') == target_name), None)

        if not ge_asset:
            print(f"\n【{target_name}】")
            print(f"  → Blueprint에서 찾을 수 없음")
            continue

        analysis = analyze_ge_blueprint(ge_asset)
        results[target_name] = analysis

        print(f"\n【{target_name}】")

        if analysis['variables']:
            print(f"  Variables: {len(analysis['variables'])}개")
            for var in analysis['variables']:
                print(f"    - {var['name']} ({var['type']}): {var['value']}")
        else:
            print(f"  Variables: 없음")

        if analysis['event_graphs']:
            print(f"  EventGraphs: {', '.join(analysis['event_graphs'])}")

    # 결과 저장
    output_file = ge_list_path.parent / "ge_blueprint_analysis.json"
    with open(output_file, 'w', encoding='utf-8') as f:
        json.dump(results, f, ensure_ascii=False, indent=2)

    print(f"\n\n분석 결과 저장: {output_file}")

if __name__ == "__main__":
    main()
