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
use dioxus_native_core::real_dom::NodeImmutable;

use freya_engine::prelude::*;
use freya_node_state::{
    Border, CornerRadius, CursorSettings, Fill, FontStyleState, LayoutState, References, Shadow,
    Style, TextOverflow, Transform,
};
use torin::{alignment::Alignment, direction::DirectionMode, gaps::Gaps, size::Size};

use crate::dom::DioxusNode;

#[derive(Clone, PartialEq)]
pub struct NodeState {
    pub cursor: CursorSettings,
    pub font_style: FontStyleState,
    pub references: References,
    pub size: LayoutState,
    pub style: Style,
    pub transform: Transform,
}

pub fn get_node_state(node: &DioxusNode) -> NodeState {
    let cursor = node
        .get::<CursorSettings>()
        .as_deref()
        .cloned()
        .unwrap_or_default();
    let font_style = node
        .get::<FontStyleState>()
        .as_deref()
        .cloned()
        .unwrap_or_default();
    let references = node
        .get::<References>()
        .as_deref()
        .cloned()
        .unwrap_or_default();
    let size = node
        .get::<LayoutState>()
        .as_deref()
        .cloned()
        .unwrap_or_default();
    let style = node.get::<Style>().as_deref().cloned().unwrap_or_default();
    let transform = node
        .get::<Transform>()
        .as_deref()
        .cloned()
        .unwrap_or_default();

    NodeState {
        cursor,
        font_style,
        references,
        size,
        style,
        transform,
    }
}

impl NodeState {
    pub fn iter(&self) -> NodeStateIterator {
        NodeStateIterator {
            state: self,
            curr: 0,
        }
    }
}

pub struct NodeStateIterator<'a> {
    state: &'a NodeState,
    curr: usize,
}

impl<'a> Iterator for NodeStateIterator<'a> {
    type Item = (&'a str, AttributeType<'a>);

    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        match n {
            0 => Some(("width", AttributeType::Size(&self.state.size.width))),
            1 => Some(("height", AttributeType::Size(&self.state.size.height))),
            2 => Some((
                "min_width",
                AttributeType::Size(&self.state.size.minimum_width),
            )),
            3 => Some((
                "min_height",
                AttributeType::Size(&self.state.size.minimum_height),
            )),
            4 => Some((
                "max_width",
                AttributeType::Size(&self.state.size.maximum_width),
            )),
            5 => Some((
                "max_height",
                AttributeType::Size(&self.state.size.maximum_height),
            )),
            6 => Some((
                "direction",
                AttributeType::Direction(&self.state.size.direction),
            )),
            7 => Some(("padding", AttributeType::Measures(self.state.size.padding))),
            8 => Some((
                "main_alignment",
                AttributeType::Alignment(&self.state.size.main_alignment),
            )),
            9 => Some((
                "cross_alignment",
                AttributeType::Alignment(&self.state.size.cross_alignment),
            )),
            10 => {
                let background = &self.state.style.background;
                let fill = match *background {
                    Fill::Color(_) => AttributeType::Color(background.clone()),
                    Fill::LinearGradient(_) => AttributeType::LinearGradient(background.clone()),
                };
                Some(("background", fill))
            }
            11 => Some(("border", AttributeType::Border(&self.state.style.border))),
            12 => Some((
                "corner_radius",
                AttributeType::CornerRadius(self.state.style.corner_radius),
            )),
            13 => Some((
                "color",
                AttributeType::Color(self.state.font_style.color.into()),
            )),
            14 => Some((
                "font_family",
                AttributeType::Text(self.state.font_style.font_family.join(",")),
            )),
            15 => Some((
                "font_size",
                AttributeType::Measure(self.state.font_style.font_size),
            )),
            16 => Some((
                "line_height",
                AttributeType::Measure(self.state.font_style.line_height),
            )),
            17 => Some((
                "text_align",
                AttributeType::TextAlignment(&self.state.font_style.text_align),
            )),
            18 => Some((
                "text_overflow",
                AttributeType::TextOverflow(&self.state.font_style.text_overflow),
            )),
            19 => Some((
                "offset_x",
                AttributeType::Measure(self.state.size.offset_x.get()),
            )),
            20 => Some((
                "offset_y",
                AttributeType::Measure(self.state.size.offset_y.get()),
            )),
            n => {
                let shadows = &self.state.style.shadows;
                let shadow = shadows
                    .get(n - 21)
                    .map(|shadow| ("shadow", AttributeType::Shadow(shadow)));

                if shadow.is_some() {
                    shadow
                } else {
                    let text_shadows = &self.state.font_style.text_shadows;
                    text_shadows
                        .get(n - 21 + shadows.len())
                        .map(|text_shadow| ("text_shadow", AttributeType::TextShadow(text_shadow)))
                }
            }
        }
    }

    fn next(&mut self) -> Option<Self::Item> {
        let current = self.curr;
        self.curr += 1;

        self.nth(current)
    }
}

pub enum AttributeType<'a> {
    Color(Fill),
    LinearGradient(Fill),
    Size(&'a Size),
    Measure(f32),
    Measures(Gaps),
    CornerRadius(CornerRadius),
    Direction(&'a DirectionMode),
    Alignment(&'a Alignment),
    Shadow(&'a Shadow),
    TextShadow(&'a TextShadow),
    Text(String),
    Border(&'a Border),
    TextAlignment(&'a TextAlign),
    TextOverflow(&'a TextOverflow),
}

pub trait ExternalPretty {
    fn pretty(&self) -> String;
}

impl ExternalPretty for TextAlign {
    fn pretty(&self) -> String {
        match self {
            TextAlign::Left => "left".to_string(),
            TextAlign::Right => "right".to_string(),
            TextAlign::Center => "center".to_string(),
            TextAlign::Justify => "justify".to_string(),
            TextAlign::Start => "start".to_string(),
            TextAlign::End => "end".to_string(),
        }
    }
}