#include "input-handle.h"
#include "wayland-runtime.h"
#include <math.h>

void keyboard_key_handle(xkb_keycode_t kc, xkb_keysym_t ks, enum keyboard_key_state state, struct wayland_window *window)
{
    if (ks != XKB_KEY_NoSymbol)
    {
        char buf[64];
        int n = xkb_keysym_to_utf8(ks, buf, sizeof(buf));
       
        // if (n > 0)
        //     printf("keyboard: symbol '%s' (keysym 0x%x) on surface:%p\n", buf, ks, window);
        // else
        //     printf("keyboard: keysym 0x%x (no UTF-8 representation)\n", ks);

        if(state != KEYBOARD_KEY_STATE_RELEASED) 
        {
            switch (ks)
            {
            case XKB_KEY_Return:
                run_window();
                break;
            
            case '1':
                window->draw_info.figure.type = FIGURE_CIRCLE;
                break;
            case '2':
                window->draw_info.figure.type = FIGURE_TRIANGLE;
                break;
            case '3':
                window->draw_info.figure.type = FIGURE_SQUARE;
                break;
            case '-':
                /* decrease animation speed (multiplicative delta, clamped) */
                pthread_mutex_lock(&window->draw_info.figure_mutex);
                window->draw_info.figure.speed -= 1;
                if (window->draw_info.figure.speed < 1)
                    window->draw_info.figure.speed = 1;
                printf("keyboard: speed decreased to %.2f\n", window->draw_info.figure.speed);
                pthread_mutex_unlock(&window->draw_info.figure_mutex);
                break;
            case '+':
            case '=': /* some layouts may emit '=' for unshifted, handle + via keysym */
                /* increase animation speed (multiplicative delta, clamped) */
                pthread_mutex_lock(&window->draw_info.figure_mutex);
                window->draw_info.figure.speed += 1;
                if (window->draw_info.figure.speed > 50.0f)
                    window->draw_info.figure.speed = 50.0f;
                printf("keyboard: speed increased to %.2f\n", window->draw_info.figure.speed);
                pthread_mutex_unlock(&window->draw_info.figure_mutex);
                break;
            case XKB_KEY_Up:
                /* increase figure radius */
                pthread_mutex_lock(&window->draw_info.figure_mutex);
                {
                    float maxr = fminf(window->draw_info.width, window->draw_info.height) / 2.0f - 1.0f;
                    window->draw_info.figure.radius += 2.0f;
                    if (window->draw_info.figure.radius > maxr)
                        window->draw_info.figure.radius = maxr;
                }
                printf("keyboard: radius increased to %.2f\n", window->draw_info.figure.radius);
                pthread_mutex_unlock(&window->draw_info.figure_mutex);
                break;
            case XKB_KEY_Down:
                /* decrease figure radius */
                pthread_mutex_lock(&window->draw_info.figure_mutex);
                {
                    window->draw_info.figure.radius -= 2.0f;
                    if (window->draw_info.figure.radius < 1.0f)
                        window->draw_info.figure.radius = 1.0f;
                }
                printf("keyboard: radius decreased to %.2f\n", window->draw_info.figure.radius);
                pthread_mutex_unlock(&window->draw_info.figure_mutex);
                break;
            default:
                break;
            }
        }
    }
}