import arcade, arcade.gui, os, json, numpy as np

from scipy.interpolate import interp1d

from game.base import BaseGame

class Game(BaseGame):
    def __init__(self, pypresence_client):
        super().__init__(pypresence_client, "Fourier Simulator", "fourier_simulator", {
                "max_coefficients": 1000,
                "speed": 1.0,
                "tail_size": 1000,
                "resampling_size": 512
        })

        self.path_points = []
        
        self.drawing_done = False
        
        self.fourier_coefficients = []
        self.drawing_trail = []

        self.time = 0.0

    def on_show_view(self):
        super().on_show_view()

        self.add_setting("Max Coefficients: {value}", 1, 10000, 1, "max_coefficients")
        self.add_setting("Speed: {value}", 0.1, 1.5, 0.1, "speed")
        self.add_setting("Tail Size: {value}", 10, 1000, 10, "tail_size")
        self.add_setting("Resampling Size: {value}", 128, 8192, 128, "resampling_size")

    def on_mouse_press(self, x, y, button, modifiers):
        self.path_points = []
        self.drawing_trail.clear()
        self.drawing_done = False
    
    def on_mouse_release(self, x, y, button, modifiers):
        self.drawing_done = True
        self.calculate_fourier_coefficients()

    def calculate_drawing_point(self, t):
        pos = 0j
        
        for i, c in enumerate(self.fourier_coefficients):
            k = i - (len(self.fourier_coefficients) // 2)
            angle = 2 * np.pi * k * t
            pos += c * np.exp(1j * angle)

        return float(pos.real), float(pos.imag)
    
    def resample_path(self, path_points, N):
        t = np.linspace(0, 1, len(path_points))

        fx = interp1d(t, path_points.real, kind='linear')
        fy = interp1d(t, path_points.imag, kind='linear')

        t_uniform = np.linspace(0, 1, N)

        return fx(t_uniform) + 1j * fy(t_uniform)

    def calculate_fourier_coefficients(self):
        if not self.path_points:
            return
        
        c_points = np.array(self.path_points)
        
        c_points = self.resample_path(c_points, int(self.settings["fourier_simulator"]["resampling_size"]))

        coeffs = np.fft.fftshift(np.fft.fft(c_points) / len(c_points))

        self.fourier_coefficients = np.array(coeffs[:int(self.settings["fourier_simulator"]["max_coefficients"])], dtype=np.complex64)
    
    def on_mouse_drag(self, x, y, dx, dy, _buttons, _modifiers):
        if not self.drawing_done:
            self.path_points.append(complex(x / self.window.width, y / self.window.height))

    def on_update(self, delta_time):
        self.time += delta_time

        if self.drawing_done:
            t_normalized = (self.time * self.settings["fourier_simulator"]["speed"]) % 1.0
            x, y = self.calculate_drawing_point(t_normalized)

            pixel_x = x * self.window.width
            pixel_y = y * self.window.height

            self.drawing_trail.append((pixel_x, pixel_y))

            if len(self.drawing_trail) > self.settings["fourier_simulator"]["tail_size"]:
                self.drawing_trail.pop(0)

    def on_draw(self):
        super().on_draw()

        if len(self.drawing_trail) > 1:
            arcade.draw_line_strip(
                self.drawing_trail,
                color=arcade.color.WHITE,
                line_width=2
            )