import pygame
import random
import os

pygame.init()

SCREEN_WIDTH = 500
SCREEN_HEIGHT = 600

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (200, 200, 200)
DARK_GRAY = (100, 100, 100)

BIRD_WIDTH = 34
BIRD_HEIGHT = 24
PIPE_WIDTH = 50
PIPE_HEIGHT = 320
PIPE_GAP = 220
BACKGROUND_SPEED = 2

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Flappy Minecraft')

BIRD_IMG = pygame.image.load('bird.png').convert_alpha()
BIRD_IMG = pygame.transform.scale(BIRD_IMG, (BIRD_WIDTH, BIRD_HEIGHT))

PIPE_IMG = pygame.image.load('pipe.png').convert_alpha()
PIPE_IMG = pygame.transform.scale(PIPE_IMG, (PIPE_WIDTH, PIPE_HEIGHT))

BACKGROUND_IMG = pygame.image.load('background.png').convert_alpha()
BACKGROUND_IMG = pygame.transform.scale(BACKGROUND_IMG, (SCREEN_WIDTH, SCREEN_HEIGHT))

PAUSE_ICON = pygame.image.load('pause.png').convert_alpha()
PAUSE_ICON = pygame.transform.scale(PAUSE_ICON, (40, 40))

class Bird:
    def __init__(self):
        self.x = 50
        self.y = SCREEN_HEIGHT // 2
        self.gravity = 0.6
        self.lift = -15
        self.velocity = 0

    def show(self):
        screen.blit(BIRD_IMG, (self.x, self.y))

    def update(self):
        self.velocity += self.gravity
        self.y += self.velocity

        if self.y > SCREEN_HEIGHT - BIRD_HEIGHT:
            self.y = SCREEN_HEIGHT - BIRD_HEIGHT
            self.velocity = 0

        if self.y < 0:
            self.y = 0
            self.velocity = 0

    def up(self):
        self.velocity = self.lift

class Pipe:
    def __init__(self):
        self.x = SCREEN_WIDTH
        self.top = random.randint(50, SCREEN_HEIGHT // 2 - PIPE_GAP // 2)
        self.bottom = self.top + PIPE_GAP
        self.speed = 3
        self.passed = False

    def show(self):
        screen.blit(pygame.transform.flip(PIPE_IMG, False, True), (self.x, self.top - PIPE_HEIGHT))
        screen.blit(PIPE_IMG, (self.x, self.bottom))

    def update(self):
        self.x -= self.speed

    def offscreen(self):
        return self.x < -PIPE_WIDTH

    def hits(self, bird):
        if bird.x + BIRD_WIDTH > self.x and bird.x < self.x + PIPE_WIDTH:
            if bird.y < self.top or bird.y + BIRD_HEIGHT > self.bottom:
                return True
        return False

def draw_button(text, x, y, width, height, color, hover_color):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if x + width > mouse[0] > x and y + height > mouse[1] > y:
        pygame.draw.rect(screen, hover_color, (x, y, width, height))
        if click[0] == 1:
            return True
    else:
        pygame.draw.rect(screen, color, (x, y, width, height))

    font = pygame.font.Font(None, 36)
    text_surface = font.render(text, True, BLACK)
    screen.blit(text_surface, (x + (width // 2 - text_surface.get_width() // 2),
                               y + (height // 2 - text_surface.get_height() // 2)))

    return False

def main_menu():
    running = True
    while running:
        screen.blit(BACKGROUND_IMG, (0, 0))

        if draw_button('Start', 200, 300, 100, 50, GRAY, DARK_GRAY):
            return 'start'

        pygame.display.update()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()

def pause_menu():
    paused = True
    while paused:
        screen.fill(WHITE)

        if draw_button('Resume', 200, 200, 100, 50, GRAY, DARK_GRAY):
            return 'resume'

        if draw_button('Exit', 200, 300, 100, 50, GRAY, DARK_GRAY):
            pygame.quit()
            exit()

        pygame.display.update()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()

HIGHSCORE_FILE = os.path.join(os.path.dirname(__file__), 'highscore.txt')

def load_highscore():
    try:
        with open(HIGHSCORE_FILE, 'r') as f:
            return int(f.read().strip() or 0)
    except Exception:
        return 0

def save_highscore(score):
    try:
        with open(HIGHSCORE_FILE, 'w') as f:
            f.write(str(score))
    except Exception:
        pass

def game_over_screen(score):
    highscore = load_highscore()
    if score > highscore:
        highscore = score
        save_highscore(score)

    font_large = pygame.font.Font(None, 64)
    font_small = pygame.font.Font(None, 36)

    while True:
        screen.blit(BACKGROUND_IMG, (0, 0))
        overlay = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT))
        overlay.set_alpha(180)
        overlay.fill(WHITE)
        screen.blit(overlay, (0, 0))

        title = font_large.render('Game Over', True, BLACK)
        scr_surf = font_small.render(f'Score: {score}', True, BLACK)
        hs_surf = font_small.render(f'High Score: {highscore}', True, BLACK)

        screen.blit(title, (SCREEN_WIDTH // 2 - title.get_width() // 2, 120))
        screen.blit(scr_surf, (SCREEN_WIDTH // 2 - scr_surf.get_width() // 2, 220))
        screen.blit(hs_surf, (SCREEN_WIDTH // 2 - hs_surf.get_width() // 2, 260))

        if draw_button('Restart', 150, 340, 200, 50, GRAY, DARK_GRAY):
            return 'restart'
        if draw_button('Exit', 150, 410, 200, 50, GRAY, DARK_GRAY):
            pygame.quit()
            exit()

        pygame.display.update()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()

def main():
    clock = pygame.time.Clock()
    font = pygame.font.Font(None, 36)

    while True:
        bird = Bird()
        pipes = [Pipe()]
        score = 0

        background_x1 = 0
        background_x2 = SCREEN_WIDTH
        paused = False
        running = True

        while running:
            clock.tick(30)

            background_x1 -= BACKGROUND_SPEED
            background_x2 -= BACKGROUND_SPEED

            if background_x1 <= -SCREEN_WIDTH:
                background_x1 = SCREEN_WIDTH
            if background_x2 <= -SCREEN_WIDTH:
                background_x2 = SCREEN_WIDTH

            screen.blit(BACKGROUND_IMG, (background_x1, 0))
            screen.blit(BACKGROUND_IMG, (background_x2, 0))

            screen.blit(PAUSE_ICON, (10, 10))
            mouse = pygame.mouse.get_pos()
            click = pygame.mouse.get_pressed()
            if 10 + 40 > mouse[0] > 10 and 10 + 40 > mouse[1] > 10:
                if click[0] == 1:
                    paused = True

            while paused:
                if pause_menu() == 'resume':
                    paused = False

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        bird.up()

            bird.update()
            bird.show()

            if pipes[-1].x < SCREEN_WIDTH // 2:
                pipes.append(Pipe())

            for pipe in pipes:
                pipe.update()
                pipe.show()

                if pipe.hits(bird):
                    print(f"Bird hit a pipe at x={pipe.x}, top={pipe.top}, bottom={pipe.bottom}")
                    running = False

                if not pipe.passed and pipe.x + PIPE_WIDTH < bird.x:
                    pipe.passed = True
                    score += 1

            pipes = [pipe for pipe in pipes if not pipe.offscreen()]

            highscore = load_highscore()
            score_surface = font.render(f'Score: {score}', True, BLACK)
            hs_surface = font.render(f'High: {highscore}', True, BLACK)
            screen.blit(score_surface, (SCREEN_WIDTH // 2 - score_surface.get_width() // 2, 50))
            screen.blit(hs_surface, (10, 10 + 40 + 8))

            pygame.display.update()

        result = game_over_screen(score)
        if result == 'restart':
            continue
        else:
            break

    pygame.quit()

if __name__ == "__main__":
    if main_menu() == 'start':
        main()
