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
// patternFill
use super::Color;
use super::EnumValue;
use super::PatternValues;
use md5::Digest;
use quick_xml::events::{BytesStart, Event};
use quick_xml::Reader;
use quick_xml::Writer;
use reader::driver::*;
use std::io::Cursor;
use writer::driver::*;

#[derive(Default, Debug, Clone, PartialEq, PartialOrd)]
pub struct PatternFill {
    pub(crate) pattern_type: EnumValue<PatternValues>,
    foreground_color: Option<Color>,
    background_color: Option<Color>,
}
impl PatternFill {
    pub fn get_pattern_type(&self) -> &PatternValues {
        self.pattern_type.get_value()
    }

    pub fn set_pattern_type(&mut self, value: PatternValues) -> &mut Self {
        self.pattern_type.set_value(value);
        self
    }

    fn auto_set_pattern_type(&mut self) -> &mut Self {
        if self.get_pattern_type() == &PatternValues::None {
            if self.get_foreground_color().is_some() {
                self.set_pattern_type(PatternValues::Solid);
            }
        } else if self.get_foreground_color().is_none() {
            self.set_pattern_type(PatternValues::None);
        }
        self
    }

    pub fn get_foreground_color(&self) -> &Option<Color> {
        &self.foreground_color
    }

    pub fn get_foreground_color_mut(&mut self) -> &mut Color {
        self.foreground_color.get_or_insert(Color::default())
    }

    pub fn set_foreground_color(&mut self, value: Color) -> &mut Self {
        self.foreground_color = Some(value);
        self.auto_set_pattern_type();
        self
    }

    pub fn remove_foreground_color(&mut self) -> &mut Self {
        self.foreground_color = None;
        self
    }

    pub fn get_background_color(&self) -> &Option<Color> {
        &self.background_color
    }

    pub fn get_background_color_mut(&mut self) -> &mut Color {
        self.background_color.get_or_insert(Color::default())
    }

    pub fn set_background_color(&mut self, value: Color) -> &mut Self {
        self.background_color = Some(value);
        self
    }

    pub fn remove_background_color(&mut self) -> &mut Self {
        self.background_color = None;
        self
    }

    pub(crate) fn get_hash_code(&self) -> String {
        format!(
            "{:x}",
            md5::Md5::digest(format!(
                "{}{}{}",
                &self.pattern_type.get_value_string(),
                match &self.foreground_color {
                    Some(v) => {
                        v.get_hash_code()
                    }
                    None => {
                        "None".into()
                    }
                },
                match &self.background_color {
                    Some(v) => {
                        v.get_hash_code()
                    }
                    None => {
                        "None".into()
                    }
                },
            ))
        )
    }

    pub(crate) fn set_attributes<R: std::io::BufRead>(
        &mut self,
        reader: &mut Reader<R>,
        e: &BytesStart,
        empty_flag: bool,
    ) {
        match get_attribute(e, b"patternType") {
            Some(v) => {
                self.pattern_type.set_value_string(v);
            }
            None => {}
        }

        if empty_flag {
            return;
        }

        let mut buf = Vec::new();
        loop {
            match reader.read_event_into(&mut buf) {
                Ok(Event::Empty(ref e)) => match e.name().into_inner() {
                    b"fgColor" => {
                        let mut obj = Color::default();
                        obj.set_attributes(reader, e, true);
                        self.set_foreground_color(obj);
                    }
                    b"bgColor" => {
                        let mut obj = Color::default();
                        obj.set_attributes(reader, e, true);
                        self.set_background_color(obj);
                    }
                    _ => (),
                },
                Ok(Event::End(ref e)) => match e.name().into_inner() {
                    b"patternFill" => return,
                    _ => (),
                },
                Ok(Event::Eof) => panic!("Error not find {} end element", "patternFill"),
                Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
                _ => (),
            }
            buf.clear();
        }
    }

    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
        let empty_flag = self.foreground_color.is_none() && self.background_color.is_none();

        // patternFill
        let mut attributes: Vec<(&str, &str)> = Vec::new();
        if self.pattern_type.has_value() {
            attributes.push(("patternType", self.pattern_type.get_value_string()));
        }
        write_start_tag(writer, "patternFill", attributes, empty_flag);

        if !empty_flag {
            // fgColor
            match &self.foreground_color {
                Some(v) => {
                    v.write_to_fg_color(writer);
                }
                None => {}
            }

            // bgColor
            match &self.background_color {
                Some(v) => {
                    v.write_to_bg_color(writer);
                }
                None => {}
            }

            write_end_tag(writer, "patternFill");
        }
    }
}