import pygame
import core.image_loader as image_loader

class CraftMenu:
    def __init__(self):
        self.crafts = {
            "wood": {
                "raw_wood": 1,
            },
            "wooden_picaxe": {
                "wood": 2,
                "raw_wood": 2
            },
            "stone_picaxe": {
                "wood": 2,
                "stone": 3
            }
        }
        self.result_amounts = {
            "wood": 3,
            "wooden_picaxe": 1,
            "stone_picaxe": 1,
        }

        self.is_open = False

    def draw(self, screen, screen_size, font):
        pygame.draw.rect(screen, (190, 140, 80), (50, 50, screen_size[0]-100, screen_size[1]-100))
        for i, (result, needs) in enumerate(self.crafts.items()):
            screen.blit(image_loader.item_images[result], (60, 60+i*70))
            screen.blit(font.render("needs: ", True, (0, 0, 0)), (130, 60+i*70))
            for j, (item, count) in enumerate(needs.items()):
                pos = [240+j*70, 60+i*70]
                screen.blit(image_loader.item_images[item], (pos[0], pos[1]))
                screen.blit(font.render(str(count), True, (0, 0, 0)), (pos[0], pos[1]+20))

    def handle_event(self, event, hotbar_items):
        if event.type == pygame.MOUSEBUTTONDOWN:
            if self.is_open:
                for i, (result, needs) in enumerate(self.crafts.items()):
                    pos = [60, 60+i*70]
                    rect = pygame.Rect(pos[0], pos[1], 64, 64)
                    if rect.collidepoint(event.pos):
                        have_needs = True
                        for item, count in needs.items():
                            have_good_amount = True
                            test_hotbar_items = hotbar_items.copy()
                            for j in range(count):
                                if item in test_hotbar_items:
                                    test_hotbar_items[item] -= 1
                                    if test_hotbar_items[item] <= 0:
                                        del test_hotbar_items[item]

                                else:
                                    have_good_amount = False
                                    break
                            if not have_good_amount:
                                have_needs = False
                                break

                        if have_needs:
                            new_hotbar_items = hotbar_items.copy()
                            for item, count in needs.items():
                                for j in range(count):
                                    new_hotbar_items[item] -= 1
                                    if new_hotbar_items[item] <= 0:
                                        del new_hotbar_items[item]
                            if result in new_hotbar_items:
                                new_hotbar_items[result] += self.result_amounts[result]
                            else:
                                new_hotbar_items[result] = self.result_amounts[result]
                            return True, new_hotbar_items

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_e:
                self.is_open = not self.is_open

        return False, None