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
//!
//! Draw.
//!

use crate::attrmap2::AttrMap2;
use crate::style::units::RelativeScale;
use crate::style::{GraphicStyleRef, ParagraphStyleRef};
use crate::text::{TextP, TextTag};
use crate::xlink::{XLinkActuate, XLinkShow, XLinkType};
use crate::{CellRef, Length, OdsError};
use base64::Engine;
use chrono::NaiveDateTime;

/// The <office:annotation> element specifies an OpenDocument annotation. The annotation's
/// text is contained in <text:p> and <text:list> elements.
#[derive(Debug, Clone)]
pub struct Annotation {
    //
    name: String,
    //
    display: bool,
    //
    creator: Option<String>,
    date: Option<NaiveDateTime>,
    text: Vec<TextTag>,
    //
    attr: AttrMap2,
}

impl Annotation {
    /// New annotation.
    pub fn new_empty() -> Self {
        Self {
            name: Default::default(),
            display: false,
            creator: None,
            date: None,
            text: Default::default(),
            attr: Default::default(),
        }
    }

    /// New annotation.
    pub fn new<S: Into<String>>(annotation: S) -> Self {
        let mut r = Self {
            name: Default::default(),
            display: true,
            creator: None,
            date: None,
            text: Default::default(),
            attr: Default::default(),
        };
        r.push_text(TextP::new().text(annotation).into_xmltag());
        r
    }

    /// Allows access to all attributes of the style itself.
    pub fn attrmap(&self) -> &AttrMap2 {
        &self.attr
    }

    /// Allows access to all attributes of the style itself.
    pub fn attrmap_mut(&mut self) -> &mut AttrMap2 {
        &mut self.attr
    }

    /// Name
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Name
    pub fn set_name<S: Into<String>>(&mut self, name: S) {
        self.name = name.into();
    }

    /// Display
    pub fn display(&self) -> bool {
        self.display
    }

    /// Name
    pub fn set_display(&mut self, display: bool) {
        self.display = display;
    }

    /// Creator
    pub fn creator(&self) -> Option<&String> {
        self.creator.as_ref()
    }

    /// Creator
    pub fn set_creator<S: Into<String>>(&mut self, creator: Option<S>) {
        self.creator = creator.map(|v| v.into())
    }

    /// Date of the annotation.
    pub fn date(&self) -> Option<&NaiveDateTime> {
        self.date.as_ref()
    }

    /// Date of the annotation.
    pub fn set_date(&mut self, date: Option<NaiveDateTime>) {
        self.date = date;
    }

    /// Text.
    pub fn text(&self) -> &Vec<TextTag> {
        &self.text
    }

    /// Text.
    pub fn push_text(&mut self, text: TextTag) {
        self.text.push(text);
    }

    /// Text.
    pub fn push_text_str<S: Into<String>>(&mut self, text: S) {
        self.text.push(TextP::new().text(text).into_xmltag());
    }

    /// Text.
    pub fn set_text(&mut self, text: Vec<TextTag>) {
        self.text = text;
    }

    draw_caption_point_x!(attr);
    draw_caption_point_y!(attr);
    draw_class_names!(attr);
    draw_corner_radius!(attr);
    draw_id!(attr);
    draw_layer!(attr);
    draw_style_name!(attr);
    draw_text_style_name!(attr);
    draw_transform!(attr);
    draw_z_index!(attr);
    svg_height!(attr);
    svg_width!(attr);
    svg_x!(attr);
    svg_y!(attr);
    table_end_cell_address!(attr);
    table_end_x!(attr);
    table_end_y!(attr);
    table_table_background!(attr);
    xml_id!(attr);
}

// /// The <draw:rect> element represents a rectangular drawing shape.
// #[derive(Debug, Clone)]
// pub struct DrawRect {
//     ///
//     name: String,
//     ///
//     attr: AttrMap2,
// }
//
// impl DrawRect {
//     pub fn new_empty() -> Self {
//         Self {
//             name: "".to_string(),
//             attr: Default::default(),
//         }
//     }
//
//     pub fn new<S: Into<String>>(name: S) -> Self {
//         Self {
//             name: name.into(),
//             attr: Default::default(),
//         }
//     }
//
//     /// Allows access to all attributes of the style itself.
//     pub fn attrmap(&self) -> &AttrMap2 {
//         &self.attr
//     }
//
//     /// Allows access to all attributes of the style itself.
//     pub fn attrmap_mut(&mut self) -> &mut AttrMap2 {
//         &mut self.attr
//     }
//
//     /// Name
//     pub fn name(&self) -> &str {
//         &self.name
//     }
//
//     /// Name
//     pub fn set_name<S: Into<String>>(&mut self, name: S) {
//         self.name = name.into();
//     }
//
//     draw_caption_id!(attr);
//     draw_class_names!(attr);
//     draw_corner_radius!(attr);
//     draw_id!(attr);
//     draw_layer!(attr);
//     draw_style_name!(attr);
//     draw_text_style_name!(attr);
//     draw_transform!(attr);
//     draw_z_index!(attr);
//     svg_height!(attr);
//     svg_width!(attr);
//     svg_rx!(attr);
//     svg_ry!(attr);
//     svg_x!(attr);
//     svg_y!(attr);
//     table_end_cell_address!(attr);
//     table_end_x!(attr);
//     table_end_y!(attr);
//     table_table_background!(attr);
//     xml_id!(attr);
// }

/// The <draw:frame> element represents a frame and serves as the container for elements that
/// may occur in a frame.
/// Frame formatting properties are stored in styles belonging to the graphic family.
#[derive(Debug, Clone, Default)]
pub struct DrawFrame {
    /// The <svg:title> element specifies a name for a graphic object.
    title: Option<String>,
    /// The <svg:desc> element specifies a prose description of a graphic object that may be used to
    /// support accessibility. See appendix D.
    desc: Option<String>,
    ///
    attr: AttrMap2,
    ///
    content: Vec<DrawFrameContent>,
}

/// Draw-frame content data.
#[derive(Debug, Clone)]
pub enum DrawFrameContent {
    /// Image
    Image(DrawImage),
}

impl DrawFrame {
    /// New.
    pub fn new() -> Self {
        Default::default()
    }

    /// Allows access to all attributes of the style itself.
    pub fn attrmap(&self) -> &AttrMap2 {
        &self.attr
    }

    /// Allows access to all attributes of the style itself.
    pub fn attrmap_mut(&mut self) -> &mut AttrMap2 {
        &mut self.attr
    }

    /// Desc
    pub fn desc(&self) -> Option<&String> {
        self.desc.as_ref()
    }

    /// Desc
    pub fn set_desc<S: Into<String>>(&mut self, desc: S) {
        self.desc = Some(desc.into())
    }

    /// Desc
    pub fn clear_desc(&mut self) {
        self.desc = None;
    }

    /// Title
    pub fn title(&self) -> Option<&String> {
        self.title.as_ref()
    }

    /// Name
    pub fn set_title<S: Into<String>>(&mut self, title: S) {
        self.title = Some(title.into());
    }

    /// Name
    pub fn clear_title(&mut self) {
        self.title = None;
    }

    /// Frame content.
    pub fn set_content(&mut self, content: Vec<DrawFrameContent>) {
        self.content = content;
    }

    /// Frame content.
    pub fn push_content(&mut self, content: DrawFrameContent) {
        self.content.push(content);
    }

    /// Frame content.
    pub fn clear_content(&mut self) {
        self.content.clear();
    }

    /// Frame content.
    pub fn content_ref(&self) -> &Vec<DrawFrameContent> {
        &self.content
    }

    /// Frame content.
    pub fn content_mut(&mut self) -> &mut Vec<DrawFrameContent> {
        &mut self.content
    }

    draw_name!(attr);
    draw_caption_id!(attr);
    draw_class_names!(attr);
    draw_corner_radius!(attr);
    draw_copy_of!(attr);
    draw_id!(attr);
    draw_layer!(attr);
    draw_style_name!(attr);
    draw_text_style_name!(attr);
    draw_transform!(attr);
    draw_z_index!(attr);
    style_rel_height!(attr);
    style_rel_width!(attr);
    svg_height!(attr);
    svg_width!(attr);
    svg_x!(attr);
    svg_y!(attr);
    table_end_cell_address!(attr);
    table_end_x!(attr);
    table_end_y!(attr);
    table_table_background!(attr);
    xml_id!(attr);
}

/// The <draw:image> element represents an image. An image can be either:
/// • A link to an external resource
/// or
/// • Embedded in the document
/// The <draw:image> element may have text content. Text content is displayed in addition to the
/// image data.
/// Note: While the image data may have an arbitrary format, vector graphics should
/// be stored in the SVG format and bitmap graphics in the PNG format.
#[derive(Debug, Clone, Default)]
pub struct DrawImage {
    attr: AttrMap2,
    binary_data: Option<String>,
    text: Vec<TextTag>,
}

impl DrawImage {
    /// New.
    pub fn new() -> Self {
        Default::default()
    }

    /// Allows access to all attributes of the style itself.
    pub fn attrmap(&self) -> &AttrMap2 {
        &self.attr
    }

    /// Allows access to all attributes of the style itself.
    pub fn attrmap_mut(&mut self) -> &mut AttrMap2 {
        &mut self.attr
    }

    /// Image binary data.
    pub fn get_binary_base64(&self) -> Option<&String> {
        self.binary_data.as_ref()
    }

    /// Image binary data.
    pub fn set_binary_base64(&mut self, binary: String) {
        self.binary_data = Some(binary);
    }

    /// Image binary data.
    pub fn get_binary(&self) -> Result<Vec<u8>, OdsError> {
        let ng = base64::engine::GeneralPurpose::new(
            &base64::alphabet::STANDARD,
            base64::engine::general_purpose::NO_PAD,
        );

        if let Some(binary_data) = &self.binary_data {
            Ok(ng.decode(binary_data)?)
        } else {
            Ok(Default::default())
        }
    }

    /// Image binary data.
    /// Note: While the image data may have an arbitrary format, vector graphics should
    /// be stored in the SVG format and bitmap graphics in the PNG format.
    pub fn set_binary(&mut self, binary: &[u8]) {
        let ng = base64::engine::GeneralPurpose::new(
            &base64::alphabet::STANDARD,
            base64::engine::general_purpose::NO_PAD,
        );

        self.binary_data = Some(ng.encode(binary));
    }

    /// Image binary data.
    pub fn clear_binary(&mut self) {
        self.binary_data = None;
    }

    /// Text
    pub fn get_text(&self) -> &Vec<TextTag> {
        &self.text
    }

    /// Text
    pub fn push_text(&mut self, text: TextTag) {
        self.text.push(text);
    }

    /// Text
    pub fn push_text_str<S: Into<String>>(&mut self, text: S) {
        self.text.push(TextP::new().text(text).into_xmltag());
    }

    /// Text
    pub fn set_text(&mut self, text: Vec<TextTag>) {
        self.text = text;
    }

    draw_filter_name!(attr);
    draw_mime_type!(attr);
    xlink_actuate!(attr);
    xlink_href!(attr);
    xlink_show!(attr);
    xlink_type!(attr);
    xml_id!(attr);
}