# arbre.py
"""Classe Arbre pour le jeu Bucheron - Projet NSI Première

Trois types d'arbres :
  "normal"  — équilibré, PV variable selon le niveau
  "bouleau" — fin et facile (1 PV), feuillage jaune-vert pâle
  "chene"   — massif et résistant (5 PV), feuillage vert très foncé
"""

import pygame
import random
import math
from constantes import *


# ── Données par type d'arbre (pv_fixe, rayon, tronc_w, c_bas, c_milieu, c_haut, c_reflet)
TYPES_ARBRES = {
    "normal":  (None, 20,  9, (20, 100,  20), (40, 130,  40), (60, 160,  60), ( 80, 200,  80)),
    "bouleau": (1,    13,  5, (155, 195,  95), (175, 215, 125), (195, 232, 155), (215, 242, 185)),
    "chene":   (5,    28, 13, (15,  65,  15), (25,  85,  25), (35, 105,  35), ( 55, 130,  55)),
}


class Arbre:
    """Représente un arbre sur la carte.

    Paramètres :
        x, y         : position du tronc (centre)
        pv           : points de vie (ignoré si le type impose ses PV)
        type_arbre   : "normal" | "bouleau" | "chene"
    """

    def __init__(self, x: int, y: int, pv: int, type_arbre: str = "normal"):
        self.x          = x
        self.y          = y
        self.type_arbre = type_arbre

        pv_fixe, rayon_base, self._tronc_w, \
            self._c_bas, self._c_milieu, self._c_haut, self._c_reflet \
            = TYPES_ARBRES[type_arbre]

        self.pv_max = pv_fixe if pv_fixe is not None else pv
        self.pv     = self.pv_max
        self.rayon  = rayon_base

        self.var_couleur = random.randint(-20, 20)
        self.var_taille  = random.uniform(0.88, 1.18)
        self.haut_tronc  = random.randint(7, 14)

        self._timer_tremblement = 0
        self._decalage_x        = 0.0

    # ── Accesseurs ─────────────────────────────────────────────────────
    def est_vivant(self) -> bool:
        return self.pv > 0

    # ── Logique ────────────────────────────────────────────────────────
    def recevoir_degats(self, degats: int = 1) -> None:
        self.pv = max(0, self.pv - degats)
        self._timer_tremblement = 12

    def mettre_a_jour(self) -> None:
        if self._timer_tremblement > 0:
            self._timer_tremblement -= 1
            self._decalage_x = math.sin(self._timer_tremblement * 1.8) * 4
        else:
            self._decalage_x = 0.0

    # ── Dessin ─────────────────────────────────────────────────────────
    def dessiner(self, surface: pygame.Surface) -> None:
        cx = int(self.x + self._decalage_x)
        cy = int(self.y)

        if not self.est_vivant():
            self._dessiner_souche(surface, cx, cy)
            return

        r = int(self.rayon * self.var_taille)
        v = self.var_couleur

        def clampe(val: int) -> int:
            return max(0, min(255, val))

        c_bas    = tuple(clampe(c + v) for c in self._c_bas)
        c_milieu = tuple(clampe(c + v) for c in self._c_milieu)
        c_haut   = tuple(clampe(c + v) for c in self._c_haut)

        # ── Tronc selon le type ──
        hw = self._tronc_w // 2
        if self.type_arbre == "bouleau":
            pygame.draw.rect(surface, BLANC_BOULEAU,
                             (cx - hw, cy - 3, self._tronc_w, self.haut_tronc))
            for ry in range(cy - 1, cy + self.haut_tronc - 2, 4):
                pygame.draw.line(surface, (150, 140, 120),
                                 (cx - hw, ry), (cx + hw, ry), 1)
        elif self.type_arbre == "chene":
            pygame.draw.rect(surface, BRUN_CHENE,
                             (cx - hw, cy - 5, self._tronc_w, self.haut_tronc + 4))
        else:
            pygame.draw.rect(surface, MARRON_FONCE,
                             (cx - hw, cy - 4, self._tronc_w, self.haut_tronc))

        # Ombre portée
        ombre = pygame.Surface((r * 2 + 6, r + 8), pygame.SRCALPHA)
        pygame.draw.ellipse(ombre, (0, 0, 0, 45), ombre.get_rect())
        surface.blit(ombre, (cx - r - 3, cy - r // 3))

        # Feuillage (3 couches)
        centre_y = cy - r // 2
        pygame.draw.circle(surface, c_bas,    (cx, centre_y),      r)
        pygame.draw.circle(surface, c_milieu, (cx, centre_y - 5),  int(r * 0.82))
        pygame.draw.circle(surface, c_haut,   (cx, centre_y - 10), int(r * 0.62))
        pygame.draw.circle(surface, self._c_reflet,
                           (cx - r // 4, centre_y - 10), int(r * 0.28))

        # Icône de type + barre de vie
        self._dessiner_icone_type(surface, cx, cy - r - 12)
        if self.pv < self.pv_max:
            self._dessiner_barre_vie(surface, cx, cy - r - 22)

    def _dessiner_icone_type(self, surface, cx, by):
        if self.type_arbre == "bouleau":
            pygame.draw.circle(surface, VERT_BOULEAU, (cx, by), 4)
            pygame.draw.circle(surface, BLANC,        (cx, by), 4, 1)
        elif self.type_arbre == "chene":
            pygame.draw.circle(surface, BRUN_CHENE,   (cx, by), 5)
            pygame.draw.circle(surface, (200, 160, 60), (cx, by), 5, 1)

    def _dessiner_souche(self, surface, cx, cy):
        r_s = 8 if self.type_arbre == "bouleau" else (13 if self.type_arbre == "chene" else 10)
        c_i = BLANC_BOULEAU if self.type_arbre == "bouleau" else MARRON
        pygame.draw.circle(surface, MARRON_FONCE, (cx, cy), r_s)
        pygame.draw.circle(surface, c_i,          (cx, cy), r_s - 3)
        pygame.draw.circle(surface, MARRON_FONCE, (cx, cy), r_s // 3, 1)

    def _dessiner_barre_vie(self, surface, cx, by):
        bw, bh = 36, 6
        bx = cx - bw // 2
        pygame.draw.rect(surface, ROUGE, (bx, by, bw, bh))
        ratio = self.pv / self.pv_max
        pygame.draw.rect(surface, VERT,  (bx, by, int(bw * ratio), bh))
        pygame.draw.rect(surface, NOIR,  (bx, by, bw, bh), 1)
