#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <wiringPi.h> 

#include "lcd_2inch.h"
#include "lcd_gui.h"
#include "fonts.h"
#include "touch.h"

#define TOOLBAR_WIDTH 60
#define LCD_W_1   320 
#define LCD_H_1   240 
#define BTN_H     40 

#ifndef YELLOW
#define YELLOW  0xFFE0
#endif

// Global variables
u_int16_t *image_buffer;
u_int16_t brush_color = BLACK;
u_int16_t brush_size = 3;      

/**
 * Coordinate conversion: Convert touch coordinates (320x240) to physical display coordinates (240x320)
 * This handles the orientation difference between touch panel and LCD
 */
void Logic_To_Phys(u_int16_t lx, u_int16_t ly, u_int16_t *px, u_int16_t *py) {
    if (ly >= 240) ly = 239; 
    *px = 240 - ly - 1;
    *py = lx;
}

/**
 * Draw toolbar button
 */
void Draw_Button(int index, u_int16_t bg_color, const char* text, u_int16_t text_color) {
    u_int16_t x_start = 2;
    u_int16_t y_start = index * BTN_H + 2;
    u_int16_t x_end   = TOOLBAR_WIDTH - 2;
    u_int16_t y_end   = (index + 1) * BTN_H - 2;

    Gui_draw_rectangle(x_start, y_start, x_end, y_end, bg_color, 1, FULL);

    if (brush_color == bg_color && (text == NULL || strcmp(text, "CLR") != 0)) {
        u_int16_t border = (bg_color == WHITE) ? RED : WHITE;
        Gui_draw_rectangle(x_start + 2, y_start + 2, x_end - 2, y_end - 2, border, 2, EMPTY);
    }
    if (text != NULL && strlen(text) > 0) {
        Gui_draw_str(x_start + 5, y_start + 12, text, &Font24, text_color, bg_color);
    }
}

/**
 * Draw complete toolbar to memory buffer
 */
void Draw_Toolbar() {
    Gui_draw_line(TOOLBAR_WIDTH, 0, TOOLBAR_WIDTH, LCD_H_1, BLACK, 2, SOLID);
    Draw_Button(0, WHITE, "CLR", BLACK);
    Draw_Button(1, RED, NULL, WHITE);
    Draw_Button(2, GREEN, NULL, WHITE);
    Draw_Button(3, BLUE, NULL, WHITE);
    Draw_Button(4, BLACK, NULL, WHITE);
    Draw_Button(5, YELLOW, NULL, BLACK);
}

/**
 *Handle toolbar touch events
 */
int Handle_Toolbar_Click(u_int16_t y) {
    u_int16_t old_color = brush_color;
    int btn_index = y / BTN_H;   
    switch (btn_index) {
        case 0: Gui_Clear(WHITE); return 2;    // Clear screen action
        case 1: brush_color = RED; break;
        case 2: brush_color = GREEN; break;
        case 3: brush_color = BLUE; break;
        case 4: brush_color = BLACK; break;
        case 5: brush_color = YELLOW; break;
        default: return 0;
    }
    if (brush_color != old_color) return 1;   // Color changed
    return 0;
}

int main(void)
{
    TouchPoint tPoint;
    u_int16_t px, py; 

    // Allocate memory for image buffer (LCD_SIZE = LCD_W * LCD_H * 2 bytes for RGB565)
    image_buffer = (u_int16_t *)malloc(LCD_SIZE);
    if(image_buffer == NULL) return -1;

    Lcd_Init();     
    Touch_Init();    // Initialize touch controller
    
    Image_Init(image_buffer, LCD_W, LCD_H, ROTATE_90, MIRROR_NONE);
    
        Gui_Clear(WHITE);
	Gui_draw_point(20, 200, YELLOW, 18);
	Gui_draw_line(310, 175, 180, 190, BLUE, 1, SOLID);
	Gui_draw_line(310, 110, 230, 10, BLUE, 2, DOTTED);
	Gui_draw_line(180, 195, 310, 215, RED, 2, DOTTED);
	Gui_draw_rectangle(200, 210, 230, 235, BLUE, 1, EMPTY);
	Gui_draw_rectangle(50, 200, 70, 230, YELLOW, 1, FULL);
	Gui_draw_circle(110, 210, 20, RED,1, EMPTY);
	Gui_draw_circle(170, 220, 15, RED,1, FULL);
	Gui_draw_str(10, 30, "876543210!", &Font24, BLACK, WHITE);
	Gui_draw_str(10, 80, "seengreat!", &Font24, BLUE, WHITE);
	Gui_draw_str(10, 120, "SEENGREAT!", &Font24, GREEN, WHITE);
	Display(Image.img);
	delay(3000);
	
	Gui_Clear(WHITE);	
	Gui_show_bmp("./fruits8.bmp");
	Display(Image.img);
    delay(3000);
    
    // Initial Screen
    Gui_Clear(WHITE);
    Draw_Toolbar();
    Display(image_buffer); 

    printf("Paint App Ready (WiringPi Version).\n");

    while(1)
    {
        if(Touch_Read_Point(&tPoint))    // Check for touch input
        {
            if (tPoint.x < TOOLBAR_WIDTH) {
                int status = Handle_Toolbar_Click(tPoint.y);
                if (status > 0) {
                    Draw_Toolbar();
                    Display(image_buffer); 
                    delay(status == 2 ? 200 : 50); 
                }
            } 
            else {
                
                Logic_To_Phys(tPoint.x, tPoint.y, &px, &py);
                Gui_draw_point(tPoint.x, tPoint.y, brush_color, brush_size);
                LCD_Write_Block(px, py, brush_size, brush_size, brush_color); 
                
            }
        }

    }
    
    free(image_buffer);
    return 0;
}
