import numpy as np
from typing import List, Tuple
from tqdm import tqdm
import os
import jax.numpy as jnp
from utils import generate_random_grid, pad_grid, apply_transformation

from v0_programs import *


def generate_dataset(B: int, N: int, grid_sizes: List[int], colors: int) -> Tuple[np.ndarray, np.ndarray]:
    """
    Generate a dataset of transformations using varying grid sizes.

    Args:
        B: Batch size
        N: Number of samples (input-output pairs) per batch
        grid_sizes: List of possible grid sizes
        colors: Number of colors in the grids

    Returns:
        data: Numpy array of shape (B, N, 30, 30, 2)
        grid_shapes: Numpy array of shape (B, N, 2, 2)  # [input_shape, output_shape]
    """
    data = np.zeros((B, N, 30, 30, 2), dtype=np.int32)
    grid_shapes = np.zeros((B, N, 2, 2), dtype=np.int32)

    for b in tqdm(range(B), desc="Generating batches"):
        idx = np.random.randint(len(TRANSFORMATIONS))
        transformation = TRANSFORMATIONS[idx]

        for n in range(N):
            idx = np.random.randint(len(grid_sizes))
            grid_size = grid_sizes[idx]

            input_grid = generate_random_grid(grid_size, colors)
            output_grid = apply_transformation(input_grid, transformation)

            data[b, n, :, :, 0] = pad_grid(input_grid, (30, 30))
            data[b, n, :, :, 1] = pad_grid(output_grid, (30, 30))
            grid_shapes[b, n] = np.array(
                [[input_grid.shape[0], output_grid.shape[0]], [input_grid.shape[1], output_grid.shape[1]]]
            )

    return data, grid_shapes


if __name__ == "__main__":

    # This creates a dataset gird sizes drawn uniformly from a set of specified grid sizes
    # of arc style tasks with transformations from from v_0 programs

    # List of all transformation functions to use in generation
    TRANSFORMATIONS = [rotate_90, rotate_180, rotate_270]

    # Creating the directory for dataset version 0
    os.makedirs("src/datasets/v1_train", exist_ok=True)
    os.makedirs("src/datasets/v1_test", exist_ok=True)

    # Set the parameters for the dataset
    B_train = 100000  # Number of training examples
    B_test = 1000  # Number of testing examples
    N = 3  # Number of samples per batch, set to 1 for individual examples
    grid_sizes = list(range(2, 31))
    colors = 10
    seed = 42

    print("Starting dataset generation...")

    # Seed seed to control randomness
    np.random.seed(seed)
    # Generate the training dataset
    dataset_train, grid_shapes_train = generate_dataset(B_train, N, grid_sizes, colors)
    # Generate the testing dataset
    dataset_test, grid_shapes_test = generate_dataset(B_test, N, grid_sizes, colors)

    # Save the datasets
    np.save(f"src/datasets/v1_train/grids.npy", dataset_train.astype(jnp.uint8))
    np.save(f"src/datasets/v1_train/shapes.npy", grid_shapes_train.astype(jnp.uint8))
    np.save(f"src/datasets/v1_test/grids.npy", dataset_test.astype(jnp.uint8))
    np.save(f"src/datasets/v1_test/shapes.npy", grid_shapes_test.astype(jnp.uint8))

    print("Dataset and grid shapes saved to 'dataset_v1' folder.")
    print("Train dataset of shape:", dataset_train.shape)
    print("Train shapes of shape:", grid_shapes_train.shape)
    print("Test dataset of shape:", dataset_test.shape)
    print("Test shapes of shape:", grid_shapes_test.shape)
