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
use std::ops::DerefMut;
use std::slice::Iter;
use crate::api::cell::formula::FormulaType;
use crate::api::cell::values::{CellDisplay, CellValue};
use crate::api::cell::location::{Location, LocationRange};
use crate::api::worksheet::format::_Format;
use crate::api::worksheet::hyperlink::_Hyperlink;
use crate::Format;
use crate::api::worksheet::WorkSheet;
use crate::result::WorkSheetResult;
use crate::xml::extension::{AddExtension, ExtensionType};

pub trait Write: _Write {
    fn write<L: Location, T: CellDisplay + CellValue>(&mut self, loc: L, data: T) -> WorkSheetResult<()> {
        self.write_display_all(&loc, &data, None)
    }
    fn write_string<L: Location>(&mut self, loc: L, data: String) -> WorkSheetResult<()> { self.write_display_all(&loc, &data, None) }
    fn write_number<L: Location>(&mut self, loc: L, data: i32) -> WorkSheetResult<()> { self.write_display_all(&loc, &data, None) }
    fn write_double<L: Location>(&mut self, loc: L, data: f64) -> WorkSheetResult<()> { self.write_display_all(&loc, &data, None) }
    fn write_boolean<L: Location>(&mut self, loc: L, data: bool) -> WorkSheetResult<()> { self.write_display_all(&loc, &data, None) }
    fn write_row<L: Location, T: CellDisplay + CellValue>(&mut self, loc: L, data: Iter<'_, T>) -> WorkSheetResult<()> {
        let (row, mut col) = loc.to_location();
        for data in data {
            self.write_display_all(&(row, col), data, None)?;
            col += 1;
        }
        Ok(())
    }
    fn write_column<L: Location, T: CellDisplay + CellValue>(&mut self, loc: L, data: Iter<'_, T>) -> WorkSheetResult<()> {
        let (mut row, col) = loc.to_location();
        for data in data {
            self.write_display_all(&(row, col), data, None)?;
            row += 1;
        }
        Ok(())
    }
    fn write_url<L: Location>(&mut self, loc: L, url: &str) -> WorkSheetResult<()> {
        self.write_hyperlink(&loc, url, url, None)
    }
    fn write_url_text<L: Location>(&mut self, loc: L, url: &str, data: &str) -> WorkSheetResult<()> {
        self.write_hyperlink(&loc, url, data, None)
    }
    fn merge_range<L: LocationRange, T: CellDisplay + CellValue>(&mut self, loc: L, data: T) -> WorkSheetResult<()> {
        self.merge_range_all(loc, data, None)
    }
    fn write_formula<L: Location>(&mut self, loc: L, data: &str) -> WorkSheetResult<()> { self.write_formula_all(&loc, data, FormulaType::Formula(loc.to_ref()), None) }
    fn write_array_formula<L: Location>(&mut self, loc: L, data: &str) -> WorkSheetResult<()> {
        // let loc = &loc_range.to_locations();
        self.write_formula_all(&loc, data, FormulaType::ArrayFormula(loc.to_ref()), None)
    }
    fn write_dynamic_array_formula<L: Location>(&mut self, loc: L, data: &str) -> WorkSheetResult<()> {
        self.write_formula_all(&loc, data, FormulaType::DynamicArrayFormula(loc.to_ref()), None)
    }

    fn write_with_format<L: Location, T: CellDisplay + CellValue>(&mut self, loc: L, data: T, format: &Format) -> WorkSheetResult<()> { self.write_display_all(&loc, &data, Some(format)) }
    fn write_string_with_format<L: Location>(&mut self, loc: L, data: String, format: &Format) -> WorkSheetResult<()> { self.write_display_all(&loc, &data, Some(format)) }
    fn write_number_with_format<L: Location>(&mut self, loc: L, data: i32, format: &Format) -> WorkSheetResult<()> { self.write_display_all(&loc, &data, Some(format)) }
    fn write_double_with_format<L: Location>(&mut self, loc: L, data: f64, format: &Format) -> WorkSheetResult<()> { self.write_display_all(&loc, &data, Some(format)) }
    fn write_boolean_with_format<L: Location>(&mut self, loc: L, data: bool, format: &Format) -> WorkSheetResult<()> { self.write_display_all(&loc, &data, Some(format)) }
    fn write_row_with_format<L: Location, T: CellDisplay + CellValue>(&mut self, loc: L, data: Iter<'_, T>, format: &Format) -> WorkSheetResult<()> {
        let (row, mut col) = loc.to_location();
        for data in data {
            self.write_display_all(&(row, col), data, Some(format))?;
            col += 1;
        }
        Ok(())
    }
    fn write_column_with_format<L: Location, T: CellDisplay + CellValue>(&mut self, loc: L, data: Iter<'_, T>, format: &Format) -> WorkSheetResult<()> {
        let (mut row, col) = loc.to_location();
        for data in data {
            self.write_display_all(&(row, col), data, Some(format))?;
            row += 1;
        }
        Ok(())
    }
    fn write_url_with_format<L: Location>(&mut self, loc: L, url: &str, format: &Format) -> WorkSheetResult<()> {
        self.write_hyperlink(&loc, url, url, Some(format))
    }
    fn write_url_data_with_format<L: Location>(&mut self, loc: L, url: &str, data: &str, format: &Format) -> WorkSheetResult<()> {
        self.write_hyperlink(&loc, url, data, Some(format))
    }
    fn write_formula_with_format<L: Location>(&mut self, loc: L, data: &str, format: &Format) -> WorkSheetResult<()> {
        self.write_formula_all(&loc, data, FormulaType::Formula(loc.to_ref()), Some(format))
    }
    fn write_array_formula_with_format<L: Location>(&mut self, loc: L, data: &str, format: &Format) -> WorkSheetResult<()> {
        self.write_formula_all(&loc.to_location(), data, FormulaType::ArrayFormula(loc.to_ref()), Some(format))
    }
    fn write_dynamic_array_formula_with_format<L: LocationRange>(&mut self, loc_range: L, data: &str, format: &Format) -> WorkSheetResult<()> {
        let loc = loc_range.to_range();
        self.write_formula_all(&(loc.0, loc.1), data, FormulaType::DynamicArrayFormula(loc_range.to_range_ref()), Some(format))
    }
    
    fn merge_range_with_format<L: LocationRange, T: CellDisplay + CellValue>(&mut self, loc: L, data: T, format:&Format) -> WorkSheetResult<()> {
        self.merge_range_all(loc, data, Some(format))
    }
}

pub(crate) trait _Write: _Format + _Hyperlink {
    fn write_display_all<L: Location, T: CellDisplay + CellValue>(&mut self, loc: &L, data: &T, format: Option<&Format>) -> WorkSheetResult<()>;
    fn write_formula_all<L: Location>(&mut self, loc: &L, formula: &str, formula_type: FormulaType, format: Option<&Format>) -> WorkSheetResult<()>;
    fn write_hyperlink<L: Location>(&mut self, loc: &L, url: &str, data: &str, format: Option<&Format>) -> WorkSheetResult<()>;
    fn merge_range_all<L: LocationRange, T: CellDisplay + CellValue>(&mut self, loc: L, data: T, format: Option<&Format>) -> WorkSheetResult<()>;
}

impl _Write for WorkSheet {
    fn write_display_all<L: Location, T: CellDisplay + CellValue>(&mut self, loc: &L, data: &T, format: Option<&Format>) -> WorkSheetResult<()> {
        let mut style = self.worksheet.get_default_style(loc);
        if let Some(format) = format {
            style = Some(self.add_format(format));
        }
        let worksheet = &mut self.worksheet;
        let sheet_data = &mut worksheet.sheet_data;
        sheet_data.write_display(loc, data, style)?;
        Ok(())
    }

    fn write_formula_all<L: Location>(&mut self, loc: &L, formula: &str, formula_type: FormulaType, format: Option<&Format>) -> WorkSheetResult<()> {
        let mut style = None;
        if let Some(format) = format {
            style = Some(self.add_format(format));
        }
        let worksheet = &mut self.worksheet;
        let sheet_data = &mut worksheet.sheet_data;
        // self.workbook_rel.borrow_mut().get_or_add_metadata();
        self.workbook_rel.borrow_mut().get_or_add_metadata();
        self.content_types.borrow_mut().add_metadata();
        self.metadata.borrow_mut().add_extension(ExtensionType::XdaDynamicArrayProperties);
        sheet_data.write_formula(loc, formula, formula_type, style)?;
        Ok(())
    }

    fn write_hyperlink<L: Location>(&mut self, loc: &L, url: &str, data: &str, format: Option<&Format>) -> WorkSheetResult<()> {
        self.write_display_all(loc, &data, format)?;
        let r_id = self.worksheet_rel.add_hyperlink(url);
        self.worksheet.add_hyperlink(loc, r_id);
        Ok(())
    }

    fn merge_range_all<L: LocationRange, T: CellDisplay + CellValue>(&mut self, loc: L, data: T, format: Option<&Format>) -> WorkSheetResult<()> {
        let (first_row, first_col, last_row, last_col) = loc.to_range();
        let worksheet = &mut self.worksheet;
        worksheet.add_merge_cell(first_row, first_col, last_row, last_col);
        for row in first_row..=last_row {
            for col in first_col..=last_col {
                self.write_display_all(&(row, col), &data, format)?;
            }
        }
        Ok(())
    }
}