#!/usr/bin/env python3
import os
import csv
import json
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('Agg')  # Non-interactive backend
import numpy as np

# Read analysis data
with open('data/analysis.json', 'r') as f:
    data = json.load(f)

# Define language types
compiled = ['Assembly', 'C', 'C++', 'Rust', 'Go', 'Nim', 'Odin', 'Fortran', 'Swift', 'Crystal', 'D', 'Zig', 'Objective-C', 'Haskell']
jit = ['Java', 'CSharp', 'Kotlin', 'Scala', 'Dart', 'Julia']
interpreted = ['Python', 'JavaScript', 'TypeScript', 'Ruby', 'PHP', 'Perl', 'Lua', 'Bash', 'Brainfuck', 'Elixir', 'Erlang', 'R']

def get_type(lang):
    if lang in compiled:
        return 'Compiled'
    elif lang in jit:
        return 'JIT'
    else:
        return 'Interpreted'

def get_color(lang):
    lang_type = get_type(lang)
    if lang_type == 'Compiled':
        return '#2ecc71'  # Green
    elif lang_type == 'JIT':
        return '#3498db'  # Blue
    else:
        return '#e74c3c'  # Red

# Create visualizations for each decimal level
for decimals in [1, 2, 5, 10, 100, 1000, 2000]:
    print(f"Creating visualizations for {decimals} decimals...")
    
    # Sort by time
    sorted_langs = sorted(data.keys(), key=lambda x: data[x].get('time_ms', float('inf')))
    
    # Create figure with subplots
    fig, axes = plt.subplots(2, 2, figsize=(16, 12))
    fig.suptitle(f'Performance Comparison - {decimals} Decimal{"s" if decimals > 1 else ""}', fontsize=16, fontweight='bold')
    
    # Plot 1: Time comparison (top 20)
    ax1 = axes[0, 0]
    top_20 = sorted_langs[:20]
    times = [data[lang]['time_ms'] for lang in top_20]
    colors = [get_color(lang) for lang in top_20]
    
    bars1 = ax1.barh(range(len(top_20)), times, color=colors)
    ax1.set_yticks(range(len(top_20)))
    ax1.set_yticklabels(top_20)
    ax1.set_xlabel('Time (ms)')
    ax1.set_title('Execution Time (Top 20)')
    ax1.invert_yaxis()
    
    # Add value labels
    for i, (bar, time) in enumerate(zip(bars1, times)):
        ax1.text(time + 1, i, f'{time:.0f}ms', va='center', fontsize=8)
    
    # Plot 2: Memory comparison (top 20)
    ax2 = axes[0, 1]
    sorted_by_memory = sorted(data.keys(), key=lambda x: data[x].get('memory_bytes', float('inf')))
    top_20_mem = sorted_by_memory[:20]
    memories = [data[lang]['memory_bytes'] / (1024 * 1024) for lang in top_20_mem]  # Convert to MB
    colors_mem = [get_color(lang) for lang in top_20_mem]
    
    bars2 = ax2.barh(range(len(top_20_mem)), memories, color=colors_mem)
    ax2.set_yticks(range(len(top_20_mem)))
    ax2.set_yticklabels(top_20_mem)
    ax2.set_xlabel('Memory (MB)')
    ax2.set_title('Memory Usage (Top 20)')
    ax2.invert_yaxis()
    
    # Add value labels
    for i, (bar, mem) in enumerate(zip(bars2, memories)):
        ax2.text(mem + 0.1, i, f'{mem:.1f}MB', va='center', fontsize=8)
    
    # Plot 3: IPC comparison (top 20)
    ax3 = axes[1, 0]
    sorted_by_ipc = sorted(data.keys(), key=lambda x: data[x].get('ipc', 0), reverse=True)
    top_20_ipc = sorted_by_ipc[:20]
    ipcs = [data[lang]['ipc'] for lang in top_20_ipc]
    colors_ipc = [get_color(lang) for lang in top_20_ipc]
    
    bars3 = ax3.barh(range(len(top_20_ipc)), ipcs, color=colors_ipc)
    ax3.set_yticks(range(len(top_20_ipc)))
    ax3.set_yticklabels(top_20_ipc)
    ax3.set_xlabel('IPC (Instructions Per Cycle)')
    ax3.set_title('CPU Efficiency (Top 20)')
    ax3.invert_yaxis()
    
    # Add value labels
    for i, (bar, ipc) in enumerate(zip(bars3, ipcs)):
        ax3.text(ipc + 0.05, i, f'{ipc:.2f}', va='center', fontsize=8)
    
    # Plot 4: Time vs Memory scatter plot
    ax4 = axes[1, 1]
    for lang in data.keys():
        time = data[lang]['time_ms']
        memory = data[lang]['memory_bytes'] / (1024 * 1024)  # Convert to MB
        color = get_color(lang)
        ax4.scatter(time, memory, c=color, s=100, alpha=0.6, edgecolors='black', linewidths=0.5)
        
        # Add label for top performers
        if time < 30 or memory < 2:
            ax4.annotate(lang, (time, memory), fontsize=7, ha='center', va='bottom')
    
    ax4.set_xlabel('Time (ms)')
    ax4.set_ylabel('Memory (MB)')
    ax4.set_title('Time vs Memory Trade-off')
    ax4.grid(True, alpha=0.3)
    
    # Add legend
    from matplotlib.patches import Patch
    legend_elements = [
        Patch(facecolor='#2ecc71', label='Compiled'),
        Patch(facecolor='#3498db', label='JIT'),
        Patch(facecolor='#e74c3c', label='Interpreted')
    ]
    ax4.legend(handles=legend_elements, loc='upper right')
    
    plt.tight_layout()
    
    # Save figure
    output_file = f'reports/{decimals}_decimals.png'
    plt.savefig(output_file, dpi=150, bbox_inches='tight')
    plt.close()
    
    print(f"✓ Created {output_file}")

print("\n=== All visualizations created ===")