from pathlib import Path
import torch
from braindecode.datasets import BaseConcatDataset
from braindecode.preprocessing import preprocess, Preprocessor, create_windows_from_events
from braindecode.models import EEGNeX
from torch.utils.data import DataLoader
from sklearn.model_selection import train_test_split
from sklearn.utils import check_random_state
from typing import Optional
from torch.nn import Module
from torch.optim.lr_scheduler import LRScheduler
from tqdm import tqdm
import copy
from joblib import Parallel, delayed
from eegdash.dataset import EEGChallengeDataset
from eegdash.hbn.windows import (
    annotate_trials_with_target,
    add_aux_anchors,
    add_extras_columns,
    keep_only_recordings_with,
)
import os
from datetime import datetime
import csv
import numpy as np
import pickle


"""# Preprocess and Concatenate Data"""

release_list = ['R1', 'R2', 'R3', 'R4',
                'R5',
                'R6', 'R7', 'R8',
                'R9', 'R10', 'R11']


train_set = []
valid_set = []

DATA_DIR = Path("data")
DATA_DIR.mkdir(parents=True, exist_ok=True)

for release in tqdm(release_list):
    dataset_ccd = EEGChallengeDataset(
        task="contrastChangeDetection",
        release=release,
        cache_dir=DATA_DIR,
        mini=False
    )

    raws = Parallel(n_jobs=-1)(
        delayed(lambda d: d.raw)(d) for d in dataset_ccd.datasets
    )

    # %% Create windows of interest [code]
    EPOCH_LEN_S = 2.0
    SFREQ = 100  # by definition here

    transformation_offline = [
        Preprocessor(
            annotate_trials_with_target,
            target_field="rt_from_stimulus", epoch_length=EPOCH_LEN_S,
            require_stimulus=True, require_response=True,
            apply_on_array=False,
        ),
        Preprocessor(add_aux_anchors, apply_on_array=False),
    ]
    preprocess(dataset_ccd, transformation_offline, n_jobs=1)

    ANCHOR = "stimulus_anchor"
    SHIFT_AFTER_STIM = 0.5
    WINDOW_LEN = 2.0

    # Keep only recordings that actually contain stimulus anchors
    dataset = keep_only_recordings_with(ANCHOR, dataset_ccd)

    # Create single-interval windows (stim-locked, long enough to include the response)
    single_windows = create_windows_from_events(
        dataset,
        mapping={ANCHOR: 0},
        trial_start_offset_samples=int(SHIFT_AFTER_STIM * SFREQ),                 # +0.5 s
        trial_stop_offset_samples=int((SHIFT_AFTER_STIM + WINDOW_LEN) * SFREQ),   # +2.5 s
        window_size_samples=int(EPOCH_LEN_S * SFREQ),
        window_stride_samples=SFREQ,
        preload=True,
    )

    # Injecting metadata into the extra mne annotation.
    single_windows = add_extras_columns(
        single_windows,
        dataset,
        desc=ANCHOR,
        keys=("target", "rt_from_stimulus", "rt_from_trialstart",
            "stimulus_onset", "response_onset", "correct", "response_type")
    )

    meta_information = single_windows.get_metadata()

    # # Inspect meta information
    # from matplotlib.pylab import plt
    # fig, ax = plt.subplots(figsize=(15, 5))
    # ax = meta_information["target"].plot.hist(bins=30, ax=ax, color='lightblue')
    # ax.set_xlabel("Response Time (s)")
    # ax.set_ylabel("Frequency")
    # ax.set_title(f"Distribution of Response Times {release}")
    # plt.savefig(f"response_time_{release}_distribution.png")


    subjects = meta_information["subject"].unique()
    sub_rm = ["NDARWV769JM7", "NDARME789TD2", "NDARUA442ZVF", "NDARJP304NK1",
            "NDARTY128YLU", "NDARDW550GU6", "NDARLD243KRE", "NDARUJ292JXV", "NDARBA381JGH"]
    subjects = [s for s in subjects if s not in sub_rm]

    # Create train/valid for the windows
    subject_split = single_windows.split("subject")


    if release != 'R5':
        for s in subject_split:
            train_set.append(subject_split[s])
    else:
        for s in subject_split:
            valid_set.append(subject_split[s])


train_set = BaseConcatDataset(train_set)
valid_set = BaseConcatDataset(valid_set)


print("Number of examples in train/validation set")
print(f"Train:\t{len(train_set)}")
print(f"Valid:\t{len(valid_set)}")

def save_dataset_memmap(dataset, save_prefix):
    """
    Save a BaseConcatDataset into memory-mapped numpy arrays (portable).
    """
    n_samples = len(dataset)
    X0, y0, *_ = dataset[0]  # peek one sample to get shape & dtype
    x_shape = (n_samples,) + X0.shape
    y_shape = (n_samples,) if np.ndim(y0) == 0 else (n_samples,) + y0.shape

    print(f"[INFO] Saving {save_prefix} with {n_samples} samples...")
    print(f"[INFO] X shape: {x_shape}, dtype={X0.dtype}")
    print(f"[INFO] y shape: {y_shape}, dtype={y0.dtype}")

    X_mm = np.memmap(f"{save_prefix}_X.dat", dtype=X0.dtype, mode="w+", shape=x_shape)
    y_mm = np.memmap(f"{save_prefix}_y.dat", dtype=y0.dtype, mode="w+", shape=y_shape)

    for i in tqdm(range(n_samples)):
        Xi, yi, *_ = dataset[i]
        X_mm[i] = Xi
        y_mm[i] = yi

    # flush data to disk
    X_mm.flush()
    y_mm.flush()

    # store metadata to reconstruct later
    meta = {
        "X_shape": x_shape,
        "X_dtype": str(X0.dtype),
        "y_shape": y_shape,
        "y_dtype": str(y0.dtype),
    }
    with open(f"{save_prefix}_meta.pkl", "wb") as f:
        pickle.dump(meta, f)

    print(f"[DONE] Saved {save_prefix}_X.dat, {save_prefix}_y.dat and metadata.")

save_dataset_memmap(train_set, 'train_1_memmap')
save_dataset_memmap(valid_set, 'validation_1_memmap')
