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
use std::ops::Deref;

use crate::{
    prelude::{
        Area,
        Point2D,
        Size2D,
    },
    scaled::Scaled,
};

#[derive(Default, PartialEq, Clone, Debug)]
pub struct PositionSides {
    pub top: Option<f32>,
    pub right: Option<f32>,
    pub bottom: Option<f32>,
    pub left: Option<f32>,
}

#[derive(Default, PartialEq, Clone, Debug)]
pub enum Position {
    #[default]
    Stacked,

    Absolute(Box<PositionSides>),
    Fixed(Box<PositionSides>),
}

impl Position {
    pub fn is_empty(&self) -> bool {
        match self {
            Self::Absolute(absolute_position) => {
                let PositionSides {
                    top,
                    right,
                    bottom,
                    left,
                } = absolute_position.deref();
                top.is_some() && right.is_some() && bottom.is_some() && left.is_some()
            }
            Self::Fixed(absolute_position) => {
                let PositionSides {
                    top,
                    right,
                    bottom,
                    left,
                } = absolute_position.deref();
                top.is_some() && right.is_some() && bottom.is_some() && left.is_some()
            }
            Self::Stacked => true,
        }
    }

    pub fn new_absolute() -> Self {
        Self::Absolute(Box::new(PositionSides {
            top: None,
            right: None,
            bottom: None,
            left: None,
        }))
    }

    pub fn new_fixed() -> Self {
        Self::Fixed(Box::new(PositionSides {
            top: None,
            right: None,
            bottom: None,
            left: None,
        }))
    }

    pub fn is_absolute(&self) -> bool {
        matches!(self, Self::Absolute { .. })
    }

    pub fn is_fixed(&self) -> bool {
        matches!(self, Self::Fixed { .. })
    }

    pub fn set_top(&mut self, value: f32) {
        if !self.is_absolute() {
            *self = Self::new_absolute();
        }
        if let Self::Absolute(absolute_position) = self {
            absolute_position.top = Some(value)
        }
    }

    pub fn set_right(&mut self, value: f32) {
        if !self.is_absolute() {
            *self = Self::new_absolute();
        }
        if let Self::Absolute(absolute_position) = self {
            absolute_position.right = Some(value)
        }
    }

    pub fn set_bottom(&mut self, value: f32) {
        if !self.is_absolute() {
            *self = Self::new_absolute();
        }
        if let Self::Absolute(absolute_position) = self {
            absolute_position.bottom = Some(value)
        }
    }

    pub fn set_left(&mut self, value: f32) {
        if !self.is_absolute() {
            *self = Self::new_absolute();
        }
        if let Self::Absolute(absolute_position) = self {
            absolute_position.left = Some(value)
        }
    }

    pub fn get_origin(
        &self,
        available_parent_area: &Area,
        parent_area: &Area,
        area_size: &Size2D,
        root_area: &Area,
    ) -> Point2D {
        match self {
            Position::Stacked => available_parent_area.origin,
            Position::Absolute(absolute_position) => {
                let PositionSides {
                    top,
                    right,
                    bottom,
                    left,
                } = absolute_position.deref();
                let y = {
                    let mut y = parent_area.min_y();
                    if let Some(top) = top {
                        y += top;
                    } else if let Some(bottom) = bottom {
                        y = parent_area.max_y() - bottom - area_size.height;
                    }
                    y
                };
                let x = {
                    let mut x = parent_area.min_x();
                    if let Some(left) = left {
                        x += left;
                    } else if let Some(right) = right {
                        x = parent_area.max_x() - right - area_size.width;
                    }
                    x
                };
                Point2D::new(x, y)
            }
            Position::Fixed(fixed_position) => {
                let PositionSides {
                    top,
                    right,
                    bottom,
                    left,
                } = fixed_position.deref();
                let y = {
                    let mut y = 0.;
                    if let Some(top) = top {
                        y = *top;
                    } else if let Some(bottom) = bottom {
                        y = root_area.max_y() - bottom;
                    }
                    y
                };
                let x = {
                    let mut x = 0.;
                    if let Some(left) = left {
                        x = *left;
                    } else if let Some(right) = right {
                        x = root_area.max_x() - right;
                    }
                    x
                };
                Point2D::new(x, y)
            }
        }
    }
}

impl Scaled for Position {
    fn scale(&mut self, scale_factor: f32) {
        if let Self::Absolute(absolute_postion) = self {
            if let Some(top) = &mut absolute_postion.top {
                *top *= scale_factor;
            }
            if let Some(right) = &mut absolute_postion.right {
                *right *= scale_factor;
            }
            if let Some(bottom) = &mut absolute_postion.bottom {
                *bottom *= scale_factor;
            }
            if let Some(left) = &mut absolute_postion.left {
                *left *= scale_factor;
            }
        }
    }
}

impl Position {
    pub fn pretty(&self) -> String {
        match self {
            Self::Stacked => "stacked".to_string(),
            Self::Absolute(positions) => format!(
                "{}, {}, {}, {}",
                positions.top.unwrap_or_default(),
                positions.right.unwrap_or_default(),
                positions.bottom.unwrap_or_default(),
                positions.left.unwrap_or_default()
            ),
            Self::Fixed(positions) => format!(
                "{}, {}, {}, {}",
                positions.top.unwrap_or_default(),
                positions.right.unwrap_or_default(),
                positions.bottom.unwrap_or_default(),
                positions.left.unwrap_or_default()
            ),
        }
    }
}