1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
use std::{collections::HashMap, io::stdout};

use crossterm::{cursor::MoveTo, execute};
use dioxus_html::{input_data::keyboard_types::Key, KeyboardData, MouseData};
use dioxus_native_core::{
    custom_element::CustomElement,
    node::OwnedAttributeDiscription,
    node_ref::AttributeMask,
    prelude::{ElementNode, NodeType},
    real_dom::{ElementNodeMut, NodeImmutable, NodeMut, NodeTypeMut, RealDom},
    utils::cursor::{Cursor, Pos},
    NodeId,
};
use shipyard::UniqueView;
use taffy::geometry::Point;

use crate::{query::get_layout, Event, EventData, FormData, Query};

use super::{RinkWidget, WidgetContext};

pub(crate) trait TextLikeController {
    fn display_text(&self, text: &str) -> String {
        text.to_string()
    }
}

#[derive(Debug, Default)]
pub(crate) struct EmptyController;

impl TextLikeController for EmptyController {}

#[derive(Debug, Default)]
pub(crate) struct TextLike<C: TextLikeController = EmptyController> {
    text: String,
    div_wrapper: NodeId,
    pre_cursor_text: NodeId,
    highlighted_text: NodeId,
    post_cursor_text: NodeId,
    cursor: Cursor,
    dragging: bool,
    border: bool,
    max_len: Option<usize>,
    controller: C,
}

impl<C: TextLikeController> TextLike<C> {
    fn width(el: &ElementNodeMut) -> String {
        if let Some(value) = el
            .get_attribute(&OwnedAttributeDiscription {
                name: "width".to_string(),
                namespace: Some("style".to_string()),
            })
            .and_then(|value| value.as_text())
            .map(|value| value.to_string())
        {
            value
        } else {
            "1px".to_string()
        }
    }

    fn height(el: &ElementNodeMut) -> String {
        if let Some(value) = el
            .get_attribute(&OwnedAttributeDiscription {
                name: "height".to_string(),
                namespace: Some("style".to_string()),
            })
            .and_then(|value| value.as_text())
            .map(|value| value.to_string())
        {
            value
        } else {
            "1px".to_string()
        }
    }

    fn update_max_width_attr(&mut self, el: &ElementNodeMut) {
        if let Some(value) = el
            .get_attribute(&OwnedAttributeDiscription {
                name: "maxlength".to_string(),
                namespace: None,
            })
            .and_then(|value| value.as_text())
            .map(|value| value.to_string())
        {
            if let Ok(max_len) = value.parse::<usize>() {
                self.max_len = Some(max_len);
            }
        }
    }

    fn update_size_attr(&mut self, el: &mut ElementNodeMut) {
        let width = Self::width(el);
        let height = Self::height(el);
        let single_char = width
            .strip_prefix("px")
            .and_then(|n| n.parse::<u32>().ok().filter(|num| *num > 3))
            .is_some()
            || height
                .strip_prefix("px")
                .and_then(|n| n.parse::<u32>().ok().filter(|num| *num > 3))
                .is_some();
        self.border = !single_char;
        let border_style = if self.border { "solid" } else { "none" };
        el.set_attribute(
            OwnedAttributeDiscription {
                name: "border-style".to_string(),
                namespace: Some("style".to_string()),
            },
            border_style.to_string(),
        );
    }

    fn update_value_attr(&mut self, el: &ElementNodeMut) {
        if let Some(value) = el
            .get_attribute(&OwnedAttributeDiscription {
                name: "value".to_string(),
                namespace: None,
            })
            .and_then(|value| value.as_text())
            .map(|value| value.to_string())
        {
            self.text = value;
        }
    }

    pub(crate) fn set_text(&mut self, text: String, rdom: &mut RealDom, id: NodeId) {
        self.text = text;
        self.write_value(rdom, id);
    }

    pub(crate) fn text(&self) -> &str {
        self.text.as_str()
    }

    fn write_value(&self, rdom: &mut RealDom, id: NodeId) {
        let start_highlight = self.cursor.first().idx(self.text.as_str());
        let end_highlight = self.cursor.last().idx(self.text.as_str());
        let (text_before_first_cursor, text_after_first_cursor) =
            self.text.split_at(start_highlight);
        let (text_highlighted, text_after_second_cursor) =
            text_after_first_cursor.split_at(end_highlight - start_highlight);

        if let Some(mut text) = rdom.get_mut(self.pre_cursor_text) {
            let node_type = text.node_type_mut();
            let NodeTypeMut::Text(mut text) = node_type else {
                panic!("input must be an element")
            };
            *text.text_mut() = self.controller.display_text(text_before_first_cursor);
        }

        if let Some(mut text) = rdom.get_mut(self.highlighted_text) {
            let node_type = text.node_type_mut();
            let NodeTypeMut::Text(mut text) = node_type else {
                panic!("input must be an element")
            };
            *text.text_mut() = self.controller.display_text(text_highlighted);
        }

        if let Some(mut text) = rdom.get_mut(self.post_cursor_text) {
            let node_type = text.node_type_mut();
            let NodeTypeMut::Text(mut text) = node_type else {
                panic!("input must be an element")
            };
            *text.text_mut() = self.controller.display_text(text_after_second_cursor);
        }

        // send the event
        {
            let world = rdom.raw_world_mut();
            let data: FormData = FormData {
                value: self.text.clone(),
                values: HashMap::new(),
                files: None,
            };
            let ctx: UniqueView<WidgetContext> = world.borrow().expect("expected widget context");

            ctx.send(Event {
                id,
                name: "input",
                data: EventData::Form(data),
                bubbles: true,
            });
        }
    }

    fn handle_keydown(&mut self, mut root: NodeMut, data: &KeyboardData) {
        let key = data.key();
        let modifiers = data.modifiers();
        let code = data.code();

        if key == Key::Enter {
            return;
        }
        self.cursor.handle_input(
            &code,
            &key,
            &modifiers,
            &mut self.text,
            self.max_len.unwrap_or(1000),
        );

        let id = root.id();

        let rdom = root.real_dom_mut();
        self.write_value(rdom, id);
        let world = rdom.raw_world_mut();

        // move cursor to new position
        let taffy = {
            let query: UniqueView<Query> = world.borrow().unwrap();
            query.stretch.clone()
        };

        let taffy = taffy.lock().unwrap();

        let layout = get_layout(rdom.get(self.div_wrapper).unwrap(), &taffy).unwrap();
        let Point { x, y } = layout.location;

        let Pos { col, row } = self.cursor.start;
        let (x, y) = (col as u16 + x as u16, row as u16 + y as u16);
        if let Ok(pos) = crossterm::cursor::position() {
            if pos != (x, y) {
                execute!(stdout(), MoveTo(x, y)).unwrap();
            }
        } else {
            execute!(stdout(), MoveTo(x, y)).unwrap();
        }
    }

    fn handle_mousemove(&mut self, mut root: NodeMut, data: &MouseData) {
        if self.dragging {
            let id = root.id();
            let offset = data.element_coordinates();
            let mut new = Pos::new(offset.x as usize, offset.y as usize);

            // textboxs are only one line tall
            new.row = 0;

            if new != self.cursor.start {
                self.cursor.end = Some(new);
            }
            let rdom = root.real_dom_mut();
            self.write_value(rdom, id);
        }
    }

    fn handle_mousedown(&mut self, mut root: NodeMut, data: &MouseData) {
        let offset = data.element_coordinates();
        let mut new = Pos::new(offset.x as usize, offset.y as usize);

        // textboxs are only one line tall
        new.row = 0;

        new.realize_col(self.text.as_str());
        self.cursor = Cursor::from_start(new);
        self.dragging = true;

        let id = root.id();

        // move cursor to new position
        let rdom = root.real_dom_mut();
        let world = rdom.raw_world_mut();
        let taffy = {
            let query: UniqueView<Query> = world.borrow().unwrap();
            query.stretch.clone()
        };

        let taffy = taffy.lock().unwrap();

        let layout = get_layout(rdom.get(self.div_wrapper).unwrap(), &taffy).unwrap();
        let Point { x, y } = layout.location;

        let Pos { col, row } = self.cursor.start;
        let (x, y) = (col as u16 + x as u16, row as u16 + y as u16);
        if let Ok(pos) = crossterm::cursor::position() {
            if pos != (x, y) {
                execute!(stdout(), MoveTo(x, y)).unwrap();
            }
        } else {
            execute!(stdout(), MoveTo(x, y)).unwrap();
        }

        self.write_value(rdom, id)
    }
}

impl<C: TextLikeController + Send + Sync + Default + 'static> CustomElement for TextLike<C> {
    const NAME: &'static str = "input";

    fn roots(&self) -> Vec<NodeId> {
        vec![self.div_wrapper]
    }

    fn create(mut root: dioxus_native_core::real_dom::NodeMut) -> Self {
        let node_type = root.node_type();
        let NodeType::Element(el) = &*node_type else {
            panic!("input must be an element")
        };

        let value = el
            .attributes
            .get(&OwnedAttributeDiscription {
                name: "value".to_string(),
                namespace: None,
            })
            .and_then(|value| value.as_text())
            .map(|value| value.to_string());

        drop(node_type);

        let rdom = root.real_dom_mut();

        let pre_text = rdom.create_node(String::new());
        let pre_text_id = pre_text.id();
        let highlighted_text = rdom.create_node(String::new());
        let highlighted_text_id = highlighted_text.id();
        let mut highlighted_text_span = rdom.create_node(NodeType::Element(ElementNode {
            tag: "span".to_string(),
            attributes: [(
                OwnedAttributeDiscription {
                    name: "background-color".to_string(),
                    namespace: Some("style".to_string()),
                },
                "rgba(255, 255, 255, 50%)".to_string().into(),
            )]
            .into_iter()
            .collect(),
            ..Default::default()
        }));
        highlighted_text_span.add_child(highlighted_text_id);
        let highlighted_text_span_id = highlighted_text_span.id();
        let post_text = rdom.create_node(value.clone().unwrap_or_default());
        let post_text_id = post_text.id();
        let mut div_wrapper = rdom.create_node(NodeType::Element(ElementNode {
            tag: "div".to_string(),
            attributes: [(
                OwnedAttributeDiscription {
                    name: "display".to_string(),
                    namespace: Some("style".to_string()),
                },
                "flex".to_string().into(),
            )]
            .into_iter()
            .collect(),
            ..Default::default()
        }));
        let div_wrapper_id = div_wrapper.id();
        div_wrapper.add_child(pre_text_id);
        div_wrapper.add_child(highlighted_text_span_id);
        div_wrapper.add_child(post_text_id);

        div_wrapper.add_event_listener("mousemove");
        div_wrapper.add_event_listener("mousedown");
        div_wrapper.add_event_listener("mouseup");
        div_wrapper.add_event_listener("mouseleave");
        div_wrapper.add_event_listener("mouseenter");
        root.add_event_listener("keydown");
        root.add_event_listener("focusout");

        Self {
            pre_cursor_text: pre_text_id,
            highlighted_text: highlighted_text_id,
            post_cursor_text: post_text_id,
            div_wrapper: div_wrapper_id,
            cursor: Cursor::default(),
            text: value.unwrap_or_default(),
            ..Default::default()
        }
    }

    fn attributes_changed(
        &mut self,
        mut root: dioxus_native_core::real_dom::NodeMut,
        attributes: &dioxus_native_core::node_ref::AttributeMask,
    ) {
        match attributes {
            AttributeMask::All => {
                {
                    let node_type = root.node_type_mut();
                    let NodeTypeMut::Element(mut el) = node_type else {
                        panic!("input must be an element")
                    };
                    self.update_value_attr(&el);
                    self.update_size_attr(&mut el);
                    self.update_max_width_attr(&el);
                }
                let id = root.id();
                self.write_value(root.real_dom_mut(), id);
            }
            AttributeMask::Some(attrs) => {
                {
                    let node_type = root.node_type_mut();
                    let NodeTypeMut::Element(mut el) = node_type else {
                        panic!("input must be an element")
                    };
                    if attrs.contains("width") || attrs.contains("height") {
                        self.update_size_attr(&mut el);
                    }
                    if attrs.contains("maxlength") {
                        self.update_max_width_attr(&el);
                    }
                    if attrs.contains("value") {
                        self.update_value_attr(&el);
                    }
                }
                if attrs.contains("value") {
                    let id = root.id();
                    self.write_value(root.real_dom_mut(), id);
                }
            }
        }
    }
}

impl<C: TextLikeController + Send + Sync + Default + 'static> RinkWidget for TextLike<C> {
    fn handle_event(&mut self, event: &crate::Event, node: NodeMut) {
        match event.name {
            "keydown" => {
                if let EventData::Keyboard(data) = &event.data {
                    self.handle_keydown(node, data);
                }
            }

            "mousemove" => {
                if let EventData::Mouse(data) = &event.data {
                    self.handle_mousemove(node, data);
                }
            }

            "mousedown" => {
                if let EventData::Mouse(data) = &event.data {
                    self.handle_mousedown(node, data);
                }
            }

            "mouseup" => {
                self.dragging = false;
            }

            "mouseleave" => {
                self.dragging = false;
            }

            "mouseenter" => {
                self.dragging = false;
            }

            "focusout" => {
                execute!(stdout(), MoveTo(0, 1000)).unwrap();
            }

            _ => {}
        }
    }
}