#include "lcd_touch.h"
#include "lcd_gui.h"

// === UI layout definitions ===
#define MENU_W      50      // Left menu width
#define BTN_H       40      // Button height
#define BTN_W       40      // Button width
#define BTN_X       5       // Button X coordinate

// Button Y positions
#define Y_BTN_CLR   10
#define Y_BTN_RED   50
#define Y_BTN_GRN   90
#define Y_BTN_BLU   130
#define Y_BTN_BLK   170
#define Y_BTN_YEL   210
tp_dev_t tp_dev;  

// I2C write register
void I2C_Write(uint8_t reg, uint8_t data) {
    Wire.beginTransmission(CST816S_ADDR);
    Wire.write(reg);
    Wire.write(data);
    Wire.endTransmission();
}

// I2C read register
uint8_t I2C_Read(uint8_t reg) {
    Wire.beginTransmission(CST816S_ADDR);
    Wire.write(reg);
    Wire.endTransmission();
    Wire.requestFrom(CST816S_ADDR, 1);
    return Wire.read();
}

void TP_Init(void)
{
    // Initialize I2C
    Wire.begin();
    
    // Initialize pins
    pinMode(TP_RST, OUTPUT);
    pinMode(TP_INT, INPUT);

    // Reset touch controller
    TP_RST_0;
    delay(10);
    TP_RST_1;
    delay(50);

    tp_dev.paint_color = BLACK;
    tp_dev.statu = 0;
}

// Scan touch screen for touch events
unsigned char TP_Scan(unsigned char mode)
{
    uint8_t i2c_data[6];
    uint8_t touch_cnt;

    Wire.beginTransmission(CST816S_ADDR);
    Wire.write(0x02); 
    Wire.endTransmission();
    
    Wire.requestFrom(CST816S_ADDR, 5); 
    
    // Read touch data
    if (Wire.available()) {
        touch_cnt = Wire.read();  // 0x02
        uint8_t xh = Wire.read(); // 0x03
        uint8_t xl = Wire.read(); // 0x04
        uint8_t yh = Wire.read(); // 0x05
        uint8_t yl = Wire.read(); // 0x06
        
        // Check if any touch point detected
        if (touch_cnt > 0 && touch_cnt < 2) {  // Single touch

             uint16_t phys_x=((xh & 0x0F) << 8) | xl;
             uint16_t phys_y=((yh & 0x0F) << 8) | yl; 

             tp_dev.x[0] = phys_y;
             tp_dev.y[0] = phys_x;
             
             tp_dev.statu |= TP_PRESS_DOWN;
        } else {
             // Mark release
             tp_dev.statu &= ~TP_PRESS_DOWN;
        }
    }
    return (tp_dev.statu & TP_PRESS_DOWN);
}

// Load drawing UI with color buttons
void Load_Touch_Draw(void)
{
    Gui_fill_color(0, 0, MENU_W, 240, 0xE71C); // Light gray background
    Gui_fill_color(BTN_X, Y_BTN_CLR, BTN_X+BTN_W, Y_BTN_CLR+30, WHITE); // Clear button
    Gui_draw_str(BTN_X+5, Y_BTN_CLR+8, "CLR", &Font16, BLACK, WHITE);
    Gui_fill_color(BTN_X, Y_BTN_RED, BTN_X+BTN_W, Y_BTN_RED+30, RED);
    Gui_fill_color(BTN_X, Y_BTN_GRN, BTN_X+BTN_W, Y_BTN_GRN+30, GREEN);
    Gui_fill_color(BTN_X, Y_BTN_BLU, BTN_X+BTN_W, Y_BTN_BLU+30, BLUE);
    Gui_fill_color(BTN_X, Y_BTN_BLK, BTN_X+BTN_W, Y_BTN_BLK+30, BLACK);
    Gui_fill_color(BTN_X, Y_BTN_YEL, BTN_X+BTN_W, Y_BTN_YEL+30, YELLOW);
    Gui_draw_line(MENU_W, 0, MENU_W, 240, BLACK, 2, SOLID);
}

// Main touch test loop
void TP_test(void)
{
   uint16_t paint_width = 4;
   
      TP_Scan(0);
      if(tp_dev.statu & TP_PRESS_DOWN)  // Detect press
      {
         // Check if touch is within menu area
         if(tp_dev.x[0] < MENU_W) 
         {
             delay(100);  // Debounce
             if(tp_dev.y[0]>Y_BTN_CLR && tp_dev.y[0]<Y_BTN_CLR+35 )
             {
              Gui_fill_color(MENU_W+1, 0, 320, 240, WHITE);  // Clear drawing area
             }
             else if(tp_dev.y[0]>Y_BTN_RED && tp_dev.y[0]<Y_BTN_RED+35 ) tp_dev.paint_color = RED;
             else if(tp_dev.y[0]>Y_BTN_GRN && tp_dev.y[0]<Y_BTN_GRN+35 ) tp_dev.paint_color = GREEN;
             else if(tp_dev.y[0]>Y_BTN_BLU && tp_dev.y[0]<Y_BTN_BLU+35 ) tp_dev.paint_color = BLUE;
             else if(tp_dev.y[0]>Y_BTN_BLK && tp_dev.y[0]<Y_BTN_BLK+35 ) tp_dev.paint_color = BLACK;
             else if(tp_dev.y[0]>Y_BTN_YEL && tp_dev.y[0]<Y_BTN_YEL+35 ) tp_dev.paint_color = YELLOW;
         }
         else  // Drawing area
             {
                Gui_draw_point(tp_dev.x[0], tp_dev.y[0], tp_dev.paint_color, paint_width);  
             }          
      }

}
