#!/bin/bash

# --- TUI Utility Functions (similar to my_tui_lib.sh) ---

# Clear the screen and move cursor to home (top-left)
clear_screen() {
    echo -e "\033[2J\033[H"
}

# Set cursor position (1-based row, col)
set_cursor() {
    local row=$1
    local col=$2
    echo -ne "\033[${row};${col}H" # -n for no newline, -e for escape interpretation
}

# Set text color (e.g., "red", "green")
set_color() {
    local color=$1
    case "$color" in
        "red") echo -ne "\033[31m";;
        "green") echo -ne "\033[32m";;
        "yellow") echo -ne "\033[33m";;
        "blue") echo -ne "\033[34m";;
        "cyan") echo -ne "\033[36m";;
        "magenta") echo -ne "\033[35m";;
        "white") echo -ne "\033[37m";;
        "bold") echo -ne "\033[1m";;
        "underline") echo -ne "\033[4m";;
        "reset") echo -ne "\033[0m";;
        *) echo -ne "\033[0m";; # Default to reset
    esac
}

# Get terminal width
get_term_width() {
    tput cols
}

# Get terminal height
get_term_height() {
    tput lines
}

# Draw a box with Unicode characters
# Args: y x width height title
draw_box() {
    local y=$1
    local x=$2
    local width=$3
    local height=$4
    local title="$5"

    set_cursor "$y" "$x"
    echo -ne "┌$(printf '─%.0s' $(seq 1 $((width-2))))┐"

    for ((i=1; i<height-1; i++)); do
        set_cursor $((y+i)) "$x"
        echo -ne "│$(printf ' %.0s' $(seq 1 $((width-2))))│"
    done

    set_cursor $((y+height-1)) "$x"
    echo -ne "└$(printf '─%.0s' $(seq 1 $((width-2))))┘"

    # Add title
    if [[ -n "$title" ]]; then
        local title_len=${#title}
        local title_start_x=$(( x + (width - title_len) / 2 ))
        set_cursor "$y" "$title_start_x"
        echo -ne "┤ $(set_color bold)${title}$(set_color reset) ├" # Use ┤ and ├ for title on border
    fi
}

# Clear a specific line within a box's content area
# Args: y x width
clear_line_in_box() {
    local y=$1
    local x=$2
    local width=$3
    set_cursor "$y" "$x"
    printf "%*s" "$width" "" # Print spaces to clear the line
}

# --- Global Variables ---
declare -a display_lines # Array to hold lines in the display box
input_buffer=""          # Current text in the input field

# --- Box Definitions (Adjust these as needed) ---
TERM_WIDTH=$(get_term_width)
TERM_HEIGHT=$(get_term_height)

CHANNEL_WIDTH=20

# Input Box
INPUT_BOX_HEIGHT=3
INPUT_BOX_WIDTH=$((TERM_WIDTH - CHANNEL_WIDTH - 8)) # 2 chars padding on each side
INPUT_BOX_Y=$((TERM_HEIGHT - INPUT_BOX_HEIGHT - 1)) # 1 char from bottom
INPUT_BOX_X=$((CHANNEL_WIDTH+4))
INPUT_CONTENT_Y=$((INPUT_BOX_Y + 1))
INPUT_CONTENT_X=$((INPUT_BOX_X + 1))
INPUT_MAX_LEN=$((INPUT_BOX_WIDTH - 2)) # Max chars inside input box

# Display Box
DISPLAY_BOX_HEIGHT=$((TERM_HEIGHT - INPUT_BOX_HEIGHT - 4)) # Space for input box, labels, and padding
DISPLAY_BOX_WIDTH=$((TERM_WIDTH - CHANNEL_WIDTH - 8))
DISPLAY_BOX_Y=1
DISPLAY_BOX_X=$((CHANNEL_WIDTH+4))
DISPLAY_CONTENT_START_Y=$((DISPLAY_BOX_Y + 1))
DISPLAY_CONTENT_START_X=$((DISPLAY_BOX_X + 1))
DISPLAY_CONTENT_WIDTH=$((DISPLAY_BOX_WIDTH - 2))
DISPLAY_MAX_LINES=$((DISPLAY_BOX_HEIGHT - 2))

# --- Core Logic Functions ---

# Function to wrap text into lines based on a given width
wrap_text() {
    local text="$1"
    local width=$2
    local wrapped_output=""
    local current_line=""

    for (( i=0; i<${#text}; i++ )); do
        local char="${text:$i:1}"
        if (( ${#current_line} < width )); then
            current_line+="$char"
        else
            wrapped_output+="$current_line\n"
            current_line="$char"
        fi
    done
    wrapped_output+="$current_line" # Add the last line
    echo "$wrapped_output"
}

# Redraws the content of the display box
redraw_display_box_content() {
    # Clear the content area first
    for ((i=0; i<DISPLAY_MAX_LINES; i++)); do
        clear_line_in_box $((DISPLAY_CONTENT_START_Y + i)) "$DISPLAY_CONTENT_START_X" "$DISPLAY_CONTENT_WIDTH"
    done

    # Print lines from the display_lines array, handling scrolling
    local start_index=0
    if (( ${#display_lines[@]} > DISPLAY_MAX_LINES )); then
        start_index=$(( ${#display_lines[@]} - DISPLAY_MAX_LINES ))
    fi

    local current_display_y=$DISPLAY_CONTENT_START_Y
    for ((i=start_index; i<${#display_lines[@]}; i++)); do
        set_cursor "$current_display_y" "$DISPLAY_CONTENT_START_X"
        echo -ne "${display_lines[i]}"
        current_display_y=$((current_display_y + 1))
    done
}

# --- Main Application Loop ---

# Function to restore terminal settings on exit
cleanup() {
    set_color reset
    stty echo -raw # Restore original terminal settings
    clear_screen
}

# Set trap to run cleanup function on script exit (Ctrl+C, etc.)
trap cleanup EXIT

main() {
	clear_screen

    # Draw the boxes
    draw_box "$DISPLAY_BOX_Y" "$DISPLAY_BOX_X" "$DISPLAY_BOX_WIDTH" "$DISPLAY_BOX_HEIGHT" "#general"
    draw_box "$INPUT_BOX_Y" "$INPUT_BOX_X" "$INPUT_BOX_WIDTH" "$INPUT_BOX_HEIGHT" ""

    local char
    set_cursor "$INPUT_CONTENT_Y" "$INPUT_CONTENT_X"
    while true; do
		read -n 1 char
	done
}

# Run the main function
main
