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
use crate::datacell::{BlockChildType::*, Datacell::*};

use super::counters::Counters;
use regex::Regex;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub enum CommandType {
    #[default]
    INC,
    DEC,
    ASSIGN,
    INSERT,
}

impl CommandType {
    fn from_str(string: &str) -> Vec<Self> {
        let mut sub_string = string;
        if string.starts_with("<<") {
            sub_string = &string[2..];
        }

        let mut res = match sub_string {
            "::++" => vec![CommandType::INC, CommandType::INSERT],
            "::--" => vec![CommandType::DEC, CommandType::INSERT],
            "..++" => vec![CommandType::INC],
            "..--" => vec![CommandType::DEC],
            "::::" => vec![CommandType::INSERT],
            _ => vec![],
        };
        if string.starts_with("<<") {
            res.push(CommandType::ASSIGN); // align should be the last
        }
        res
    }

    fn to_tag_name(&self) -> String {
        match self {
            CommandType::INC => "CounterIncrement".to_string(),
            CommandType::DEC => "CounterDecrement".to_string(),
            CommandType::ASSIGN => "CounterAssign".to_string(),
            CommandType::INSERT => "CounterInsert".to_string(),
        }
    }

    pub fn from_tag_name(tag_name: &str) -> Option<Self> {
        match tag_name {
            "CounterIncrement" => Some(CommandType::INC),
            "CounterDecrement" => Some(CommandType::DEC),
            "CounterAssign" => Some(CommandType::ASSIGN),
            "CounterInsert" => Some(CommandType::INSERT),
            _ => None,
        }
    }
}

#[derive(Debug)]
pub struct CounterCommand<'a> {
    //commands: Vec<CommandType>,
    pub counters: &'a mut Counters,
    pub json_tree: String,
}

#[derive(Debug)]
pub struct LineSplits {
    pub content: String,
    pub is_command: bool,
}

impl<'a> CounterCommand<'a> {
    pub fn new(counters: &'a mut Counters, json_tree: &'a str) -> Self {
        Self {
            counters,
            json_tree: json_tree.to_string(),
        }
    }

    fn get_commands(&self, command_str: &str) -> Vec<CommandType> {
        let command_split = command_str.split("<<").collect::<Vec<&str>>();

        if command_split.len() == 1 {
            CommandType::from_str(&command_split[0][0..4])
        } else {
            CommandType::from_str(&("<<".to_string() + &command_split[1][0..4]))
        }
    }

    fn get_command_counter_name(&self, command_str: &str) -> String {
        let command_split = command_str.split("<<").collect::<Vec<&str>>();

        command_split.last().unwrap()[4..].to_string()
    }
    fn get_handle_name(&self, command_str: &str) -> Option<String> {
        let command_split = command_str.split("<<").collect::<Vec<&str>>();

        if command_split.len() == 1 {
            None
        } else {
            Some(command_split[0].to_string())
        }
    }

    pub fn has_counter_syntax(text: &str) -> bool {
        let cmd_regex = r"(^|[^\\])(\w+<<)?(::|\.\.)(::|\+\+|--)\w+";
        let re = Regex::new(cmd_regex).unwrap();
        re.is_match(text)
    }

    pub fn has_handle_insert_syntax(text: &str) -> bool {
        let handle_regex = r"(^|[^\\])(>>)\w+";
        let re = Regex::new(handle_regex).unwrap();
        re.is_match(text)
    }

    fn split_line(&self, line: &str, handle_insert_command: bool) -> Vec<LineSplits> {
        let cmd_regex = if handle_insert_command {
            r"(^|[^\\])(>>)\w+"
        } else {
            r"(^|[^\\])(\w+<<)?(::|\.\.)(::|\+\+|--)\w+"
        };

        // Create the regex object
        let re = Regex::new(cmd_regex).unwrap();

        // Find all matches and split the string accordingly
        let mut result = Vec::new();
        let mut start = 0;

        for mat in re.find_iter(line) {
            let match_first_char = &line[mat.start()..mat.start() + 1];

            if mat.start() != start {
                //if first char of found regex is not a space, add the previous part of the string
                let mut string = String::from(&line[start..mat.start()]);
                string.push_str(match_first_char);
                result.push(LineSplits {
                    content: string,
                    is_command: false,
                });
            }

            let mut string = String::new();
            if mat.start() > 0 {
                string.push_str(&mat.as_str()[1..]);
            } else {
                string.push_str(&mat.as_str());
            }

            result.push(LineSplits {
                content: string,
                is_command: true,
            });
            start = mat.end();
        }

        // Add the remaining part of the string, if any
        if start < line.len() {
            result.push(LineSplits {
                content: line[start..].to_string(),
                is_command: false,
            });
        }

        result
    }

    pub fn run(&mut self, json: &mut DataCell) -> String {
        if self.counters.counters_list.is_empty() {
            return self.json_tree.clone();
        }
        let json_str = self.replace_counters(json, false);
        let mut json: DataCell = serde_json::from_str(&json_str).unwrap();
        let json_str = self.replace_counters(&mut json, true);

        json_str
    }

    pub fn replace_counters(&mut self, cell: &mut DataCell, handle_insert_command: bool) -> String {
        let cloned_cell = cell.clone();

        match &mut cell.cell_type {
            CellType::Block(block) => {
                if (block.has_counter_commands && !handle_insert_command)
                    || (block.has_handle_insert && handle_insert_command)
                {
                    block.children.iter_mut().for_each(|child| {
                        self.handle_block_child(child, &cloned_cell, handle_insert_command)
                    });
                }
            }
            CellType::Element(el) => el.children.iter_mut().for_each(|child| {
                self.replace_counters(child, handle_insert_command);
            }),
            CellType::Root(el) => el.children.iter_mut().for_each(|child| {
                self.replace_counters(child, handle_insert_command);
            }),
            _ => (),
        }

        serde_json::to_string(&cell).unwrap()
    }

    fn handle_block_child(
        &mut self,
        block_child: &mut BlockChildType,
        cell: &DataCell,
        handle_insert_command: bool,
    ) {
        match block_child {
            BlockChildType::Text(text) => {
                text.content = if handle_insert_command {
                    self.insert_handle_value(&text.content)
                } else {
                    self.replace_counter_value(&text.content, cell)
                }
            }
            BlockChildType::Delimited(d) => {
                d.terminal = if handle_insert_command {
                    self.insert_handle_value(&d.terminal)
                } else {
                    self.replace_counter_value(&d.terminal, cell)
                };
            }
        };
    }

    fn replace_counter_value(&mut self, text: &str, cell: &DataCell) -> String {
        let splits = self.split_line(&text, false);
        let mut res = String::new();

        for split in splits {
            if split.is_command {
                let counter_name = &self.get_command_counter_name(split.content.as_str());
                self.counters.check_scope(
                    &cell,
                    counter_name,
                    &serde_json::from_str(&self.json_tree).unwrap(),
                );

                let commands = self.get_commands(split.content.as_str());

                let handle_name = self.get_handle_name(split.content.as_str());

                for command in commands {
                    let execution = self.counters.execute(command, counter_name, &handle_name);
                    if execution.is_some() {
                        if &split.content.as_str()[0..1] == " " {
                            res.push_str(" ");
                        }
                        res.push_str(&execution.unwrap());
                    }
                }
            } else {
                res.push_str(&split.content);
            }
        }

        res
    }

    fn insert_handle_value(&mut self, text: &str) -> String {
        let splits = self.split_line(&text, true);

        let mut res = String::new();
        for split in splits {
            if split.is_command {
                let handle = self
                    .counters
                    .handles_list
                    .iter()
                    .find(|h| h.name == &split.content[2..]);
                if handle.is_some() {
                    res.push_str(&handle.unwrap().value);
                };
            } else {
                res.push_str(&split.content);
            }
        }

        res
    }
}

//create a test for replacecounter_in_line function
// #[test]
// fn test_replace_counter_in_line() {
//     let mut counters = Counters::new();
//     counters.add_counter(CounterInstance::new(
//         "TestArabicCounter",
//         "counter",
//         0,
//         None,
//     ));
//     counters.add_counter(CounterInstance::new(
//         "TestRomanCounter",
//         "roman_counter",
//         0,
//         None,
//     ));

//     //with text after
//     let test1 =
//         replace_counter_in_line("askljdklasj ::::TestArabicCounter qweqweqwe", &mut counters);
//     assert_eq!(test1, "askljdklasj 0 qweqweqwe");

//     //no text after
//     let test2 = replace_counter_in_line("askljdklasj ::::TestArabicCounter", &mut counters);
//     assert_eq!(test2, "askljdklasj 0");

//     // 2 same type counters in 1 line
//     let test3 = replace_counter_in_line(
//         "askljdklasj ::::TestArabicCounter hi ::::TestArabicCounter",
//         &mut counters,
//     );
//     assert_eq!(test3, "askljdklasj 0 hi 0");

//     // 2 different type counters in 1 line
//     let test4 = replace_counter_in_line(
//         "askljdklasj ::::TestArabicCounter hi ::::TestRomanCounter",
//         &mut counters,
//     );
//     assert_eq!(test4, "askljdklasj 0 hi 0");

//     // test increment
//     let test4 = replace_counter_in_line(
//         "askljdklasj ::++TestArabicCounter hi ::++TestRomanCounter",
//         &mut counters,
//     );
//     assert_eq!(test4, "askljdklasj 1 hi i");

//     // test double increment than normal
//     let test4 = replace_counter_in_line(
//         "inc ::++TestRomanCounter another inc ::++TestRomanCounter fix ::::TestRomanCounter",
//         &mut counters,
//     );
//     assert_eq!(test4, "inc ii another inc iii fix iii");

//     // test double deccrement than normal
//     let test4 = replace_counter_in_line(
//         "dec ::--TestRomanCounter another dec ::--TestRomanCounter fix ::::TestRomanCounter dec ar ::--TestArabicCounter fix ar ::::TestArabicCounter",
//         &mut counters,
//     );
//     assert_eq!(test4, "dec ii another dec i fix i dec ar 0 fix ar 0");

//     // test escaped
//     let test4 = replace_counter_in_line(r#"escaped \::--TestRomanCounter"#, &mut counters);
//     assert_eq!(test4, r"escaped \::--TestRomanCounter");
// }

// #[test]
// fn decrement_less_than_0() {
//     let mut counters = Counters::new();
//     counters.add_counter(CounterInstance::new(
//         "TestArabicCounter",
//         "counter",
//         0,
//         None,
//     ));
//     counters.add_counter(CounterInstance::new(
//         "TestRomanCounter",
//         "roman_counter",
//         0,
//         None,
//     ));

//     let test =
//         replace_counter_in_line("::--TestArabicCounter ::--TestArabicCounter", &mut counters);
//     assert_eq!(test, "- -");

//     let test2 =
//         replace_counter_in_line("::::TestRomanCounter ::--TestArabicCounter", &mut counters);
//     assert_eq!(test2, "0 -");
// }

// #[test]
// fn silent_operations() {
//     let mut counters = Counters::new();
//     counters.add_counter(CounterInstance::new(
//         "TestArabicCounter",
//         "counter",
//         0,
//         None,
//     ));

//     let test =
//         replace_counter_in_line("::++TestArabicCounter ..--TestArabicCounter", &mut counters);
//     assert_eq!(test, "1 ");

//     let test2 = replace_counter_in_line("::::TestArabicCounter", &mut counters);
//     assert_eq!(test2, "0");
// }

// #[test]
// fn default_value() {
//     let mut counters = Counters::new();
//     counters.add_counter(CounterInstance::new(
//         "TestArabicCounter",
//         "counter",
//         0,
//         Some("10"),
//     ));
//     counters.add_counter(CounterInstance::new(
//         "TestRomanCounter",
//         "roman_counter",
//         0,
//         Some("ii"),
//     ));

//     let test = replace_counter_in_line("::--TestArabicCounter", &mut counters);
//     assert_eq!(test, "9");

//     let test2 = replace_counter_in_line("::::TestRomanCounter ::--TestRomanCounter", &mut counters);
//     assert_eq!(test2, "ii i");
// }