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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
use std::{
    error::Error,
    net::SocketAddr,
    sync::{
        atomic::{self, AtomicU16},
        Arc,
        Mutex,
    },
};

use editor_rpc::{
    editor_server::{Editor, EditorServer},
    AuthorizeReply,
    AuthorizeRequest,
    DisconnectReply,
    DisconnectRequest,
    GetContentReply,
    GetContentRequest,
    SetCursorReply,
    SetCursorRequest,
    UpdateContentReply,
    UpdateContentRequest,
};
use tauri::{Manager, Window};
use tokio::task::JoinHandle;
use tonic::{transport::Server, Request, Response, Status};

use super::{
    utils::priority_lsit::{list_check_and_del, list_insert_or_replace_asc},
    ClientCursor,
    CursorAsc,
    Modification,
};
use crate::{
    dprintln,
    interface::remote::RpcServer,
    types::{
        middleware_types::{Tab, TabMap, UpdateContent},
        rpc_types::CursorList,
        ResultVoid,
    },
    utility::ptr::Ptr,
    APP_HANDLE,
    HISTORY,
};

pub mod editor_rpc {
    tonic::include_proto!("editor");
}

struct ServerHandle {
    window: Option<Window>,
    map_state: Mutex<(String, Option<Ptr<TabMap>>)>,
    password: Mutex<String>,
    clients: Mutex<Vec<SocketAddr>>,
    cursor_list: Arc<Mutex<CursorList>>,
    history: Arc<Mutex<Vec<Modification>>>,
}

impl Default for ServerHandle {
    fn default() -> Self {
        Self {
            window: None,
            map_state: Mutex::new((String::new(), None)),
            password: Mutex::new(String::new()),
            clients: Mutex::new(Vec::new()),
            cursor_list: Arc::new(Mutex::new(Default::default())),
            history: HISTORY.clone(),
        }
    }
}

impl ServerHandle {
    /// Change password for server.
    fn change_password(&self, password: &str) {
        let mut lock = self.password.lock().unwrap();
        *lock = password.to_string();
    }

    /// Update host cursor position.
    fn set_host_cursor(&mut self, row: u64, col: u64, port: u16) {
        let mut lock = self.cursor_list.lock().unwrap();
        list_insert_or_replace_asc::<CursorAsc>(
            &mut *lock,
            ClientCursor {
                addr: format!("0.0.0.0:{}", port).parse().unwrap(),
                row,
                col,
            },
        );
    }

    /// Handle logic current tab wht a `&mut Tab` as lambda parameter.
    /// Only use to bypass the fucking borrow checker.
    fn handle_with_cur_tab<F, R>(&self, handle: F) -> Result<R, String>
    where
        F: Fn(&mut Tab) -> Result<R, String>,
    {
        let map_state_lock = self.map_state.lock().unwrap();
        match map_state_lock.1 {
            Some(tab_map_ptr) => {
                let tab_map = tab_map_ptr.as_ref();
                let mut tabs_lock = tab_map.tabs.lock().unwrap();
                let mut tab = tabs_lock.get_mut(&map_state_lock.0).unwrap();
                handle(&mut tab)
            }
            None => Err("TabMap has not been initialized".to_string()),
        }
    }

    fn handle_rpc_with_cur_tab<F, R>(&self, handle: F) -> Result<Response<R>, Status>
    where
        F: Fn(&mut Tab) -> Result<R, String>,
    {
        match self.handle_with_cur_tab(handle) {
            Ok(success) => Ok(Response::new(success)),
            Err(err) => Err(Status::internal(err)),
        }
    }

    fn check_ip_authorized(&self, ip: SocketAddr) -> Result<(), Status> {
        let clients = self.clients.lock().unwrap();
        if clients.iter().any(|x| *x == ip) {
            Ok(())
        } else {
            Err(Status::unauthenticated("Unauthorized"))
        }
    }

    fn get_history_since<T>(&self, start_version: usize) -> Vec<T>
    where
        Modification: Into<T> + Clone,
    {
        let lock = self.history.lock().unwrap();
        lock[start_version..]
            .to_vec()
            .into_iter()
            .map(Into::into)
            .collect()
    }
}

#[tonic::async_trait]
impl Editor for Arc<Mutex<ServerHandle>> {
    async fn authorize(
        &self,
        request: Request<AuthorizeRequest>,
    ) -> Result<Response<AuthorizeReply>, Status> {
        let handler = self.lock().unwrap();
        handler.handle_rpc_with_cur_tab(|tab| {
            if request.get_ref().password == handler.password.lock().unwrap().as_str()
                || handler.password.lock().unwrap().is_empty()
            {
                if let Ok(_) = handler.check_ip_authorized(request.remote_addr().unwrap()) {
                    Ok(AuthorizeReply {
                        success: true,
                        file_name: tab.text.get_path_str(),
                        version: tab.text.get_version() as u64,
                        content: tab.text.to_string(),
                    })
                } else {
                    let mut client_lock = handler.clients.lock().unwrap();
                    client_lock.push(request.remote_addr().unwrap());
                    Ok(AuthorizeReply {
                        success: true,
                        file_name: tab.text.get_path_str(),
                        version: tab.text.get_version() as u64,
                        content: tab.text.to_string(),
                    })
                }
            } else {
                Ok(AuthorizeReply {
                    success: false,
                    file_name: String::new(),
                    version: 0,
                    content: String::new(),
                })
            }
        })
    }

    async fn disconnect(
        &self,
        request: Request<DisconnectRequest>,
    ) -> Result<Response<DisconnectReply>, Status> {
        let handler = self.lock().unwrap();
        handler.check_ip_authorized(request.remote_addr().unwrap())?;
        let mut clients = handler.clients.lock().unwrap();
        let mut success = false;
        if let Some(pos) = clients
            .iter()
            .position(|x| *x == request.remote_addr().unwrap())
        {
            clients.remove(pos);
            success = true;
            let mut cursor_lock = handler.cursor_list.lock().unwrap();
            _ = list_check_and_del::<CursorAsc>(
                &mut cursor_lock,
                &ClientCursor {
                    addr: request.remote_addr().unwrap(),
                    row: 0,
                    col: 0,
                },
            );
        }
        Ok(Response::new(DisconnectReply { success }))
    }

    async fn set_cursor(
        &self,
        request: Request<SetCursorRequest>,
    ) -> Result<Response<SetCursorReply>, Status> {
        let handler = self.lock().unwrap();
        handler.check_ip_authorized(request.remote_addr().unwrap())?;
        let mut cursor_lock = handler.cursor_list.lock().unwrap();
        list_insert_or_replace_asc::<CursorAsc>(
            &mut *cursor_lock,
            ClientCursor {
                addr: request.remote_addr().unwrap(),
                row: request.get_ref().row,
                col: request.get_ref().col,
            },
        );
        dprintln!("Set cursor: {:?}", cursor_lock);
        Ok(Response::new(SetCursorReply { success: true }))
    }

    async fn get_content(
        &self,
        request: Request<GetContentRequest>,
    ) -> Result<Response<GetContentReply>, Status> {
        let handler = self.lock().unwrap();
        handler.check_ip_authorized(request.remote_addr().unwrap())?;
        return handler.handle_rpc_with_cur_tab(
            |tab: &mut Tab| -> Result<GetContentReply, String> {
                if request.get_ref().full_content {
                    return Ok(GetContentReply {
                        history: vec![],
                        full_content: tab.text.to_string(),
                    });
                } else if request.get_ref().version == tab.text.get_version() as u64 {
                    Ok(GetContentReply {
                        history: vec![],
                        full_content: String::new(),
                    })
                } else {
                    Ok(GetContentReply {
                        history: handler.get_history_since(request.get_ref().version as usize),
                        full_content: String::new(),
                    })
                }
            },
        );
    }

    async fn update_content(
        &self,
        request: Request<UpdateContentRequest>,
    ) -> Result<Response<UpdateContentReply>, Status> {
        let handler = self.lock().unwrap();
        handler.check_ip_authorized(request.remote_addr().unwrap())?;
        let map_state_lock = handler.map_state.lock().unwrap();

        match map_state_lock.1 {
            Some(tab_map_ptr) => {
                let tab_map = tab_map_ptr.as_ref();
                let mut tabs_lock = tab_map.tabs.lock().unwrap();
                let tab = tabs_lock.get_mut(&map_state_lock.0).unwrap();
                let mut cursor_lock = handler.cursor_list.lock().unwrap();
                let request_ref = request.get_ref();
                let content_position = request_ref.op_range.clone().unwrap();
                let start = content_position.start.unwrap();
                let end = content_position.end.unwrap();

                // check version correct(up to date)
                if request_ref.version != tab.text.get_version() as u64 {
                    return Ok(Response::new(UpdateContentReply {
                        success: false,
                        message: format!(
                            "server version: {}, your version {}",
                            tab.text.get_version(),
                            request_ref.version
                        ),
                    }));
                }

                // check cursor position correct
                // for cursor in &*cursor_lock {
                //     if cursor.addr == request.remote_addr().unwrap()
                //         && (start.row != cursor.row || start.col != cursor.col)
                //         && (end.row != cursor.row || end.col != cursor.col)
                //     {
                //         return Ok(Response::new(UpdateContentReply {
                //             success: false,
                //             message: "miss matched cursor position".to_string(),
                //         }));
                //     }
                // }

                let mut history = handler.history.lock().unwrap();
                history.append(&mut vec![Modification::from(request_ref.clone())]);

                // handle operation
                match tab.text.merge_history(
                    &vec![Modification::from(request_ref.clone())],
                    &mut *cursor_lock,
                ) {
                    Ok(_) => {
                        #[cfg(not(test))]
                        {
                            let _ = APP_HANDLE.lock().unwrap().as_ref().unwrap().emit_all(
                                "front_update_content",
                                UpdateContent {
                                    file_name: tab.text.get_path_str(),
                                    op: request_ref.op,
                                    start: (start.row, start.col),
                                    end: (end.row, end.col),
                                    content: request_ref.modified_content.clone(),
                                },
                            );
                        }
                        Ok(Response::new(UpdateContentReply {
                            success: true,
                            message: String::new(),
                        }))
                    }
                    Err(e) => Ok(Response::new(UpdateContentReply {
                        success: false,
                        message: e.to_string(),
                    })),
                }
            }
            None => Err(Status::internal("TabMap have not been initialized")),
        }
    }
}

pub struct RpcServerImpl {
    port: AtomicU16,
    tokio_runtime: tokio::runtime::Runtime,
    rpc_server_handle: Option<JoinHandle<()>>,
    shared_handler: Arc<Mutex<ServerHandle>>,
}

impl RpcServerImpl {
    /// Start the service with a tab map.
    ///
    /// If the server is already running, return an error.
    pub fn start_server(
        &mut self,
        cur_tab_name: String,
        tab_map: Ptr<TabMap>,
        cursor_list: &Arc<Mutex<CursorList>>,
    ) -> ResultVoid {
        if self.is_running() {
            return Err("Server already running".into());
        }
        self.shared_handler.lock().unwrap().map_state = Mutex::new((cur_tab_name, Some(tab_map)));
        self.shared_handler.lock().unwrap().cursor_list = cursor_list.clone();
        self.start()?;
        Ok(())
    }

    pub fn stop_server(&mut self) {
        self.stop();
    }

    /// Set the port for the server.
    ///
    /// If the server is already running, return an error.
    pub fn set_port(&mut self, port: u16) -> Result<(), Box<dyn Error>> {
        if self.is_running() {
            return Err("Server already running".into());
        }
        self.port.store(port, atomic::Ordering::Relaxed);
        Ok(())
    }

    /// Change the password for the server.
    pub fn change_password(&mut self, new_password: &str) {
        let handler = self.shared_handler.lock().unwrap();
        handler.change_password(new_password);
    }

    /// Check if the server is running.
    pub fn is_running(&self) -> bool {
        self.rpc_server_handle.is_some()
    }

    /// Get the port of the server.
    pub fn get_port(&self) -> u16 {
        self.port.load(atomic::Ordering::Relaxed)
    }

    /// Update the host cursor position.
    ///
    /// - `row`: The row of the cursor.
    /// - `col`: The column of the cursor.
    /// TODO:  add out of range check
    pub fn set_host_cursor(&mut self, row: u64, col: u64) {
        self.shared_handler.lock().unwrap().set_host_cursor(
            row,
            col,
            self.port.load(atomic::Ordering::Relaxed),
        );
    }

    const fn get_ip() -> &'static str {
        #[cfg(test)]
        {
            "127.0.0.1"
        }
        #[cfg(not(test))]
        {
            "0.0.0.0"
        }
    }
}

impl Default for RpcServerImpl {
    fn default() -> Self {
        Self {
            port: AtomicU16::new(0),
            tokio_runtime: tokio::runtime::Builder::new_multi_thread()
                .worker_threads(8)
                .enable_all()
                .build()
                .unwrap(),
            rpc_server_handle: None,
            shared_handler: Arc::new(Mutex::new(Default::default())),
        }
    }
}

impl RpcServer for RpcServerImpl {
    fn start(&mut self) -> Result<(), Box<dyn Error>> {
        if self.rpc_server_handle.is_some() {
            return Err("Server already running".into());
        }

        let addr = format!(
            "{}:{}",
            RpcServerImpl::get_ip(),
            self.port.load(atomic::Ordering::Relaxed)
        )
        .parse()
        .unwrap();
        dprintln!("Server listening on: {}", addr);
        let handler = Arc::clone(&self.shared_handler);

        let server_handle = self.tokio_runtime.spawn(async move {
            Server::builder()
                .add_service(EditorServer::new(handler))
                .serve(addr)
                .await
                .unwrap();
        });
        self.rpc_server_handle = Some(server_handle);
        Ok(())
    }

    fn stop(&mut self) {
        if let Some(handle) = self.rpc_server_handle.take() {
            handle.abort();
        }
    }
}