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
use std::{
    error::Error,
    path::{Path, PathBuf},
    sync::{Arc, Mutex},
    time::SystemTime,
};

use ropey::Rope;

use crate::{
    dprintln,
    interface::storage::{
        BasicFile,
        FileShareStatus::{self, Client, Private, Server},
        HistorianFile,
        MFile,
    },
    io::file_io,
    remote::{server::editor_rpc::OperationType, Modification},
    types::{rpc_types::CursorList, ResultVoid},
    utility::text_helper::{all_to_lf, lines_count},
    CURSOR_LIST,
    HISTORY,
};

pub struct ConcurrencyShare {
    mutex: Mutex<()>,
    update_thread: Option<std::thread::JoinHandle<()>>,
    cursor_list: Option<Arc<Mutex<CursorList>>>,
}

pub struct Text {
    share_status: FileShareStatus,
    data: Box<Rope>,
    path: PathBuf,
    version: usize,
    dirty: bool,
    last_modified: SystemTime,
    concurrent_share: ConcurrencyShare,
}

impl BasicFile<Rope, Modification> for Text {
    fn get_path(&self) -> &PathBuf {
        &self.path
    }
    fn get_path_str(&self) -> String {
        self.path.to_str().unwrap().to_string()
    }

    fn is_dirty(&self) -> bool {
        self.dirty
    }

    fn set_dirty(&mut self, dirty: bool) {
        self.dirty = dirty;
    }

    fn to_string(&self) -> String {
        self.data.as_ref().to_string()
    }

    fn save(&mut self) -> ResultVoid {
        file_io::write_file(self.path.as_path(), &self.data.as_ref().to_string())?;
        self.dirty = false;
        Ok(())
    }

    fn get_raw(&mut self) -> &mut Rope {
        self.data.as_mut()
    }

    fn handle_modify(&mut self, modify: &Modification) -> ResultVoid {
        dprintln!("handle_modify, version {}", self.version);
        let modified_content = all_to_lf(&modify.modified_content);
        match &self.share_status {
            Private => {
                let raw_rope = self.data.as_mut();
                let range = &modify.op_range;
                let start_idx =
                    raw_rope.line_to_char(range.start.row as usize) + range.start.col as usize;
                let end_idx =
                    raw_rope.line_to_char(range.end.row as usize) + range.end.col as usize;

                match modify.op {
                    OperationType::Insert => {
                        raw_rope.insert(start_idx, &modified_content);
                    }
                    OperationType::Delete => {
                        raw_rope.remove(start_idx..end_idx);
                    }
                    OperationType::Replace => {
                        raw_rope.remove(start_idx..end_idx);
                        raw_rope.insert(start_idx, &modified_content);
                    }
                }
                self.dirty = true;
                Ok(())
            }
            Server => {
                let cursor_list = self.concurrent_share.cursor_list.as_ref().unwrap().clone();
                let mut histories_mutex = HISTORY.lock().unwrap();
                histories_mutex.push(modify.clone());
                self.merge_history(&vec![modify.clone()], &mut cursor_list.lock().unwrap())?;
                self.dirty = true;
                Ok(())
            }
            Client => {
                let concur_lock = self.concurrent_share.mutex.lock().unwrap();

                let raw_rope = self.data.as_mut();
                let range = &modify.op_range;
                let start_idx =
                    raw_rope.line_to_char(range.start.row as usize) + range.start.col as usize;
                let end_idx =
                    raw_rope.line_to_char(range.end.row as usize) + range.end.col as usize;

                match modify.op {
                    OperationType::Insert => {
                        raw_rope.insert(start_idx, &modified_content);
                    }
                    OperationType::Delete => {
                        raw_rope.remove(start_idx..end_idx);
                    }
                    OperationType::Replace => {
                        raw_rope.remove(start_idx..end_idx);
                        raw_rope.insert(start_idx, &modified_content);
                    }
                }
                self.version += 1;
                self.dirty = true;
                Ok(())
            }
        }
    }
}

impl Text {
    pub fn from_path(file_path: &Path) -> Result<Self, Box<dyn Error>> {
        match file_io::read_file(file_path) {
            Ok(content) => match file_io::get_last_modified(file_path) {
                Ok(last_modified) => Ok(Text {
                    share_status: FileShareStatus::default(),
                    data: Box::new(Rope::from_str(&all_to_lf(&content))),
                    path: PathBuf::from(file_path),
                    version: 0,
                    dirty: false,
                    last_modified,
                    concurrent_share: ConcurrencyShare {
                        mutex: Mutex::new(()),
                        update_thread: None,
                        cursor_list: None,
                    },
                }),
                Err(e) => Err(e),
            },
            Err(e) => Err(e),
        }
    }

    pub fn from_path_str(file_path: &str) -> Result<Self, Box<dyn Error>> {
        Text::from_path(Path::new(file_path))
    }

    pub fn from_str(file_path: &Path, text: &str) -> Self {
        Text {
            share_status: FileShareStatus::default(),
            data: Box::new(Rope::from_str(text)),
            path: file_path.to_path_buf(),
            version: 0,
            dirty: false,
            last_modified: SystemTime::now(),
            concurrent_share: ConcurrencyShare {
                mutex: Mutex::new(()),
                update_thread: None,
                cursor_list: None,
            },
        }
    }
}

impl HistorianFile<Rope, Modification, CursorList> for Text {
    fn get_version(&self) -> usize {
        self.version
    }

    fn get_share_status(&self) -> FileShareStatus {
        self.share_status.clone()
    }

    fn merge_history(&mut self, modifies: &[Modification], cursors: &mut CursorList) -> ResultVoid {
        let concur_lock = self.concurrent_share.mutex.lock().unwrap();

        for modify in modifies {
            let increase_lines = lines_count(&modify.modified_content);
            let raw_rope = self.data.as_mut();
            let range = &modify.op_range;
            let start_idx =
                raw_rope.line_to_char(range.start.row as usize) + range.start.col as usize;
            let end_idx = raw_rope.line_to_char(range.end.row as usize) + range.end.col as usize;
            // let mut changed_lines;

            match modify.op {
                OperationType::Insert => {
                    raw_rope.insert(start_idx, &modify.modified_content);
                    // changed_lines = increase_lines;
                }
                OperationType::Delete => {
                    raw_rope.remove(start_idx..end_idx);
                    // changed_lines = (range.end.row - range.start.row) as
                    // usize;
                }
                OperationType::Replace => {
                    raw_rope.remove(start_idx..end_idx);
                    raw_rope.insert(start_idx, &modify.modified_content);
                    // changed_lines = increase_lines - (range.end.row -
                    // range.start.row) as usize;
                }
            }

            // let mut cursors_to_update = get_cursor::<CursorRowEq>(
            //     cursors,
            //     &ClientCursor {
            //         addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0,
            // 1)), 0),         row: range.start.row,
            //         col: range.start.col,
            //     },
            // )
            // .unwrap();
            //
            // if changed_lines == 0 {
            //     cursors_to_update.current().unwrap().col +=
            //         modify.op_range.end.col - modify.op_range.start.col;
            // } else {
            //     cursors_to_update.current().unwrap().col += {
            //         let idx = modify.modified_content.rfind("\n").unwrap();
            //         (modify.modified_content.len() - idx - 1) as u64
            //     };
            //     loop {
            //         cursors_to_update.move_next();
            //         match cursors_to_update.current() {
            //             Some(cursor) => {
            //                 cursor.row += changed_lines as u64;
            //             }
            //             None => break,
            //         }
            //     }
            // }
        }
        self.dirty = true;
        self.version += modifies.len();
        Ok(())
    }

    fn change_share_status(&mut self, status: FileShareStatus) -> bool {
        if self.share_status == Server && status == Private {
            self.share_status = status;
            true
        } else if self.share_status == Private && status == Server {
            self.concurrent_share = ConcurrencyShare {
                mutex: Mutex::new(()),
                update_thread: None,
                cursor_list: Some(CURSOR_LIST.clone()),
            };
            self.share_status = status;
            true
        } else if self.share_status == Private && status == Client {
            self.concurrent_share = ConcurrencyShare {
                mutex: Mutex::new(()),
                update_thread: None,
                cursor_list: None,
            };
            self.share_status = status;
            true
        } else {
            false
        }
    }
}

impl MFile<Rope, Modification, CursorList> for Text {}

/// AI-generated-content
/// tool: Copilot
/// version: v0.1.0
/// usage: Test the implementation of the Text struct rope_store
#[cfg(test)]
mod rope_test {
    use super::*;
    use crate::{
        interface::storage::BasicFile,
        remote::{server::editor_rpc::OperationType, Modification, OpRange},
        types::rpc_types::CursorPosition,
    };

    fn get_full_path(name: &str) -> String {
        match std::env::var("TEMP") {
            Ok(val) => format!("{}/{}", val, name),
            Err(_e) => format!("/tmp/{}", name),
        }
    }

    #[test]
    fn test_get_path() {
        let file_name = get_full_path("moras_test.txt");
        std::fs::write(&file_name, "Hello, world!\nThis is a test file.\n").unwrap();
        let file_path = PathBuf::from(&file_name);
        let text = Text::from_path(&file_path).unwrap();

        assert_eq!(text.get_path(), &file_path);
    }

    #[test]
    fn test_get_path_str() {
        let file_name = get_full_path("moras_test.txt");
        std::fs::write(&file_name, "Hello, world!\nThis is a test file.\n").unwrap();
        let file_path = PathBuf::from(&file_name);
        let text = Text::from_path(&file_path).unwrap();

        assert_eq!(text.get_path_str(), file_name);
    }

    #[test]
    fn test_is_dirty() {
        let file_name = get_full_path("moras_test.txt");
        std::fs::write(&file_name, "Hello, world!\nThis is a test file.\n").unwrap();
        let file_path = PathBuf::from(&file_name);
        let mut text = Text::from_path(&file_path).unwrap();

        assert_eq!(text.is_dirty(), false);

        text.set_dirty(true);
        assert_eq!(text.is_dirty(), true);
    }

    #[test]
    fn test_to_string() {
        let file_name = get_full_path("moras_test2.txt");
        std::fs::write(&file_name, "Hello, world!\nThis is a test file.\n").unwrap();
        let file_path = PathBuf::from(&file_name);
        let text = Text::from_path(&file_path).unwrap();

        assert_eq!(text.to_string(), "Hello, world!\nThis is a test file.\n");
    }

    #[test]
    fn test_save() {
        let file_name = get_full_path("moras_test.txt");
        std::fs::write(&file_name, "Hello, world!\nThis is a test file.\n").unwrap();
        let file_path = PathBuf::from(&file_name);
        let mut text = Text::from_path(&file_path).unwrap();

        text.set_dirty(true);
        let _ = text.save();

        assert_eq!(text.is_dirty(), false);
    }

    #[test]
    fn test_get_raw() {
        let file_name = get_full_path("moras_test.txt");
        std::fs::write(&file_name, "Hello, world!\nThis is a test file.\n").unwrap();
        let file_path = PathBuf::from(&file_name);
        let mut text = Text::from_path(&file_path).unwrap();

        let path = text.get_path_str();

        assert_eq!(path.len(), file_name.len());
    }

    #[test]
    fn test_handle_modify() {
        let file_name = get_full_path("moras_test1.txt");
        std::fs::write(&file_name, "Hello, world!\nThis is a test file.\n").unwrap();
        let file_path = PathBuf::from(&file_name);
        let mut text = Text::from_path(&file_path).unwrap();

        let modify = Modification {
            op: OperationType::Insert,
            version: 0,
            op_range: OpRange {
                start: CursorPosition { row: 0, col: 0 },
                end: CursorPosition { row: 0, col: 0 },
            },
            modified_content: "Test".to_string(),
        };

        text.handle_modify(&modify).unwrap();

        assert_eq!(
            text.to_string(),
            "TestHello, world!\nThis is a test file.\n"
        );
    }
}