"""Image loading, processing, and statistics logic."""

from __future__ import annotations

import colorsys
from pathlib import Path
from typing import Iterable, Sequence, Tuple

from tkinter import filedialog, messagebox

from PIL import Image, ImageDraw, ImageTk

from .constants import IMAGES_DIR, PREVIEW_MAX_SIZE, SUPPORTED_IMAGE_EXTENSIONS


class ImageProcessingMixin:
    """Handles all image related operations."""

    image_path: Path | None
    orig_img: Image.Image | None
    preview_img: Image.Image | None
    preview_tk: ImageTk.PhotoImage | None
    overlay_tk: ImageTk.PhotoImage | None

    image_paths: list[Path]
    current_image_index: int

    def load_image(self) -> None:
        default_dir = IMAGES_DIR if IMAGES_DIR.exists() else Path.cwd()
        path = filedialog.askopenfilename(
            title="Bild wählen",
            filetypes=[("Images", "*.webp *.png *.jpg *.jpeg *.bmp")],
            initialdir=str(default_dir),
        )
        if not path:
            return
        self._set_image_collection([Path(path)], 0)

    def load_folder(self) -> None:
        default_dir = IMAGES_DIR if IMAGES_DIR.exists() else Path.cwd()
        directory = filedialog.askdirectory(
            title="Ordner mit Bildern wählen",
            initialdir=str(default_dir),
        )
        if not directory:
            return
        folder = Path(directory)
        if not folder.exists():
            messagebox.showerror("Fehler", "Der Ordner wurde nicht gefunden.")
            return
        image_files = sorted(
            (
                path
                for path in folder.iterdir()
                if path.suffix.lower() in SUPPORTED_IMAGE_EXTENSIONS and path.is_file()
            ),
            key=lambda item: item.name.lower(),
        )
        if not image_files:
            messagebox.showinfo("Info", "Keine unterstützten Bilder im Ordner gefunden.")
            return
        self._set_image_collection(image_files, 0)

    def show_next_image(self, event=None) -> None:
        if not getattr(self, "image_paths", None):
            return
        next_index = getattr(self, "current_image_index", -1) + 1
        if next_index < len(self.image_paths):
            self._display_image_by_index(next_index)

    def show_previous_image(self, event=None) -> None:
        if not getattr(self, "image_paths", None):
            return
        prev_index = getattr(self, "current_image_index", -1) - 1
        if prev_index >= 0:
            self._display_image_by_index(prev_index)

    def _set_image_collection(self, paths: Sequence[Path], start_index: int) -> None:
        self.image_paths = list(paths)
        if not self.image_paths:
            return
        self.current_image_index = -1
        self._display_image_by_index(max(0, start_index))

    def _display_image_by_index(self, index: int) -> None:
        if not self.image_paths:
            return
        if index < 0 or index >= len(self.image_paths):
            return
        path = self.image_paths[index]
        if not path.exists():
            messagebox.showerror("Fehler", f"Datei nicht gefunden: {path}")
            return
        try:
            image = Image.open(path).convert("RGBA")
        except Exception as exc:
            messagebox.showerror("Fehler", f"Bild konnte nicht geladen werden: {exc}")
            return

        self.image_path = path
        self.orig_img = image
        self.exclude_rects = []
        self._rubber_start = None
        self._rubber_id = None
        self.pick_mode = False

        self.prepare_preview()
        self.update_preview()

        dimensions = f"{self.orig_img.width}x{self.orig_img.height}"
        suffix = f" [{index + 1}/{len(self.image_paths)}]" if len(self.image_paths) > 1 else ""
        status_text = f"Geladen: {path.name} — {dimensions}{suffix}"
        self.status.config(text=status_text)
        self.status_default_text = status_text
        if hasattr(self, "filename_label"):
            self.filename_label.config(text=f"{path.name} — {dimensions}{suffix}")

        self.current_image_index = index

    def save_overlay(self) -> None:
        if self.orig_img is None:
            messagebox.showinfo("Info", "Kein Bild geladen.")
            return
        if self.preview_img is None:
            messagebox.showerror("Fehler", "Keine Preview vorhanden.")
            return

        overlay = self._build_overlay_image(
            self.orig_img,
            self.exclude_rects,
            alpha=int(self.alpha.get()),
            scale_from_preview=self.preview_img.size,
            is_match_fn=self.matches_target_color,
        )
        merged = Image.alpha_composite(self.orig_img.convert("RGBA"), overlay)

        out_path = filedialog.asksaveasfilename(
            defaultextension=".png", filetypes=[("PNG", "*.png")], title="Overlay speichern als"
        )
        if not out_path:
            return
        merged.save(out_path)
        messagebox.showinfo("Gespeichert", f"Overlay gespeichert: {out_path}")

    def prepare_preview(self) -> None:
        if self.orig_img is None:
            return
        width, height = self.orig_img.size
        max_w, max_h = PREVIEW_MAX_SIZE
        scale = min(max_w / width, max_h / height, 1.0)
        size = (max(1, int(width * scale)), max(1, int(height * scale)))
        self.preview_img = self.orig_img.resize(size, Image.LANCZOS)
        self.preview_tk = ImageTk.PhotoImage(self.preview_img)
        self.canvas_orig.delete("all")
        self.canvas_orig.config(width=size[0], height=size[1])
        self.canvas_overlay.config(width=size[0], height=size[1])
        self.canvas_orig.create_image(0, 0, anchor="nw", image=self.preview_tk)

    def update_preview(self) -> None:
        if self.preview_img is None:
            return
        merged = self.create_overlay_preview()
        if merged is None:
            return
        self.overlay_tk = ImageTk.PhotoImage(merged)
        self.canvas_overlay.delete("all")
        self.canvas_overlay.create_image(0, 0, anchor="nw", image=self.overlay_tk)

        self.canvas_orig.delete("all")
        self.canvas_orig.create_image(0, 0, anchor="nw", image=self.preview_tk)
        for (x0, y0, x1, y1) in self.exclude_rects:
            self.canvas_orig.create_rectangle(x0, y0, x1, y1, outline="yellow", width=3)

        stats = self.compute_stats_preview()
        if stats:
            matches_all, total_all = stats["all"]
            matches_keep, total_keep = stats["keep"]
            matches_ex, total_ex = stats["excl"]
            r_with = (matches_keep / total_keep * 100) if total_keep else 0.0
            r_no = (matches_all / total_all * 100) if total_all else 0.0
            excl_share = (total_ex / total_all * 100) if total_all else 0.0
            excl_match = (matches_ex / total_ex * 100) if total_ex else 0.0
            self.ratio_label.config(
                text=(
                    f"Treffer (mit Excludes): {r_with:.2f}%    |    "
                    f"Treffer (ohne Excludes): {r_no:.2f}%    |    "
                    f"Excluded: {excl_share:.2f}% vom Bild, davon {excl_match:.2f}% Treffer"
                )
            )

        bg = "#0f0f10" if self.theme == "dark" else "#1e1e1e"
        self.canvas_orig.configure(bg=bg)
        self.canvas_overlay.configure(bg=bg)

    def create_overlay_preview(self) -> Image.Image | None:
        if self.preview_img is None:
            return None
        base = self.preview_img.convert("RGBA")
        overlay = Image.new("RGBA", base.size, (0, 0, 0, 0))
        draw = ImageDraw.Draw(overlay)
        pixels = base.load()
        width, height = base.size
        alpha = int(self.alpha.get())
        for y in range(height):
            for x in range(width):
                if self._is_excluded(x, y):
                    continue
                r, g, b, a = pixels[x, y]
                if a == 0:
                    continue
                if self.matches_target_color(r, g, b):
                    draw.point((x, y), fill=(255, 0, 0, alpha))
        merged = Image.alpha_composite(base, overlay)
        for (x0, y0, x1, y1) in self.exclude_rects:
            ImageDraw.Draw(merged).rectangle([x0, y0, x1, y1], outline=(255, 215, 0, 200), width=3)
        return merged

    def compute_stats_preview(self):
        if self.preview_img is None:
            return None
        px = self.preview_img.convert("RGBA").load()
        width, height = self.preview_img.size
        matches_all = total_all = 0
        matches_keep = total_keep = 0
        matches_excl = total_excl = 0
        for y in range(height):
            for x in range(width):
                r, g, b, a = px[x, y]
                if a == 0:
                    continue
                is_excluded = self._is_excluded(x, y)
                total_all += 1
                if self.matches_target_color(r, g, b):
                    matches_all += 1
                if not is_excluded:
                    total_keep += 1
                    if self.matches_target_color(r, g, b):
                        matches_keep += 1
                else:
                    total_excl += 1
                    if self.matches_target_color(r, g, b):
                        matches_excl += 1
        return {
            "all": (matches_all, total_all),
            "keep": (matches_keep, total_keep),
            "excl": (matches_excl, total_excl),
        }

    def matches_target_color(self, r, g, b) -> bool:
        h, s, v = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)
        hue = h * 360.0
        hmin = float(self.hue_min.get())
        hmax = float(self.hue_max.get())
        smin = float(self.sat_min.get()) / 100.0
        vmin = float(self.val_min.get()) / 100.0
        vmax = float(self.val_max.get()) / 100.0
        if hmin <= hmax:
            hue_ok = hmin <= hue <= hmax
        else:
            hue_ok = (hue >= hmin) or (hue <= hmax)
        return hue_ok and (s >= smin) and (v >= vmin) and (v <= vmax)

    def _is_excluded(self, x: int, y: int) -> bool:
        return any(x0 <= x <= x1 and y0 <= y <= y1 for (x0, y0, x1, y1) in self.exclude_rects)

    @staticmethod
    def _map_preview_excludes(
        excludes: Iterable[Tuple[int, int, int, int]],
        orig_size: Tuple[int, int],
        preview_size: Tuple[int, int],
    ) -> list[Tuple[int, int, int, int]]:
        scale_x = orig_size[0] / preview_size[0]
        scale_y = orig_size[1] / preview_size[1]
        mapped = []
        for x0, y0, x1, y1 in excludes:
            mapped.append(
                (int(x0 * scale_x), int(y0 * scale_y), int(x1 * scale_x), int(y1 * scale_y))
            )
        return mapped

    @classmethod
    def _build_overlay_image(
        cls,
        image: Image.Image,
        excludes_preview: Iterable[Tuple[int, int, int, int]],
        *,
        alpha: int,
        scale_from_preview: Tuple[int, int],
        is_match_fn,
    ) -> Image.Image:
        overlay = Image.new("RGBA", image.size, (0, 0, 0, 0))
        draw = ImageDraw.Draw(overlay)
        pixels = image.load()
        width, height = image.size
        excludes = cls._map_preview_excludes(excludes_preview, image.size, scale_from_preview)
        for y in range(height):
            for x in range(width):
                if any(x0 <= x <= x1 and y0 <= y <= y1 for (x0, y0, x1, y1) in excludes):
                    continue
                r, g, b, a = pixels[x, y]
                if a == 0:
                    continue
                if is_match_fn(r, g, b):
                    draw.point((x, y), fill=(255, 0, 0, alpha))
        return overlay


__all__ = ["ImageProcessingMixin"]
