"""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=self._t("dialog.open_image_title"),
            filetypes=[(self._t("dialog.images_filter"), "*.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=self._t("dialog.open_folder_title"),
            initialdir=str(default_dir),
        )
        if not directory:
            return
        folder = Path(directory)
        if not folder.exists():
            messagebox.showerror(
                self._t("dialog.error_title"),
                self._t("dialog.folder_not_found"),
            )
            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(
                self._t("dialog.info_title"),
                self._t("dialog.folder_empty"),
            )
            return
        self._set_image_collection(image_files, 0)

    def show_next_image(self, event=None) -> None:
        if not getattr(self, "image_paths", None):
            return
        if not self.image_paths:
            return
        current = getattr(self, "current_image_index", -1)
        next_index = (current + 1) % 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
        if not self.image_paths:
            return
        current = getattr(self, "current_image_index", -1)
        prev_index = (current - 1) % len(self.image_paths)
        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(
                self._t("dialog.error_title"),
                self._t("dialog.file_missing", path=path),
            )
            return
        try:
            image = Image.open(path).convert("RGBA")
        except Exception as exc:
            messagebox.showerror(
                self._t("dialog.error_title"),
                self._t("dialog.image_open_failed", error=exc),
            )
            return

        self.image_path = path
        self.orig_img = image
        self.exclude_shapes = []
        self._rubber_start = None
        self._rubber_id = None
        self._stroke_preview_id = None
        self._exclude_mask = None
        self._exclude_mask_px = None
        self._exclude_mask_dirty = True
        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 = self._t("status.loaded", name=path.name, dimensions=dimensions, position=suffix)
        self.status.config(text=status_text)
        self.status_default_text = status_text
        if hasattr(self, "filename_label"):
            filename_text = self._t(
                "status.filename_label",
                name=path.name,
                dimensions=dimensions,
                position=suffix,
            )
            self.filename_label.config(text=filename_text)

        self.current_image_index = index

    def save_overlay(self) -> None:
        if self.orig_img is None:
            messagebox.showinfo(
                self._t("dialog.info_title"),
                self._t("dialog.no_image_loaded"),
            )
            return
        if self.preview_img is None:
            messagebox.showerror(
                self._t("dialog.error_title"),
                self._t("dialog.no_preview_available"),
            )
            return

        overlay = self._build_overlay_image(
            self.orig_img,
            tuple(self.exclude_shapes),
            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=self._t("dialog.save_overlay_title"),
        )
        if not out_path:
            return
        merged.save(out_path)
        messagebox.showinfo(
            self._t("dialog.saved_title"),
            self._t("dialog.overlay_saved", path=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)
        self._exclude_mask = None
        self._exclude_mask_px = None
        self._exclude_mask_dirty = True
        if getattr(self, "exclude_shapes", None):
            self._ensure_exclude_mask()

    def update_preview(self) -> None:
        if self.preview_img is None:
            return
        self._ensure_exclude_mask()
        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)
        self._render_exclusion_overlays()

        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=self._t(
                    "stats.summary",
                    with_pct=r_with,
                    without_pct=r_no,
                    excluded_pct=excl_share,
                    excluded_match_pct=excl_match,
                )
            )

        refresher = getattr(self, "_refresh_canvas_backgrounds", None)
        if callable(refresher):
            refresher()
        else:
            bg = "#0f0f10" if self.theme == "dark" else "#ffffff"
            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
        self._ensure_exclude_mask()
        base = self.preview_img.convert("RGBA")
        overlay = Image.new("RGBA", base.size, (0, 0, 0, 0))
        draw = ImageDraw.Draw(overlay)
        pixels = base.load()
        mask_px = self._exclude_mask_px
        width, height = base.size
        alpha = int(self.alpha.get())
        for y in range(height):
            for x in range(width):
                if mask_px is not None and mask_px[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)
        outline = ImageDraw.Draw(merged)
        for shape in getattr(self, "exclude_shapes", []):
            if shape.get("kind") == "rect":
                x0, y0, x1, y1 = shape["coords"]  # type: ignore[index]
                outline.rectangle([x0, y0, x1, y1], outline=(255, 215, 0, 200), width=3)
            elif shape.get("kind") == "stroke":
                points = shape.get("points", [])
                if len(points) < 2:
                    continue
                width_px = int(shape.get("width", 8))
                outline.line(points, fill=(255, 215, 0, 200), width=width_px, joint="round")
        return merged

    def compute_stats_preview(self):
        if self.preview_img is None:
            return None
        self._ensure_exclude_mask()
        px = self.preview_img.convert("RGBA").load()
        mask_px = self._exclude_mask_px
        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
                excluded = bool(mask_px and mask_px[x, y])
                total_all += 1
                if self.matches_target_color(r, g, b):
                    matches_all += 1
                if not 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:
        self._ensure_exclude_mask()
        if self._exclude_mask_px is None:
            return False
        try:
            return bool(self._exclude_mask_px[x, y])
        except Exception:
            return False

    @classmethod
    def _build_overlay_image(
        cls,
        image: Image.Image,
        shapes: Iterable[dict[str, object]],
        *,
        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
        mask = cls._build_exclude_mask_for_size(tuple(shapes), scale_from_preview, image.size)
        mask_px = mask.load() if mask else None
        for y in range(height):
            for x in range(width):
                if mask_px is not None and mask_px[x, y]:
                    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

    @classmethod
    def _build_exclude_mask_for_size(
        cls,
        shapes: Iterable[dict[str, object]],
        preview_size: Tuple[int, int],
        target_size: Tuple[int, int],
    ) -> Image.Image | None:
        if not preview_size or not target_size or preview_size[0] == 0 or preview_size[1] == 0:
            return None
        mask = Image.new("L", target_size, 0)
        draw = ImageDraw.Draw(mask)
        scale_x = target_size[0] / preview_size[0]
        scale_y = target_size[1] / preview_size[1]
        for shape in shapes:
            kind = shape.get("kind")
            cls._draw_shape_on_mask(draw, shape, scale_x=scale_x, scale_y=scale_y)
        return mask

    def _ensure_exclude_mask(self) -> None:
        if self.preview_img is None:
            return
        size = self.preview_img.size
        if (
            self._exclude_mask is None
            or self._exclude_mask.size != size
            or getattr(self, "_exclude_mask_dirty", False)
        ):
            self._exclude_mask = Image.new("L", size, 0)
            draw = ImageDraw.Draw(self._exclude_mask)
            for shape in getattr(self, "exclude_shapes", []):
                self._draw_shape_on_mask(draw, shape, scale_x=1.0, scale_y=1.0)
            self._exclude_mask_px = self._exclude_mask.load()
            self._exclude_mask_dirty = False
        elif self._exclude_mask_px is None:
            self._exclude_mask_px = self._exclude_mask.load()

    def _stamp_shape_on_mask(self, shape: dict[str, object]) -> None:
        if self.preview_img is None:
            return
        if self._exclude_mask is None or self._exclude_mask.size != self.preview_img.size:
            self._exclude_mask_dirty = True
            return
        draw = ImageDraw.Draw(self._exclude_mask)
        self._draw_shape_on_mask(draw, shape, scale_x=1.0, scale_y=1.0)
        self._exclude_mask_px = self._exclude_mask.load()

    @staticmethod
    def _draw_shape_on_mask(
        draw: ImageDraw.ImageDraw,
        shape: dict[str, object],
        *,
        scale_x: float,
        scale_y: float,
    ) -> None:
        kind = shape.get("kind")
        if kind == "rect":
            x0, y0, x1, y1 = shape["coords"]  # type: ignore[index]
            draw.rectangle(
                [
                    x0 * scale_x,
                    y0 * scale_y,
                    x1 * scale_x,
                    y1 * scale_y,
                ],
                fill=255,
            )
        elif kind == "stroke":
            points = shape.get("points")
            if not points or len(points) < 2:
                return
            base_width = float(shape.get("width", 8))  # type: ignore[arg-type]
            width_px = max(1, int(round(base_width * (scale_x + scale_y) / 2.0)))
            scaled = [(px * scale_x, py * scale_y) for px, py in points]  # type: ignore[misc]
            draw.line(scaled, fill=255, width=width_px, joint="round")

    def _render_exclusion_overlays(self) -> None:
        if not hasattr(self, "canvas_orig"):
            return
        for item in getattr(self, "_exclude_canvas_ids", []):
            try:
                self.canvas_orig.delete(item)
            except Exception:
                pass
        self._exclude_canvas_ids = []
        for shape in getattr(self, "exclude_shapes", []):
            kind = shape.get("kind")
            if kind == "rect":
                x0, y0, x1, y1 = shape["coords"]  # type: ignore[index]
                item = self.canvas_orig.create_rectangle(
                    x0, y0, x1, y1, outline="yellow", width=3
                )
                self._exclude_canvas_ids.append(item)
            elif kind == "stroke":
                points = shape.get("points")
                if not points or len(points) < 2:
                    continue
                width_px = int(shape.get("width", 8))
                coords = [coord for point in points for coord in point]  # type: ignore[misc]
                item = self.canvas_orig.create_line(
                    *coords,
                    fill="yellow",
                    width=width_px,
                    smooth=True,
                    capstyle="round",
                    joinstyle="round",
                )
                self._exclude_canvas_ids.append(item)


__all__ = ["ImageProcessingMixin"]
