#keine KI ausser bei draw
import pyxel
import random


class Block:
    def __init__(self, pos_x, pos_y, moves, w=32, h=8):
        self.pos_x = pos_x
        self.pos_y = pos_y
        self.w = w
        self.h = h
        self.is_moving = moves
        self.move_timer = 0
        self.direction = 0
        
        self.details = []
        for _ in range(2):
            dx = random.randint(4, w - 8)
            dy = random.randint(2, h - 3)
            self.details.append((dx, dy))

    def draw(self):
        # 1. Color Palette
        if self.is_moving == 10:
            top_edge = 15   # White highlight
            body_col = 14   # Pink/Red
            side_col = 2    # Dark Purple (for the slants)
        else:
            top_edge = 11   # Light Green highlight
            body_col = 3    # Dark Green
            side_col = 1    # Dark Blue (for the slants)

        # 2. DRAW THE TRAPEZOID BODY (\___/)
        # We loop through each row of the height
        for i in range(self.h):
            # Calculate how much to indent the sides (tapering)
            # Row 0 is full width, Row 1 indents by 1, Row 2 by 2...
            indent = i // 2  # Changes slope; use i for steep, i//2 for shallow
            
            draw_x = self.pos_x + indent
            draw_w = self.w - (indent * 2)
            
            # Draw the main horizontal slice
            if i == 0:
                # Top surface highlight
                pyxel.rect(draw_x, self.pos_y + i, draw_w, 1, top_edge)
            else:
                # Body fill
                pyxel.rect(draw_x, self.pos_y + i, draw_w, 1, body_col)
                
                # Side shading (the slanted parts)
                pyxel.pset(draw_x, self.pos_y + i, side_col)
                pyxel.pset(draw_x + draw_w - 1, self.pos_y + i, side_col)

        for dx, dy in self.details:
            if dx > dy // 2 and dx < self.w - (dy // 2):
                pyxel.pset(self.pos_x + dx, self.pos_y + dy, side_col)

        pyxel.line(self.pos_x, self.pos_y, self.pos_x + self.w - 1, self.pos_y, 0)

        bot_indent = (self.h - 1) // 2
        pyxel.line(self.pos_x + bot_indent, self.pos_y + self.h - 1, 
                   self.pos_x + self.w - 1 - bot_indent, self.pos_y + self.h - 1, 0)

        pyxel.line(self.pos_x, self.pos_y, self.pos_x + bot_indent, self.pos_y + self.h - 1, 0)

        pyxel.line(self.pos_x + self.w - 1, self.pos_y, 
                   self.pos_x + self.w - 1 - bot_indent, self.pos_y + self.h - 1, 0)

    def moving(self):
        if self.is_moving == 10:
            self.move_timer += 1
            if self.move_timer >= 10:
                self.move_timer = 0
                self.direction = random.uniform(-1.5, 1.5)
            self.pos_x += self.direction

            if self.pos_x < 0: self.pos_x = 0; self.direction *= -1
            if self.pos_x + self.w > 160: self.pos_x = 160 - self.w; self.direction *= -1

    def is_colliding(self, px, py, pw, ph, vel_y):
        window = max(8, abs(vel_y) + 4)
        overlapping = (px < self.pos_x + self.w and
                       px + pw > self.pos_x and
                       py + ph > self.pos_y - window and
                       py + ph < self.pos_y + 4)
        return True if overlapping and vel_y >= 0 else None

    def resolve_collision(self, px, py, pw, ph, vel_y):
        return True if self.is_colliding(px, py, pw, ph, vel_y) else None

    def return_y(self):
        return self.pos_y

    def return_x(self):
        return self.pos_x