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
//!
//! Defines one ValueFormatXX per ValueType for textual formatting of those values.
//!
//! ```
//! use spreadsheet_ods::{ValueType};
//! use spreadsheet_ods::format::{FormatCalendarStyle, FormatNumberStyle, ValueFormatDateTime, ValueFormatNumber};
//!
//! let mut v = ValueFormatDateTime::new_named("dt0");
//! v.part_day().long_style().build();
//! v.part_text(".").build();
//! v.part_month().long_style().build();
//! v.part_text(".").build();
//! v.part_year().long_style().build();
//! v.part_text(" ").build();
//! v.part_hours().long_style().build();
//! v.part_text(":").build();
//! v.part_minutes().long_style().build();
//! v.part_text(":").build();
//! v.part_seconds().long_style().build();
//!
//! let mut v = ValueFormatNumber::new_named("n3");
//! v.part_number().decimal_places(3).build();
//! ```
//!

// This cares about the following parts of office:styles and office:automatic-styles.
//
// <number:boolean-style> 16.29.24,
// <number:currency-style> 16.29.8,
// <number:date-style> 16.29.11,
// <number:number-style> 16.29.2,
// <number:percentage-style> 16.29.10,
// <number:text-style> 16.29.26,
// <number:time-style> 16.29.19,
//

mod builder;
mod create;
mod stylemap;

pub use builder::*;
pub use create::*;
pub use stylemap::*;

use crate::attrmap2::AttrMap2;
use crate::color::Rgb;
use crate::style::units::{
    Angle, FontSize, FontStyle, FontVariant, FontWeight, FormatSource, Length, LetterSpacing,
    LineMode, LineStyle, LineType, LineWidth, Percent, RotationScale, TextCombine, TextCondition,
    TextDisplay, TextEmphasize, TextEmphasizePosition, TextPosition, TextRelief, TextTransform,
    TransliterationStyle,
};
use crate::style::ParseStyleAttr;
use crate::style::{
    color_string, shadow_string, text_position, StyleOrigin, StyleUse, TextStyleRef,
};
use crate::{OdsError, ValueType};
use icu_locid::subtags::{Language, Region, Script};
use icu_locid::{LanguageIdentifier, Locale};
use std::fmt::{Display, Formatter};
use std::str::FromStr;

style_ref!(ValueFormatRef);

/// Trait used by the builder types.
pub trait ValueFormatTrait {
    /// Returns a reference name for this value format.
    fn format_ref(&self) -> ValueFormatRef;

    /// The style:name attribute specifies names that reference style mechanisms.
    fn set_name<S: Into<String>>(&mut self, name: S);

    /// The style:name attribute specifies names that reference style mechanisms.
    fn name(&self) -> &String;

    /// Returns the value type.
    fn value_type(&self) -> ValueType;

    /// Sets the storage location for this ValueFormat. Either content.xml
    /// or styles.xml.
    fn set_origin(&mut self, origin: StyleOrigin);

    /// Returns the storage location.
    fn origin(&self) -> StyleOrigin;

    /// How is the style used in the document.
    fn set_styleuse(&mut self, styleuse: StyleUse);

    /// How is the style used in the document.
    fn styleuse(&self) -> StyleUse;

    /// All direct attributes of the number:xxx-style tag.
    fn attrmap(&self) -> &AttrMap2;

    /// All direct attributes of the number:xxx-style tag.
    fn attrmap_mut(&mut self) -> &mut AttrMap2;

    /// Text style attributes.
    fn textstyle(&self) -> &AttrMap2;

    /// Text style attributes.
    fn textstyle_mut(&mut self) -> &mut AttrMap2;

    /// Adds a format part.
    fn push_part(&mut self, part: FormatPart);

    /// Adds all format parts.
    fn push_parts(&mut self, partvec: &mut Vec<FormatPart>);

    /// Returns the parts.
    fn parts(&self) -> &Vec<FormatPart>;

    /// Returns the mutable parts.
    fn parts_mut(&mut self) -> &mut Vec<FormatPart>;

    /// Adds a stylemap.
    fn push_stylemap(&mut self, stylemap: ValueStyleMap);

    /// Returns the stylemaps
    fn stylemaps(&self) -> Option<&Vec<ValueStyleMap>>;

    /// Returns the mutable stylemap.
    fn stylemaps_mut(&mut self) -> &mut Vec<ValueStyleMap>;
}

valueformat!(ValueFormatBoolean, ValueType::Boolean);

// 16.29.24 <number:boolean-style>
impl ValueFormatBoolean {
    part_boolean!();

    push_boolean!();
}

// 16.29.2 <number:number-style>
valueformat!(ValueFormatNumber, ValueType::Number);

impl ValueFormatNumber {
    part_fill_character!();
    part_fraction!();
    part_number!();
    part_scientific!();
    part_text!();

    push_fraction!();
    push_number!();
    push_number_fix!();
    push_scientific!();
    push_text!();
}

// 16.29.10 <number:percentage-style>
valueformat!(ValueFormatPercentage, ValueType::Percentage);

impl ValueFormatPercentage {
    part_fill_character!();
    part_number!();
    part_text!();

    push_number!();
    push_number_fix!();
    push_text!();
}

// 16.29.8 <number:currency-style>
valueformat!(ValueFormatCurrency, ValueType::Currency);

impl ValueFormatCurrency {
    number_automatic_order!(attr);

    part_currency!();
    part_fill_character!();
    part_number!();
    part_text!();

    push_currency_symbol!();
    push_number!();
    push_number_fix!();
    push_text!();
}

// 16.29.26 <number:text-style>
valueformat!(ValueFormatText, ValueType::Text);

impl ValueFormatText {
    part_fill_character!();
    part_text!();
    part_text_content!();

    push_text!();
    push_text_content!();
}

// 16.29.11 <number:date-style>
valueformat!(ValueFormatDateTime, ValueType::DateTime);

impl ValueFormatDateTime {
    number_automatic_order!(attr);
    number_format_source!(attr);

    part_am_pm!();
    part_day!();
    part_day_of_week!();
    part_era!();
    part_fill_character!();
    part_hours!();
    part_minutes!();
    part_month!();
    part_quarter!();
    part_seconds!();
    part_text!();
    part_week_of_year!();
    part_year!();

    push_am_pm!();
    push_day!();
    push_day_of_week!();
    push_era!();
    push_hours!();
    push_minutes!();
    push_month!();
    push_quarter!();
    push_seconds!();
    push_text!();
    push_week_of_year!();
    push_year!();
}

// 16.29.19 <number:time-style>
valueformat!(ValueFormatTimeDuration, ValueType::TimeDuration);

impl ValueFormatTimeDuration {
    number_format_source!(attr);
    number_truncate_on_overflow!(attr);

    part_am_pm!();
    part_fill_character!();
    part_hours!();
    part_minutes!();
    part_seconds!();
    part_text!();

    push_am_pm!();
    push_hours!();
    push_minutes!();
    push_seconds!();
    push_text!();
}

/// Identifies the structural parts of a value format.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[allow(missing_docs)]
pub enum FormatPartType {
    Number,
    FillCharacter,
    ScientificNumber,
    Fraction,
    CurrencySymbol,
    Day,
    Month,
    Year,
    Era,
    DayOfWeek,
    WeekOfYear,
    Quarter,
    Hours,
    Minutes,
    Seconds,
    AmPm,
    Boolean,
    Text,
    TextContent,
}

/// One structural part of a value format.
#[derive(Debug, Clone)]
pub struct FormatPart {
    /// What kind of format part is this?
    part_type: FormatPartType,
    /// Properties of this part.
    attr: AttrMap2,
    /// Textposition for embedded text when acting as a number format part.
    ///
    /// The number:position attribute specifies the position where text appears.
    /// The index of a position starts with 1 and is counted by digits from right to left in the integer part of
    /// a number, starting left from a decimal separator if one exists, or from the last digit of the number.
    /// Text is inserted before the digit at the specified position. If the value of number:position
    /// attribute is greater than the value of number:min-integer-digits and greater than
    /// the number of integer digits in the number, text is prepended to the number.
    position: Option<i32>,
    /// Some content.
    content: Option<String>,
}

/// Flag for several PartTypes.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[allow(missing_docs)]
pub enum FormatNumberStyle {
    Short,
    Long,
}

impl Display for FormatNumberStyle {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
        match self {
            FormatNumberStyle::Short => write!(f, "short"),
            FormatNumberStyle::Long => write!(f, "long"),
        }
    }
}

/// Calendar types.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[allow(missing_docs)]
pub enum FormatCalendarStyle {
    Gregorian,
    Gengou,
    Roc,
    Hanja,
    Hijri,
    Jewish,
    Buddhist,
}

impl Display for FormatCalendarStyle {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
        match self {
            FormatCalendarStyle::Gregorian => write!(f, "gregorian"),
            FormatCalendarStyle::Gengou => write!(f, "gengou"),
            FormatCalendarStyle::Roc => write!(f, "ROC"),
            FormatCalendarStyle::Hanja => write!(f, "hanja"),
            FormatCalendarStyle::Hijri => write!(f, "hijri"),
            FormatCalendarStyle::Jewish => write!(f, "jewish"),
            FormatCalendarStyle::Buddhist => write!(f, "buddhist"),
        }
    }
}

impl FormatPart {
    /// New, empty
    pub fn new(ftype: FormatPartType) -> Self {
        FormatPart {
            part_type: ftype,
            attr: Default::default(),
            position: None,
            content: None,
        }
    }

    /// Sets the kind of the part.
    pub fn set_part_type(&mut self, p_type: FormatPartType) {
        self.part_type = p_type;
    }

    /// What kind of part?
    pub fn part_type(&self) -> FormatPartType {
        self.part_type
    }

    /// General attributes.
    pub(crate) fn attrmap(&self) -> &AttrMap2 {
        &self.attr
    }

    /// General attributes.
    pub(crate) fn attrmap_mut(&mut self) -> &mut AttrMap2 {
        &mut self.attr
    }

    /// Adds an attribute.
    pub fn set_attr(&mut self, name: &str, value: String) {
        self.attr.set_attr(name, value);
    }

    /// Returns a property or a default.
    pub fn attr_def<'a, 'b, S>(&'a self, name: &'b str, default: S) -> &'a str
    where
        S: Into<&'a str>,
    {
        self.attr.attr_def(name, default)
    }

    /// Sets the position for embedded text in a number format part.
    pub fn set_position(&mut self, pos: i32) {
        self.position = Some(pos);
    }

    /// Clear the position for embedded text in a number format part.
    pub fn clear_position(&mut self) {
        self.position = None;
    }

    /// The position for embedded text in a number format part.
    pub fn position(&self) -> Option<i32> {
        self.position
    }

    /// Sets a textual content for this part. This is only used
    /// for text and currency-symbol.
    pub fn set_content<S: Into<String>>(&mut self, content: S) {
        self.content = Some(content.into());
    }

    /// Clear the textual content for this part. This is only used
    /// for text and currency-symbol.
    pub fn clear_content(&mut self) {
        self.content = None;
    }

    /// Returns the text content.
    pub fn content(&self) -> Option<&String> {
        self.content.as_ref()
    }
}