import pygame
import time
import controls
from screens.startscreen import StartScreen
from tilemap.playground import PlayGround
from player.player import Player, Players
from enum import Enum

class State(Enum):
    STARTING = 1
    PLAYING = 2
    PAUSING = 3
    ENDING = 4
    SETTING = 5
    HIGHSCORE = 6

class Snake:
    def __init__(self):
        self.STATES = {"starting": 0, "playing": 1, "pausing": 2, "ending": 3, "setting": 4, "highscore": 5}
        self.running = True
        self.numplayers: int = 1
        self.playground: PlayGround = None
        self.controls = None
        self.players: list = []
        self.windowed: bool = True
        self.width: int = 800
        self.height: int =600
        self.screen = None
        self.icon = pygame.image.load("snake.webp")
        pygame.display.set_icon(self.icon)
        
        self.set_state("starting")
        self.set_windowed(self.width, self.height)
        # self.set_fullscreen(self.width, self.height)
        
        self.mainloop()
        
    def mainloop(self):
        while self.running:
            match self.state:
                case 0:
                    self.screen = StartScreen(self)
                    pygame.display.flip()
                    waiting = True
                    while waiting:
                        for event in pygame.event.get():
                            if event.type == pygame.QUIT:
                                waiting = False
                            elif event.type == pygame.KEYDOWN:
                                if event.key == pygame.K_F1:
                                    print("F1 pressed!")
                                    waiting = False
                                elif event.key == pygame.K_F2:
                                    print("F2 pressed!")
                                    waiting = False
                case 1:
                    pass
                case 2:
                    pass
                case 3:
                    pass
                case 4:
                    pass
                case 5:
                    pass
            pygame.display.flip()
            time.sleep(5)
            self.running = False
        pygame.quit()
    
    def set_state(self, state: str) -> None:
        self.state = self.STATES[state]
        
    def set_windowed(self, width:int, height: int) -> None:
        self.screen = pygame.display.set_mode((width, height))
        self.windowed = True
        pygame.display.set_caption("Snake")
        
    def set_fullscreen(self, width: int, height: int) -> None:
        self.screen = pygame.display.set_mode((width, height), flags=pygame.FULLSCREEN)
        self.windowed = False
        
    def get_width(self) -> int:
        return self.width
    
    @property
    def numplayers(self) -> int:
        return self._numplayers
    
    @numplayers.setter
    def numplayers(self, pl: int):
        if pl <= 0 or pl >= 3:
            raise ValueError("Numplayers must be a positive value of 1 or 2")
        else:
            self._numplayers = pl

def main():
    pygame.init()
    snake = Snake()

if __name__ == "__main__":
    main()
    