import pygame
from pygame.locals import *
from image import *
from sys import exit
from userinput import *
from random import randint


def playhg(screen):

    backcolor = (255,255,255)
    adjacent_color = (100,100,255)
    grid_width = 64
    grid_size = (10,5)
    player_pos = (3,3)

    player_image = loadImage('player.png').convert_alpha()
    player_image = pygame.transform.scale(player_image, (grid_width, grid_width))
    hunt_image = loadImage('huntbutton.png').convert_alpha()
    hunt_rect = Rect((50, 350), hunt_image.get_size())
    settle_image = loadImage('settlebutton.png').convert_alpha()
    settle_rect = Rect((50, 420), settle_image.get_size())
    background = loadImage('map.png').convert_alpha()
    food = 200
    font = pygame.font.SysFont('Arial', 18)
    hunted = False
    settle = False
    while True:
        clicked = False
        moved = False
        
        for event in pygame.event.get():
            standardInput(event, screen)
            if event.type == MOUSEBUTTONDOWN:
                clicked = True

        mouse_pos = pygame.mouse.get_pos()

        screen.fill(backcolor)
        screen.blit(background, (0,0))
        
        

        
        showMapofSquare(screen, player_pos, grid_width)
        screen.blit(player_image, (player_pos[0]*grid_width, player_pos[1]*grid_width))


        if settle:
            showSettledMessage(screen, font)
        else:
            player_pos, moved = colorAdjacentSquares(screen, adjacent_color, player_pos,
                                grid_width, grid_size,
                                mouse_pos, clicked)

        drawGrid(grid_width, grid_size, screen)
        

        if moved:
            food_lost = randint(45,65)
            if food_lost > food:
                food_lost = food
            food -= food_lost
            showGainedMessage(screen, font, food_lost, True)
            hunted = False
            if food <=0 :
                settle = True
        if clicked and settle_rect.collidepoint(mouse_pos):
            return
        if clicked and hunt_rect.collidepoint(mouse_pos) and not settle:
            food_gained = randint(10, 60)
            hunted = True
            food += food_gained
            showGainedMessage(screen, font, food_gained, False)
        if not settle:    
            showFood(screen, font, food)

        if not hunted and not settle:
            screen.blit(hunt_image, hunt_rect)
        screen.blit(settle_image, settle_rect)
        pygame.display.update()
        

