import os
import sdl2
import sdl2.ext
from sdl2.ext.compat import byteify
from ctypes import *
 
from PIL import Image
from sdl2 import SDL_AudioSpec


class GameWindow:
    def __init__(self, width, height, cell_size, title="Default", key_callback=None):
        
        self.cell_size = cell_size
        self.width = width * cell_size
        self.height = height * cell_size
        actual_screen_size = os.environ.get("RESOLUTION", "640x480").split("x")
        actual_screen_size = tuple(map(int, actual_screen_size))
        self.target_size = actual_screen_size if self.width > actual_screen_size[0] or self.height > actual_screen_size[1] else (self.width, self.height)
        self.w_start_offset = (self.target_size[0] - self.width) // 2
        self.h_start_offset = (self.target_size[1] - self.height) // 2
        self.w_offset = self.w_start_offset
        self.h_offset = self.h_start_offset
        self.max_w_offset = self.target_size[0] - self.width
        self.max_h_offset = self.target_size[1] - self.height
        self.scale = self.target_size[1] // self.cell_size
        print(f"Screen size: {self.width}x{self.height}")
        sdl2.ext.init(joystick=True)
        sdl2.SDL_Init(sdl2.SDL_INIT_AUDIO)
        
        self.window = sdl2.ext.Window(title=title, size=self.target_size,)# flags=sdl2.SDL_WINDOW_FULLSCREEN)
        self.delay = 30
        self.load_joystick()
        self.window.show()
        self.renderer = sdl2.ext.Renderer(self.window, flags=sdl2.SDL_RENDERER_ACCELERATED)
        self.factory = sdl2.ext.SpriteFactory(renderer=self.renderer)
        self.fonts = self.generate_fonts("assets/decterm.ttf")
        self.running = True
        self.key_down, self.key_up, self.axis_scroll = key_callback
        self.performance = 0
        self.audio_devs = {}
        self.button_cursor = [0, 0]
        self.buttons = {}
        self.audio_devs["base"] = sdl2.SDL_OpenAudioDevice(None, 0, SDL_AudioSpec(freq=22050, aformat=sdl2.AUDIO_U8, channels=1, samples=2048), None, 0)
        self.audio_devs["effects"] = sdl2.SDL_OpenAudioDevice(None, 0, SDL_AudioSpec(freq=22050, aformat=sdl2.AUDIO_U8, channels=1, samples=2048), None, 0)
        self.audio_devs["music"] = sdl2.SDL_OpenAudioDevice(None, 0, SDL_AudioSpec(freq=22050, aformat=sdl2.AUDIO_U8, channels=1, samples=2048), None, 0)
    def create_texture(self, tiles: list):
        bg_surface = sdl2.SDL_CreateRGBSurface(0, self.width, self.height, 32, 0, 0, 0, 0)
        for tile in tiles:
            dstrect = sdl2.SDL_Rect(tile[1], tile[2], self.cell_size, self.cell_size)
            sdl2.SDL_BlitSurface(tile[0], None, bg_surface, dstrect)
        bg_texture = self.factory.from_surface(bg_surface)
        sdl2.SDL_FreeSurface(bg_surface)
        return bg_texture

    def load_joystick(self):
        sdl2.SDL_Init(sdl2.SDL_INIT_JOYSTICK)
        sdl2.SDL_JoystickOpen(0)

        
    def generate_fonts(self,font_file):
        fonts = {}
        for i in range(10, 70, 1):
            fonts.update({i: sdl2.ext.FontManager(font_path=font_file, size=i)})
        return fonts
    
    def load_image(self, path, transparent_color=None, surface=False):
        image_path = os.path.join("assets", path)
        image = Image.open(image_path)
        if transparent_color:
            image = image.convert("RGBA")
            datas = image.getdata()
            new_data = []
            for item in datas:
                if item[:3] == transparent_color:
                    new_data.append((255, 255, 255, 0))
                else:
                    new_data.append(item)
            image.putdata(new_data)
        scale = self.cell_size // 20
        if surface:
            return sdl2.ext.pillow_to_surface(image.resize((image.width * scale, image.height * scale), Image.NEAREST))
        image = image.resize((image.width * scale, image.height * scale), Image.NEAREST)
        return self.factory.from_surface(sdl2.ext.pillow_to_surface(image))
    
    def draw_text(self, text, font, position, color):
        sprite = self.factory.from_text(text, color=color, fontmanager=font)
        if position == "center":
            position = ("center", "center")
        if position[0] == "center":
            position = (self.target_size[0] // 2 - sprite.size[0] // 2, position[1])
        if position[1] == "center":
            position = (position[0], self.target_size[1] // 2 - sprite.size[1] // 2)
        sprite.position = position
        print(sprite.position)
        self.renderer.copy(sprite, dstrect=sprite.position)
        
    def draw_background(self, bg_texture):
        self.renderer.copy(bg_texture, dstrect=sdl2.SDL_Rect(self.w_offset, self.h_offset, self.width, self.height))
    def draw_image(self, x, y, sprite, tag, anchor="nw"):
        if not self.is_in_visible_area(x, y):
            return
        sprite.position = (x+self.w_offset, y+self.h_offset)
        self.renderer.copy(sprite, dstrect=sprite.position)
        
        
    def draw_rectangle(self, x, y, width, height, tag, outline="red", filling=None):
        if filling:
            self.renderer.fill((x, y, width, height), sdl2.ext.Color(*filling))
        else:
            self.renderer.draw_rect((x, y, width, height), sdl2.ext.Color(*outline))
        
    def draw_pointer(self, x, y):
        x=x+self.w_offset
        y=y+self.h_offset
        for i in range(3):
            self.renderer.draw_rect((x + i,y+i, self.cell_size-2*i, self.cell_size-2*i), color=sdl2.ext.Color(255, 0, 0))
        
    def delete_tag(self, tag):
        pass
    
    def dialog(self, text, **kwargs):
        self.draw_rectangle(50, 50,
                            self.target_size[0] - 100, self.target_size[1] - 100, "win", filling=(255, 255, 255))
        self.draw_text(text, self.fonts[self.target_size[1]//20], "center", sdl2.ext.Color(0, 0, 0))
        if subtitle := kwargs.get("subtitle"):
            self.draw_text(subtitle, self.fonts[self.target_size[1]//30], ("center", self.target_size[1] // 2 + 50), sdl2.ext.Color(0, 0, 0))
        if image := kwargs.get("image"):
            image_size = self.get_image_size(image)
            self.draw_image(self.target_size[0] // 2 - image_size[0] // 2 - self.w_offset,
                            self.target_size[1] // 2 - image_size[1] * 2 - self.h_offset,
                            image, "win")
        if scores := kwargs.get("scores"):
            #self.draw_text("Scores:", self.fonts[self.target_size[1]//20], (self.target_size[0] // 2 - 50, self.target_size[1] // 2 + 50), sdl2.ext.Color(0, 0, 0))
            sprite = self.factory.from_text("Scores:", color=sdl2.ext.Color(0, 0, 0), fontmanager=self.fonts[self.target_size[1]//20])
            sprite.position = (self.target_size[0] // 2 - 50, self.target_size[1] // 2 + 30)
            self.renderer.copy(sprite, dstrect=sprite.position)
            for i, score in enumerate(scores[:5]):
                score = " - ".join(score)
                self.draw_text(score, self.fonts[self.target_size[1]//40], ("center", self.target_size[1] // 2 + 50 + 30 * (i + 1)), sdl2.ext.Color(0, 0, 0))

    def get_image_size(self, image):
        return image.size
    
    def update_status(self, text):
        fps = int(1000 / self.performance) if self.performance != 0 else 0
        text = f"FPS: {fps} - {text}"
        font = self.fonts[20]
        sprite = self.factory.from_text(text, color=sdl2.ext.Color(0, 0, 0), fontmanager=font)
        text_width, text_height = sprite.size
        self.renderer.fill((3, 3, text_width + 10, text_height + 4), sdl2.ext.Color(255, 255, 255))
        self.draw_text(text, font, (8, 5), sdl2.ext.Color(0, 0, 0))
        
    def new_cycle(self, delay, callback):
        pass
    
    def full_screen(self,flag):
        sdl2.SDL_SetWindowFullscreen(self.window.window, flag)
        
    def is_in_visible_area(self, x, y):
        return -self.w_offset -self.cell_size <= x <= self.width - self.w_offset and -self.h_offset -self.cell_size <= y <= self.height - self.h_offset
    
    def get_perf_counter(self):
        return sdl2.SDL_GetPerformanceCounter()
    
    def mainloop(self, **kwargs):
        while self.running:
            performance_start = sdl2.SDL_GetPerformanceCounter()
            self.renderer.clear()
            if "bg_update" in kwargs:
                kwargs["bg_update"]()
            kwargs["update"]()
            events = sdl2.ext.get_events()
            for event in events:
                if event.type == sdl2.SDL_QUIT:
                    self.running = False
                elif event.type == sdl2.SDL_KEYDOWN and self.key_down:
                    key = sdl2.SDL_GetKeyName(event.key.keysym.sym).decode('utf-8')
                    self.key_down(key)
                elif event.type == sdl2.SDL_KEYUP and self.key_down:
                    key = sdl2.SDL_GetKeyName(event.key.keysym.sym).decode('utf-8')
                    self.key_up(key)
                    print(key)
                elif event.type == sdl2.SDL_MOUSEMOTION:
                    self.key_down("mouse", coords=(event.motion.x, event.motion.y))
                elif event.type == sdl2.SDL_JOYBUTTONDOWN:
                    key = event.jbutton.button
                    self.key_down(key)
                elif event.type == sdl2.SDL_JOYBUTTONUP:
                    key = event.jbutton.button
                    self.key_up(key)
                # elif event.type == sdl2.SDL_JOYAXISMOTION:
                #     self.axis_scroll(event.jaxis.axis, event.jaxis.value)
                    
                    
            # Disegna qui gli sprite
            #rect = sdl2.SDL_Rect(self.w_offset, self.h_offset, self.target_size[0], self.target_size[1])
            #sdl2.SDL_RenderSetClipRect(self.renderer.sdlrenderer, rect)
            self.renderer.present()
            self.performance = (sdl2.SDL_GetPerformanceCounter() - performance_start) / sdl2.SDL_GetPerformanceFrequency() * 1000
            if self.performance < self.delay:
                delay = self.delay - round(self.performance)
            else:
                delay = 0
            sdl2.SDL_Delay(delay)
            
    def close(self):
        self.running = False
        sdl2.ext.quit()

    def scroll_view(self, pointer):
        """
        Adjusts the view offset based on the given pointer coordinates.
        Scales them down by half, then adjusts offsets, ensuring they don't
        exceed maximum allowed values.
        """
        x, y = pointer
        
        # Scale down and invert
        x = -(x // 2) * self.cell_size
        y = -(y // 2) * self.cell_size
        
        # Clamp horizontal offset
        if x <= self.max_w_offset + self.cell_size:
            x = self.max_w_offset
        

        # Clamp vertical offset
        if y < self.max_h_offset:
            y = self.max_h_offset

        self.w_offset = x
        self.h_offset = y
        
    def play_sound(self, sound_file, tag="base"):
        sound_file = os.path.join("sound", sound_file)
        rw = sdl2.SDL_RWFromFile(byteify(sound_file, "utf-8"), b"rb")
        if not rw:
            raise RuntimeError("Failed to open sound file")

        _buf = POINTER(sdl2.Uint8)()
        _length = sdl2.Uint32()

        spec = SDL_AudioSpec(freq=22050, aformat=sdl2.AUDIO_U8, channels=1, samples=2048)
        if sdl2.SDL_LoadWAV_RW(rw, 1, byref(spec), byref(_buf), byref(_length)) == None:
            raise RuntimeError("Failed to load WAV")
        devid = self.audio_devs[tag]
        # Clear any queued audio
        sdl2.SDL_ClearQueuedAudio(devid)

        # Start playing audio
        sdl2.SDL_QueueAudio(devid, _buf, _length)
        sdl2.SDL_PauseAudioDevice(devid, 0)
        
    def stop_sound(self):
        for dev in self.audio_devs:
            if not dev[0]:
                sdl2.SDL_PauseAudioDevice(dev[1], 1)
                sdl2.SDL_ClearQueuedAudio(dev[1])
                
    def start_dialog(self, **kwargs):
        self.dialog("Welcome to the Mice!", subtitle="A game by Matteo because was bored", **kwargs)
        center = self.get_view_center()
        #self.draw_button(center[0], center[1] + 10 * self.scale, "Start", 120, 50, (0, 0))
        
    def draw_button(self, x, y, text, width, height, coords):
        if self.button_cursor[0] == coords[0] and self.button_cursor[1] == coords[1]:
            color = (0, 0, 255)
        self.draw_rectangle(x, y, width, height, "button", outline8u=color)
        self.draw_text(text, self.fonts[20], (x + 10, y + 10), (0,0,0))

    def get_view_center(self):
        return self.w_offset + self.width // 2, self.h_offset + self.height // 2