import pygame
from Classes.Objects.BoardField import BoardField
from Classes.System.Components.Label import Label
from Classes.Objects.Cards.MonsterCard import MonsterCard
from Classes.System.Components.InputHandler import InputHandler

class World():
    __boardFields:list
    __labels:list
    __cards:pygame.sprite.Group()
    __screen:pygame.surface
    __cardWidth:int = 150
    __cardHeight:int = 200
    __cardOffset:int = 400

    def __init__(self, screen:pygame.surface, cardWidth:int=200, cardHeight:int=250, cardOffset:int=400):
        self.__boardFields = []
        self.__labels = []
        self.__cards = []
        self.__screen = screen
        self.__cardWidth = cardWidth
        self.__cardHeight = cardHeight
        self.__cardOffset = cardOffset
        self.buildGameWorld()

    def buildGameWorld(self):
        # construct elements arround the playerfield
        # Todo add lifepoint label for player and enemy and make them scriptable move to player class

        # presets the y position later passed down to the vector2
        eRow1Height = 85
        eRow2Height = (self.__cardHeight + 45)
        pRow1Height = ((2 * self.__cardHeight) + 75)
        pRow2Height = ((2 * self.__cardHeight) + self.__cardHeight + 30)
        
        eDeckPos = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * 0)), eRow1Height)
        eGravePos = pygame.Vector2((self.__cardOffset + ((self.__cardWidth + 10) * 0)), eRow2Height)
        eNamePos = pygame.Vector2(15, 45)
        eHPPos = pygame.Vector2(15, 75)
        
        pGravePos = pygame.Vector2((self.__cardOffset + (((self.__cardWidth + 10) - 30 ) * 5)), pRow1Height)
        pDeckPos = pygame.Vector2((self.__cardOffset + (((self.__cardWidth + 10) - 30 ) * 5)), pRow2Height)
        pNamePos = pygame.Vector2(20, pRow2Height + 195)
        pHPPos = pygame.Vector2(20, pRow2Height + 225)

        # labeling
        self.__labels.append(Label("PlayerHP", self.__screen, "1000 / 1000", pHPPos))
        self.__labels.append(Label("PlayerName", self.__screen, "Player", pNamePos))
        self.__labels.append(Label("EnemyHP", self.__screen, "1000 / 1000", eHPPos))
        self.__labels.append(Label("EnemyName", self.__screen, "Enemy", eNamePos))

        self.__boardFields.append(BoardField("EnemyDeck", "Enemy", "Deck", eDeckPos, "Assets/Cards/Arena/field.png"))
        self.__boardFields.append(BoardField("EnemyGraveyard", "Enemy", "Grave", eGravePos, "Assets/Cards/Arena/field.png"))
        self.__boardFields.append(BoardField("PlayerDeck", "Player", "Deck", pDeckPos, "Assets/Cards/Arena/field.png"))
        self.__boardFields.append(BoardField("PlayerGraveyard", "Player", "Grave", pGravePos, "Assets/Cards/Arena/field.png"))

        # handle field creation
        for i in range(5):
            pMonsterPos = pygame.Vector2((self.__cardOffset + (((self.__cardWidth + 10) - 30) * i)), pRow1Height)
            pEffectPos = pygame.Vector2((self.__cardOffset + (((self.__cardWidth + 10) - 30) * i)), pRow2Height)
            eMonsterPos = pygame.Vector2((self.__cardOffset + (((self.__cardWidth + 10) - 30) * (i+1)), eRow1Height))
            eEffectPos = pygame.Vector2((self.__cardOffset + (((self.__cardWidth + 10) - 30) * (i+1))), eRow2Height)

            self.__boardFields.append(BoardField("PlayerMonsterField-"+str(i), "Player", "MonsterField", pMonsterPos, "Assets/Cards/Arena/field.png", "pm"+str(id)))
            self.__boardFields.append(BoardField("PlayerEffectField-"+str(i), "Player", "EffectField", pEffectPos, "Assets/Cards/Arena/field.png", "pe"+str(id)))
            self.__boardFields.append(BoardField("EnemyMonsterField-"+str(i), "Enemy", "MonsterField", eMonsterPos, "Assets/Cards/Arena/field.png", "em"+str(id)))
            self.__boardFields.append(BoardField("EnemySpellTrapField-"+str(i), "Enemy", "EffectField", eEffectPos, "Assets/Cards/Arena/field.png", "ee"+str(id)))
            
    def getBoardFields(self) -> list:
        return self.__boardFields
    
    def getLabels(self) -> list:
        return self.__labels

    def getCardWidth(self) -> int:
        return self.__cardWidth
    
    def getCardHeight(self) -> int:
        return self.__cardHeight
    
    def getCards(self) -> pygame.sprite.Group:
        return self.__cards
    
    def spawnCard(self, asset:str, pos:pygame.Vector2, inputHandler:InputHandler) -> MonsterCard:
        self.__cards.add(MonsterCard(pos, asset, inputHandler))

    def spawnCards(self, cards:pygame.sprite.Group):
        for card in cards:
            self.__cards.append(card)