use nalgebra_glm::Vec2;
use winit::keyboard::KeyCode;

use crate::ecs::world::World;

use super::InteractionSnapshot;
use super::text_cursor::{
    byte_index_at_x, line_col_from_char_position, line_count, line_start_char_index, line_text,
    measure_text_width,
};
use super::text_edit::EditBuffer;
use crate::ecs::ui::components::{CharStyle, TextSnapshot};

use crate::prelude::*;
pub(super) fn handle_text_area(
    world: &mut World,
    entity: freecs::Entity,
    interaction: &InteractionSnapshot,
    data: &crate::ecs::ui::components::UiTextAreaData,
    ctx: &super::TextEditContext<'_>,
) {
    let focused_entity = ctx.focused_entity;
    let frame_chars = ctx.frame_chars;
    let frame_keys = ctx.frame_keys;
    let ctrl_held = ctx.ctrl_held;
    let shift_held = ctx.shift_held;
    let mouse_position = ctx.mouse_position;
    let current_time = ctx.current_time;
    let dpi_scale = ctx.dpi_scale;
    let is_focused = focused_entity == Some(entity);
    let mut changed = false;
    let mut cursor_blink_timer = data.cursor_blink_timer;
    let mut scroll_offset_y = data.scroll_offset_y;
    let mut clear_focus = false;
    let line_height = data.line_height;
    let mut undo_stack = data.undo_stack.clone();
    let max_length = data.max_length;

    let mut buffer = EditBuffer::new(
        data.text.clone(),
        data.cursor_position,
        data.selection_start,
        true,
    );

    if is_focused {
        let mut needs_snapshot = false;
        for character in frame_chars {
            if *character >= ' ' {
                if let Some(max) = max_length {
                    let removed = buffer
                        .selection_range()
                        .map(|(min, max_sel)| max_sel - min)
                        .unwrap_or(0);
                    if buffer.length() - removed + 1 > max {
                        continue;
                    }
                }
                if !needs_snapshot {
                    undo_stack.push_initial(snapshot(&buffer));
                    needs_snapshot = true;
                }
                buffer.insert_char(*character, CharStyle::default());
                changed = true;
                cursor_blink_timer = current_time;
            }
        }

        if needs_snapshot {
            undo_stack.push(snapshot(&buffer), current_time);
        }

        for (key, is_pressed) in frame_keys {
            if !is_pressed {
                continue;
            }
            match key {
                KeyCode::Backspace => {
                    undo_stack.push_initial(snapshot(&buffer));
                    if buffer.backspace(ctrl_held) {
                        undo_stack.push(snapshot(&buffer), current_time);
                        changed = true;
                    }
                    cursor_blink_timer = current_time;
                }
                KeyCode::Delete => {
                    undo_stack.push_initial(snapshot(&buffer));
                    if buffer.delete_forward(ctrl_held) {
                        undo_stack.push(snapshot(&buffer), current_time);
                        changed = true;
                    }
                    cursor_blink_timer = current_time;
                }
                KeyCode::Enter => {
                    if ctrl_held {
                        clear_focus = true;
                    } else {
                        undo_stack.push_initial(snapshot(&buffer));
                        buffer.insert_newline(CharStyle::default());
                        undo_stack.push(snapshot(&buffer), current_time);
                        changed = true;
                        cursor_blink_timer = current_time;
                    }
                }
                KeyCode::ArrowLeft => {
                    buffer.move_left(ctrl_held, shift_held);
                    cursor_blink_timer = current_time;
                }
                KeyCode::ArrowRight => {
                    buffer.move_right(ctrl_held, shift_held);
                    cursor_blink_timer = current_time;
                }
                KeyCode::ArrowUp => {
                    buffer.move_up(shift_held);
                    cursor_blink_timer = current_time;
                }
                KeyCode::ArrowDown => {
                    buffer.move_down(shift_held);
                    cursor_blink_timer = current_time;
                }
                KeyCode::Home => {
                    buffer.move_home(shift_held);
                    cursor_blink_timer = current_time;
                }
                KeyCode::End => {
                    buffer.move_end(shift_held);
                    cursor_blink_timer = current_time;
                }
                KeyCode::KeyA if ctrl_held => {
                    buffer.select_all();
                    cursor_blink_timer = current_time;
                }
                KeyCode::KeyZ if ctrl_held => {
                    if let Some(restored) = undo_stack.undo() {
                        buffer.text = restored.text.clone();
                        buffer.cursor = restored.cursor_position;
                        buffer.selection = restored.selection_start;
                        changed = true;
                        cursor_blink_timer = current_time;
                    }
                }
                KeyCode::KeyY if ctrl_held => {
                    if let Some(restored) = undo_stack.redo() {
                        buffer.text = restored.text.clone();
                        buffer.cursor = restored.cursor_position;
                        buffer.selection = restored.selection_start;
                        changed = true;
                        cursor_blink_timer = current_time;
                    }
                }
                KeyCode::KeyC if ctrl_held => {
                    if let Some(selected) = buffer.selected_text() {
                        ui_set_clipboard_text(world, selected);
                    }
                }
                KeyCode::KeyX if ctrl_held => {
                    if let Some(selected) = buffer.selected_text() {
                        ui_set_clipboard_text(world, selected);
                        undo_stack.push_initial(snapshot(&buffer));
                        buffer.delete_selection();
                        changed = true;
                        undo_stack.push(snapshot(&buffer), current_time);
                    }
                }
                KeyCode::KeyV if ctrl_held => {
                    let mut paste_text = ui_read_system_clipboard(world);
                    if !paste_text.is_empty() {
                        if let Some(max) = max_length {
                            let removed = buffer
                                .selection_range()
                                .map(|(min, max_sel)| max_sel - min)
                                .unwrap_or(0);
                            let available = max.saturating_sub(buffer.length() - removed);
                            paste_text = paste_text.chars().take(available).collect();
                        }
                        if !paste_text.is_empty() {
                            undo_stack.push_initial(snapshot(&buffer));
                            buffer.insert_str(&paste_text, CharStyle::default());
                            changed = true;
                            undo_stack.push(snapshot(&buffer), current_time);
                        }
                    }
                }
                KeyCode::Escape => {
                    buffer.selection = None;
                    clear_focus = true;
                }
                _ => {}
            }
        }

        if interaction.clicked
            && let Some(rect) = world.ui.get_ui_layout_node(entity).map(|n| n.computed_rect)
        {
            let font_size = world
                .resources
                .retained_ui
                .theme_state
                .active_theme()
                .font_size;
            let local_y = (mouse_position.y - rect.min.y) / dpi_scale - 8.0 + scroll_offset_y;
            let clicked_line = (local_y / line_height).max(0.0) as usize;
            let total_lines = line_count(&buffer.text);
            let target_line = clicked_line.min(total_lines.saturating_sub(1));
            let target_line_text = line_text(&buffer.text, target_line).to_string();
            let local_x = (mouse_position.x - rect.min.x) / dpi_scale - 8.0;
            let column = byte_index_at_x(
                &mut world.resources.text.font_engine,
                &target_line_text,
                font_size,
                local_x,
            );
            let line_start = line_start_char_index(&buffer.text, target_line);
            if shift_held {
                if buffer.selection.is_none() {
                    buffer.selection = Some(buffer.cursor);
                }
            } else {
                buffer.selection = None;
            }
            buffer.cursor = line_start + column;
            cursor_blink_timer = current_time;
        }

        if interaction.double_clicked {
            buffer.select_word_at_cursor();
        }
    } else {
        buffer.selection = None;
    }

    let text = buffer.text;
    let cursor_position = buffer.cursor;
    let selection_start = buffer.selection;

    if clear_focus {
        world
            .resources
            .retained_ui
            .interaction_for_active_mut()
            .focused_entity = None;
    }

    if changed {
        world.resources.text.cache.set_text(data.text_slot, &text);
    }

    let cursor_visible = is_focused && ((current_time - cursor_blink_timer) % 1.0) < 0.5;
    if let Some(cursor_node) = world.ui.get_ui_layout_node_mut(data.cursor_entity) {
        cursor_node.visible = cursor_visible;
    }

    let (cur_line, cur_col) = line_col_from_char_position(&text, cursor_position);
    let visible_rows = data.visible_rows;
    let cursor_y = cur_line as f32 * line_height;
    let visible_height = visible_rows as f32 * line_height;
    if cursor_y - scroll_offset_y >= visible_height {
        scroll_offset_y = cursor_y - visible_height + line_height;
    } else if cursor_y < scroll_offset_y {
        scroll_offset_y = cursor_y;
    }
    let total_lines = line_count(&text);
    let max_scroll = ((total_lines as f32 * line_height) - visible_height).max(0.0);
    scroll_offset_y = scroll_offset_y.clamp(0.0, max_scroll);

    if world.ui.get_ui_layout_node(entity).is_some() {
        let font_size = world
            .resources
            .retained_ui
            .theme_state
            .active_theme()
            .font_size;
        {
            let cur_line_text = line_text(&text, cur_line).to_string();
            let text_before_cursor: String = cur_line_text.chars().take(cur_col).collect();
            let cursor_x = measure_text_width(
                &mut world.resources.text.font_engine,
                &text_before_cursor,
                font_size,
            );
            let cursor_screen_y = cur_line as f32 * line_height - scroll_offset_y;

            let has_selection =
                is_focused && selection_start.is_some() && selection_start != Some(cursor_position);

            let sel_positions: Vec<(f32, f32, f32)> = if has_selection {
                let sel_start = selection_start.unwrap();
                let sel_min = sel_start.min(cursor_position);
                let sel_max = sel_start.max(cursor_position);
                let (min_line, min_col) = line_col_from_char_position(&text, sel_min);
                let (max_line, max_col) = line_col_from_char_position(&text, sel_max);

                let mut positions = Vec::new();
                for line_idx in min_line..=max_line {
                    let line_segment = line_text(&text, line_idx).to_string();
                    let start_col = if line_idx == min_line { min_col } else { 0 };
                    let end_col = if line_idx == max_line {
                        max_col
                    } else {
                        line_segment.chars().count()
                    };
                    let start_text: String = line_segment.chars().take(start_col).collect();
                    let end_text: String = line_segment.chars().take(end_col).collect();
                    let sx = measure_text_width(
                        &mut world.resources.text.font_engine,
                        &start_text,
                        font_size,
                    );
                    let ex = measure_text_width(
                        &mut world.resources.text.font_engine,
                        &end_text,
                        font_size,
                    );
                    let sy = line_idx as f32 * line_height - scroll_offset_y;
                    positions.push((sx, ex - sx, sy));
                }
                positions
            } else {
                Vec::new()
            };

            if let Some(cursor_node) = world.ui.get_ui_layout_node_mut(data.cursor_entity)
                && let Some(crate::ecs::ui::layout_types::UiLayoutType::Window(window)) =
                    cursor_node.base_layout.as_mut()
            {
                window.position =
                    crate::ecs::ui::units::Ab(Vec2::new(8.0 + cursor_x, 8.0 + cursor_screen_y))
                        .into();
            }

            for (pool_index, sel_entity) in data.selection_pool.iter().enumerate() {
                if pool_index < sel_positions.len() {
                    let (sx, width, sy) = sel_positions[pool_index];
                    if let Some(sel_node) = world.ui.get_ui_layout_node_mut(*sel_entity) {
                        sel_node.visible = true;
                        if let Some(crate::ecs::ui::layout_types::UiLayoutType::Window(window)) =
                            sel_node.base_layout.as_mut()
                        {
                            window.position =
                                crate::ecs::ui::units::Ab(Vec2::new(8.0 + sx, 8.0 + sy)).into();
                            window.size =
                                crate::ecs::ui::units::Ab(Vec2::new(width.max(2.0), line_height))
                                    .into();
                        }
                    }
                } else if let Some(sel_node) = world.ui.get_ui_layout_node_mut(*sel_entity) {
                    sel_node.visible = false;
                }
            }
        }
    }

    if changed {
        world.resources.retained_ui.events_for_active_mut().push(
            crate::ecs::ui::resources::UiEvent::TextAreaChanged {
                entity,
                text: text.clone(),
            },
        );
    }
    if clear_focus {
        world.resources.retained_ui.events_for_active_mut().push(
            crate::ecs::ui::resources::UiEvent::TextInputSubmitted {
                entity,
                text: text.clone(),
            },
        );
    }
    let text_is_empty = text.is_empty();
    let placeholder_entity = if let Some(widget_data) = world.ui.get_ui_text_area_mut(entity) {
        widget_data.text = text;
        widget_data.cursor_position = cursor_position;
        widget_data.selection_start = selection_start;
        widget_data.changed = changed;
        widget_data.cursor_blink_timer = cursor_blink_timer;
        widget_data.scroll_offset_y = scroll_offset_y;
        widget_data.undo_stack = undo_stack;
        widget_data.placeholder_entity
    } else {
        None
    };
    if let Some(ph_entity) = placeholder_entity
        && let Some(node) = world.ui.get_ui_layout_node_mut(ph_entity)
    {
        node.visible = text_is_empty;
    }
}

fn snapshot(buffer: &EditBuffer) -> TextSnapshot {
    TextSnapshot {
        text: buffer.text.clone(),
        cursor_position: buffer.cursor,
        selection_start: buffer.selection,
    }
}
