import os
import sdl2
import sdl2.ext
from ctypes import *

class SDL2Gui:

    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 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
        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 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 update_status(self, 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 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