"""
Fast HAC dataloader for offline-decoded tensors.

This loader intentionally reproduces the expensive parts of HACDOMAIN/MMAction only where needed:
  - re-sample temporal frame indices every __getitem__ for train
  - apply train/val spatial transforms at runtime
  - normalize and FormatShape to NCTHW
  - derive the audio crop from the same sampled frame indices, then apply train-time audio augmentation

Expected offline .npz keys from preprocess_offline_rewrite.py:
  video:         uint8 RGB frames after Resize(short_side=256), shape (F,H,W,3)
  flow_x/flow_y: uint8 optical-flow streams after Resize(short_side=256), shape (F,H,W)
  audio_samples: float32 mono samples
  samplerate:    int
  label:         int
"""

from __future__ import annotations

import csv
import math
import os
import random
from pathlib import Path
from typing import Iterable, Optional

import numpy as np
import torch
import torch.nn.functional as F
from scipy import signal
from torch.utils.data import Dataset


RGB_MEAN = torch.tensor([123.675, 116.28, 103.53], dtype=torch.float32).view(1, 3, 1, 1)
RGB_STD = torch.tensor([58.395, 57.12, 57.375], dtype=torch.float32).view(1, 3, 1, 1)
FLOW_MEAN = torch.tensor([128.0, 128.0], dtype=torch.float32).view(1, 2, 1, 1)
FLOW_STD = torch.tensor([128.0, 128.0], dtype=torch.float32).view(1, 2, 1, 1)


def _safe_int(value) -> int:
    if isinstance(value, np.ndarray):
        return int(value.item())
    return int(value)


def _resize_nchw(frames: torch.Tensor, size: tuple[int, int]) -> torch.Tensor:
    try:
        return F.interpolate(frames, size=size, mode="bilinear", align_corners=False, antialias=True)
    except TypeError:
        return F.interpolate(frames, size=size, mode="bilinear", align_corners=False)


def _random_resized_crop_params(
    height: int,
    width: int,
    scale: tuple[float, float] = (0.08, 1.0),
    ratio: tuple[float, float] = (3.0 / 4.0, 4.0 / 3.0),
) -> tuple[int, int, int, int]:
    """Torchvision-compatible RandomResizedCrop parameter sampling."""
    area = height * width
    log_ratio = (math.log(ratio[0]), math.log(ratio[1]))

    for _ in range(10):
        target_area = area * random.uniform(scale[0], scale[1])
        aspect_ratio = math.exp(random.uniform(log_ratio[0], log_ratio[1]))

        crop_w = int(round(math.sqrt(target_area * aspect_ratio)))
        crop_h = int(round(math.sqrt(target_area / aspect_ratio)))

        if 0 < crop_w <= width and 0 < crop_h <= height:
            top = random.randint(0, height - crop_h)
            left = random.randint(0, width - crop_w)
            return top, left, crop_h, crop_w

    # Fallback used by torchvision when no random crop satisfies the constraints.
    in_ratio = width / height
    if in_ratio < ratio[0]:
        crop_w = width
        crop_h = int(round(crop_w / ratio[0]))
    elif in_ratio > ratio[1]:
        crop_h = height
        crop_w = int(round(crop_h * ratio[1]))
    else:
        crop_w = width
        crop_h = height
    top = (height - crop_h) // 2
    left = (width - crop_w) // 2
    return top, left, crop_h, crop_w


def _center_crop_params(height: int, width: int, crop_size: int) -> tuple[int, int, int, int]:
    if height < crop_size or width < crop_size:
        raise ValueError(f"CenterCrop({crop_size}) requires H,W >= crop_size, got H={height}, W={width}")
    top = int(round((height - crop_size) / 2.0))
    left = int(round((width - crop_size) / 2.0))
    return top, left, crop_size, crop_size


def _sample_frames(
    num_available_frames: int,
    clip_len: int,
    frame_interval: int,
    num_clips: int,
    test_mode: bool,
) -> np.ndarray:
    """
    MMAction-style SampleFrames approximation for the configs used here.

    Important: original HACDOMAIN passes total_frames=end_frame-start_frame = frame_num-1.
    To stay close to that behavior, caller should pass max(1, actual_frame_count - 1).
    """
    total_frames = max(1, int(num_available_frames))
    ori_clip_len = clip_len * frame_interval

    if test_mode:
        if total_frames > ori_clip_len - 1:
            avg_interval = (total_frames - ori_clip_len + 1) / float(num_clips)
            base_offsets = np.arange(num_clips) * avg_interval
            clip_offsets = (base_offsets + avg_interval / 2.0).astype(np.int64)
        else:
            clip_offsets = np.zeros((num_clips,), dtype=np.int64)
    else:
        avg_interval = (total_frames - ori_clip_len + 1) // num_clips
        if avg_interval > 0:
            base_offsets = np.arange(num_clips) * avg_interval
            clip_offsets = base_offsets + np.random.randint(avg_interval, size=num_clips)
        elif total_frames > max(num_clips, ori_clip_len):
            clip_offsets = np.sort(np.random.randint(total_frames - ori_clip_len + 1, size=num_clips))
        elif avg_interval == 0:
            ratio = (total_frames - ori_clip_len + 1.0) / num_clips
            clip_offsets = np.around(np.arange(num_clips) * ratio).astype(np.int64)
        else:
            clip_offsets = np.zeros((num_clips,), dtype=np.int64)

    frame_inds = clip_offsets[:, None] + np.arange(clip_len, dtype=np.int64)[None, :] * frame_interval
    frame_inds = np.mod(frame_inds, total_frames)
    return frame_inds.astype(np.int64)


def _to_rgb_tchw(video_np: np.ndarray) -> torch.Tensor:
    if video_np.ndim == 3:
        video_np = np.repeat(video_np[..., None], 3, axis=-1)
    if video_np.ndim != 4:
        raise ValueError(f"Expected RGB video THWC, got shape={video_np.shape}")
    if video_np.shape[-1] == 4:
        video_np = video_np[..., :3]
    if video_np.shape[-1] == 1:
        video_np = np.repeat(video_np, 3, axis=-1)
    return torch.from_numpy(np.ascontiguousarray(video_np)).permute(0, 3, 1, 2).float()


def _to_gray_thw(flow_np: np.ndarray) -> torch.Tensor:
    if flow_np.ndim == 4:
        flow_np = flow_np[..., 0]
    if flow_np.ndim != 3:
        raise ValueError(f"Expected flow THW/THWC, got shape={flow_np.shape}")
    return torch.from_numpy(np.ascontiguousarray(flow_np)).float()


def _gather_rgb(video_np: np.ndarray, frame_inds: np.ndarray) -> torch.Tensor:
    flat_inds = frame_inds.reshape(-1)
    frames = video_np[flat_inds]
    return _to_rgb_tchw(frames)  # N*T,C,H,W


def _gather_flow(flow_x_np: np.ndarray, flow_y_np: np.ndarray, frame_inds: np.ndarray) -> torch.Tensor:
    flat_inds = frame_inds.reshape(-1)
    x = _to_gray_thw(flow_x_np[flat_inds])
    y = _to_gray_thw(flow_y_np[flat_inds])
    return torch.stack([x, y], dim=1)  # N*T,2,H,W


def _apply_spatial_and_format(
    frames: torch.Tensor,
    frame_inds: np.ndarray,
    train: bool,
    is_flow: bool,
) -> torch.Tensor:
    """Apply MMAction train/val spatial transform and return (num_clips,C,T,H,W)."""
    if frames.ndim != 4:
        raise ValueError(f"Expected frames as N*T,C,H,W, got shape={tuple(frames.shape)}")

    _, channels, height, width = frames.shape
    if train:
        top, left, crop_h, crop_w = _random_resized_crop_params(height, width)
        out_size = 224
        do_flip = random.random() < 0.5
        frames = frames[:, :, top:top + crop_h, left:left + crop_w]
        frames = _resize_nchw(frames, size=(out_size, out_size))
    else:
        out_size = 256 if is_flow else 224
        top, left, crop_h, crop_w = _center_crop_params(height, width, out_size)
        do_flip = False
        frames = frames[:, :, top:top + crop_h, left:left + crop_w]

    if do_flip:
        frames = torch.flip(frames, dims=(-1,))
        # MMAction handles Flow specially: horizontal flip reverses x-flow direction.
        # Flow pixels are stored around 128, so invert x-channel before normalization.
        if is_flow:
            frames[:, 0, :, :] = 255.0 - frames[:, 0, :, :]

    if is_flow:
        if channels != 2:
            raise ValueError(f"Expected 2-channel flow, got C={channels}")
        frames = (frames - FLOW_MEAN) / FLOW_STD
    else:
        if channels != 3:
            raise ValueError(f"Expected 3-channel RGB, got C={channels}")
        frames = (frames - RGB_MEAN) / RGB_STD

    num_clips, clip_len = frame_inds.shape
    frames = frames.view(num_clips, clip_len, channels, out_size, out_size)
    frames = frames.permute(0, 2, 1, 3, 4).contiguous()  # N,C,T,H,W
    return frames


def get_spectrogram_piece_from_samples(
    samples: np.ndarray,
    start_time: float,
    end_time: float,
    duration: float,
    samplerate: int,
    training: bool = False,
) -> np.ndarray:
    """Same audio logic as dataloader_HAC.get_spectrogram_piece, but from cached samples."""
    if samples.ndim > 1:
        samples = samples.mean(axis=1)

    if duration <= 0:
        duration = max(len(samples) / float(samplerate), 1e-6)

    start = int(np.round(start_time / duration * len(samples)))
    end = int(np.round(end_time / duration * len(samples)))
    start = max(0, min(start, len(samples)))
    end = max(start, min(end, len(samples)))
    piece = samples[start:end]

    resamples = piece[:160000]
    if len(resamples) == 0:
        resamples = np.zeros((160000,), dtype=np.float32)
    while len(resamples) < 160000:
        resamples = np.tile(resamples, 10)[:160000]

    resamples = np.asarray(resamples, dtype=np.float32)
    resamples[resamples > 1.0] = 1.0
    resamples[resamples < -1.0] = -1.0

    _, _, spectrogram = signal.spectrogram(resamples, samplerate, nperseg=512, noverlap=353)
    spectrogram = np.log(spectrogram + 1e-7)
    spectrogram = (spectrogram - np.mean(spectrogram)) / (np.std(spectrogram) + 1e-9)

    if training:
        interval = 9
        spectrogram = spectrogram + np.random.uniform(-0.05, 0.05, spectrogram.shape)
        if spectrogram.shape[0] > interval:
            # Original code uses np.random.choice(256 - interval); keep the same when possible.
            high = min(256 - interval, spectrogram.shape[0] - interval)
            start_mask = np.random.choice(high, (1,))[0] if high > 0 else 0
            spectrogram[start_mask:start_mask + interval, :] = 0

    return spectrogram.astype(np.float32, copy=False)


class FastHAC(Dataset):
    def __init__(
        self,
        split: str = "test",
        source: bool = True,
        domain: Iterable[str] = ("human",),
        modality: str = "rgb",
        cfg=None,
        cfg_flow=None,
        use_video: bool = True,
        use_flow: bool = True,
        use_audio: bool = True,
        datapath: str = "/path/to/HAC/",
        tensor_dir: Optional[str] = None,
    ):
        self.base_path = Path(datapath)
        self.tensor_dir = Path(tensor_dir) if tensor_dir is not None else self.base_path / "processed_tensors"
        self.video_list: list[str] = []
        self.label_list: list[int] = []
        self.prefix_list: list[str] = []
        self.use_video = use_video
        self.use_audio = use_audio
        self.use_flow = use_flow
        self.train = split == "train"
        self.pipeline_mode = "train" if self.train else "val"

        # Kept for call-site compatibility; transforms are hard-coded from the supplied configs.
        self.cfg = cfg
        self.cfg_flow = cfg_flow
        self.modality = modality

        for dom in domain:
            split_file = self.base_path / "HAC_Splits" / f"HAC_{split}_only_{dom}.csv"
            with split_file.open(newline="") as f:
                for row in csv.reader(f):
                    if not row:
                        continue
                    self.video_list.append(row[0])
                    self.label_list.append(int(row[1]))
                    self.prefix_list.append(dom)

            if split == "test" and not source:
                train_file = self.base_path / "HAC_Splits" / f"HAC_train_only_{dom}.csv"
                with train_file.open(newline="") as f:
                    for row in csv.reader(f):
                        if not row:
                            continue
                        self.video_list.append(row[0])
                        self.label_list.append(int(row[1]))
                        self.prefix_list.append(dom)

    def _tensor_path(self, index: int) -> Path:
        return self.tensor_dir / f"{self.pipeline_mode}_{self.prefix_list[index]}_{self.video_list[index]}.npz"

    def _sample_video_indices(self, num_frames: int) -> np.ndarray:
        # RGB config: SampleFrames(clip_len=32, frame_interval=2, num_clips=1)
        return _sample_frames(
            num_available_frames=max(1, num_frames - 1),
            clip_len=32,
            frame_interval=2,
            num_clips=1,
            test_mode=not self.train,
        )

    def _sample_flow_indices(self, num_frames: int) -> np.ndarray:
        # Flow config: SampleFrames(clip_len=8, frame_interval=4, num_clips=1)
        return _sample_frames(
            num_available_frames=max(1, num_frames - 1),
            clip_len=8,
            frame_interval=4,
            num_clips=1,
            test_mode=not self.train,
        )

    def _load_video(self, npz) -> tuple[dict[str, torch.Tensor], np.ndarray]:
        # New offline format stores full resized video. Legacy fallback accepts your old key "clip".
        video_np = npz["video"] if "video" in npz.files else npz["clip"]
        frame_inds = self._sample_video_indices(video_np.shape[0])
        frames = _gather_rgb(video_np, frame_inds)
        imgs = _apply_spatial_and_format(frames, frame_inds, train=self.train, is_flow=False)
        return {"imgs": imgs}, frame_inds

    def _load_flow(self, npz) -> tuple[dict[str, torch.Tensor], np.ndarray]:
        if "flow_x" in npz.files and "flow_y" in npz.files:
            flow_x_np = npz["flow_x"]
            flow_y_np = npz["flow_y"]
            frame_inds = self._sample_flow_indices(min(flow_x_np.shape[0], flow_y_np.shape[0]))
            frames = _gather_flow(flow_x_np, flow_y_np, frame_inds)
        else:
            # Legacy fallback for old files where flow was already sampled/interleaved.
            flow_np = npz["flow"]
            if flow_np.ndim == 4 and flow_np.shape[-1] in (1, 3):
                flow_np = flow_np[..., 0]
            frame_inds = self._sample_flow_indices(flow_np.shape[0] // 2 if flow_np.shape[0] >= 2 else flow_np.shape[0])
            flat = flow_np[frame_inds.reshape(-1)]
            if flat.ndim == 4:
                flat = flat[..., 0]
            # Best-effort legacy path: pair adjacent frames as x/y.
            if flat.shape[0] % 2 != 0:
                flat = flat[:-1]
            frames = torch.from_numpy(np.ascontiguousarray(flat)).float().view(-1, 2, flat.shape[-2], flat.shape[-1])
            frame_inds = frame_inds[:, :frames.shape[0] // frame_inds.shape[0]]

        imgs = _apply_spatial_and_format(frames, frame_inds, train=self.train, is_flow=True)
        return {"imgs": imgs}, frame_inds

    def _load_audio(self, npz, primary_frame_inds: Optional[np.ndarray]) -> torch.Tensor:
        if "audio_samples" in npz.files and "samplerate" in npz.files:
            samples = npz["audio_samples"]
            samplerate = _safe_int(npz["samplerate"])
            duration = len(samples) / float(samplerate) if samplerate > 0 else 0.0

            if primary_frame_inds is not None:
                flat = primary_frame_inds.reshape(-1)
                start_time = float(flat[0]) / 24.0
                end_time = float(flat[-1]) / 24.0
            else:
                start_time = 0.0
                end_time = duration

            spec = get_spectrogram_piece_from_samples(
                samples=samples,
                start_time=start_time,
                end_time=end_time,
                duration=duration,
                samplerate=samplerate,
                training=self.train,
            )
            return torch.from_numpy(spec).float()

        # Legacy fallback: old offline file stored an already-windowed spectrogram.
        spec = torch.from_numpy(npz["spectrogram"].copy()).float()
        if self.train:
            spec = spec + torch.empty_like(spec).uniform_(-0.05, 0.05)
            interval = 9
            if spec.shape[0] > interval:
                high = min(256 - interval, spec.shape[0] - interval)
                start = random.randrange(high) if high > 0 else 0
                spec[start:start + interval, :] = 0
        return spec

    def __getitem__(self, index: int):
        tensor_path = self._tensor_path(index)
        if not tensor_path.exists():
            raise FileNotFoundError(
                f"Missing preprocessed tensor: {tensor_path}. "
                "Run preprocess_offline_rewrite.py first, or check split/source/domain naming."
            )

        primary_frame_inds: Optional[np.ndarray] = None
        with np.load(tensor_path, allow_pickle=False) as npz:
            if self.use_video:
                clip, video_frame_inds = self._load_video(npz)
                primary_frame_inds = video_frame_inds
            else:
                clip = 0

            if self.use_flow:
                flow, flow_frame_inds = self._load_flow(npz)
                if primary_frame_inds is None:
                    primary_frame_inds = flow_frame_inds
            else:
                flow = 0

            if self.use_audio:
                spectrogram = self._load_audio(npz, primary_frame_inds)
            else:
                spectrogram = 0

            label = _safe_int(npz["label"]) if "label" in npz.files else int(self.label_list[index])

        return clip, flow, spectrogram, label

    def __len__(self) -> int:
        return len(self.video_list)
