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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
use std::fmt;

pub trait CommandClone {
    fn clone_command(&self) -> Box<dyn Command>;
}

pub trait Command: CommandClone {
    fn name(&self) -> &str;
    fn description(&self) -> &str;
    fn execute(&self, context: &mut CommandContext) -> CommandResult;
    fn undo(&self, context: &mut CommandContext) -> CommandResult;
    fn preview(&self, context: &CommandContext) -> Option<super::super::data_structure::Entity>;
    fn requires_selection(&self) -> bool;
    fn get_required_entity_types(&self) -> &[&'static str];
    fn is_undoable(&self) -> bool;
    fn receive_input(&mut self, _input: &str) -> CommandResult {
        CommandResult::Failed("命令不接受交互输入".to_string())
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum CommandResult {
    Success,
    Failed(String),
    Canceled,
    RequireInput(String),
}

impl CommandResult {
    pub fn is_success(&self) -> bool {
        matches!(self, CommandResult::Success)
    }

    pub fn is_failed(&self) -> bool {
        matches!(self, CommandResult::Failed(_))
    }

    pub fn is_canceled(&self) -> bool {
        matches!(self, CommandResult::Canceled)
    }

    pub fn error_message(&self) -> Option<&str> {
        match self {
            CommandResult::Failed(msg) => Some(msg),
            _ => None,
        }
    }
}

impl fmt::Display for CommandResult {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            CommandResult::Success => write!(f, "Success"),
            CommandResult::Failed(msg) => write!(f, "Failed: {}", msg),
            CommandResult::Canceled => write!(f, "Canceled"),
            CommandResult::RequireInput(prompt) => write!(f, "Require Input: {}", prompt),
        }
    }
}

pub struct CommandContext<'a> {
    pub document: Option<&'a mut super::super::data_structure::Document>,
    pub active_layer: String,
    pub current_ucs: crate::math::Matrix4,
    pub selection_set: crate::selection::SelectionSet,
    pub active_block: String,
    pub viewport: Option<crate::render::viewport::Viewport>,
    pub current_point: Option<crate::geometry::Point>,
    pub user_data: std::collections::HashMap<String, Box<dyn std::any::Any>>,
}

impl<'a> Default for CommandContext<'a> {
    fn default() -> Self {
        Self {
            document: None,
            active_layer: "0".to_string(),
            current_ucs: crate::math::Matrix4::identity(),
            selection_set: crate::selection::SelectionSet::new(),
            active_block: "ModelSpace".to_string(),
            viewport: None,
            current_point: None,
            user_data: std::collections::HashMap::new(),
        }
    }
}

impl<'a> CommandContext<'a> {
    pub fn new(document: &'a mut super::super::data_structure::Document) -> Self {
        Self {
            document: Some(document),
            active_layer: "0".to_string(),
            current_ucs: crate::math::Matrix4::identity(),
            selection_set: crate::selection::SelectionSet::new(),
            active_block: "ModelSpace".to_string(),
            viewport: None,
            current_point: None,
            user_data: std::collections::HashMap::new(),
        }
    }

    pub fn get_document(&self) -> Option<&super::super::data_structure::Document> {
        self.document.as_deref()
    }

    pub fn get_document_mut(&mut self) -> Option<&mut super::super::data_structure::Document> {
        self.document.as_deref_mut()
    }

    pub fn set_user_data<T: 'static>(&mut self, key: impl Into<String>, value: T) {
        self.user_data.insert(key.into(), Box::new(value));
    }

    pub fn get_user_data<T: 'static>(&self, key: &str) -> Option<&T> {
        self.user_data.get(key).and_then(|boxed| {
            boxed.downcast_ref::<T>()
        })
    }

    pub fn take_user_data<T: 'static>(&mut self, key: &str) -> Option<Box<T>> {
        self.user_data.remove(key).and_then(|boxed| {
            boxed.downcast::<T>().ok()
        })
    }

    pub fn add_selection(&mut self, entity_id: super::super::data_structure::ObjectId) {
        self.selection_set.add(entity_id);
    }

    pub fn clear_selection(&mut self) {
        self.selection_set.clear();
    }

    pub fn get_selected_entities(&self) -> Vec<super::super::data_structure::ObjectId> {
        self.selection_set.get_selected().to_vec()
    }
}

pub struct CommandBuilder {
    name: String,
    description: String,
    command_type: CommandType,
}

impl Default for CommandBuilder {
    fn default() -> Self {
        Self {
            name: String::new(),
            description: String::new(),
            command_type: CommandType::EntityOperation,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CommandType {
    EntityOperation,
    DrawingAid,
    DisplayControl,
    FileOperation,
    LayerControl,
    BlockOperation,
    Dimension,
    Text,
    Selection,
    Unknown,
}

impl CommandBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = name.into();
        self
    }

    pub fn description(mut self, desc: impl Into<String>) -> Self {
        self.description = desc.into();
        self
    }

    pub fn command_type(mut self, cmd_type: CommandType) -> Self {
        self.command_type = cmd_type;
        self
    }

    pub fn build(self) -> Result<Box<dyn Command>, crate::error::CadError> {
        use crate::error::CadError;

        if self.name.is_empty() {
            return Err(CadError::command("build", "命令名称不能为空"));
        }

        if self.description.is_empty() {
            return Err(CadError::command(&self.name, "命令描述不能为空"));
        }

        match self.command_type {
            CommandType::EntityOperation => {
                Ok(Box::new(EntityCommand::new(self.name, self.description)))
            }
            CommandType::DrawingAid => {
                Ok(Box::new(DrawingAidCommand::new(self.name, self.description)))
            }
            CommandType::DisplayControl => {
                Ok(Box::new(DisplayControlCommand::new(self.name, self.description)))
            }
            CommandType::FileOperation => {
                Ok(Box::new(FileOperationCommand::new(self.name, self.description)))
            }
            CommandType::LayerControl => {
                Ok(Box::new(LayerControlCommand::new(self.name, self.description)))
            }
            CommandType::BlockOperation => {
                Ok(Box::new(BlockOperationCommand::new(self.name, self.description)))
            }
            CommandType::Dimension => {
                Ok(Box::new(DimensionCommand::new(self.name, self.description)))
            }
            CommandType::Text => {
                Ok(Box::new(TextCommand::new(self.name, self.description)))
            }
            CommandType::Selection => {
                Ok(Box::new(SelectionCommand::new(self.name, self.description)))
            }
            CommandType::Unknown => {
                Ok(Box::new(GenericCommand::new(self.name, self.description,
                    |_| CommandResult::Failed("命令未实现".to_string()),
                    |_| CommandResult::Failed("无法撤销未实现的命令".to_string())
                )))
            }
        }
    }
}

#[derive(Clone)]
struct EntityCommand {
    name: String,
    description: String,
}

impl EntityCommand {
    fn new(name: String, description: String) -> Self {
        Self { name, description }
    }
}

impl CommandClone for EntityCommand {
    fn clone_command(&self) -> Box<dyn Command> { Box::new(self.clone()) }
}

impl Command for EntityCommand {
    fn name(&self) -> &str { &self.name }
    fn description(&self) -> &str { &self.description }
    fn execute(&self, _: &mut CommandContext) -> CommandResult { CommandResult::Success }
    fn undo(&self, _: &mut CommandContext) -> CommandResult { CommandResult::Success }
    fn preview(&self, _: &CommandContext) -> Option<super::super::data_structure::Entity> { None }
    fn requires_selection(&self) -> bool { true }
    fn get_required_entity_types(&self) -> &[&'static str] { &[] }
    fn is_undoable(&self) -> bool { true }
}

#[derive(Clone)]
struct DrawingAidCommand {
    name: String,
    description: String,
}

impl DrawingAidCommand {
    fn new(name: String, description: String) -> Self {
        Self { name, description }
    }
}

impl CommandClone for DrawingAidCommand {
    fn clone_command(&self) -> Box<dyn Command> { Box::new(self.clone()) }
}

impl Command for DrawingAidCommand {
    fn name(&self) -> &str { &self.name }
    fn description(&self) -> &str { &self.description }
    fn execute(&self, _: &mut CommandContext) -> CommandResult { CommandResult::Success }
    fn undo(&self, _: &mut CommandContext) -> CommandResult { CommandResult::Success }
    fn preview(&self, _: &CommandContext) -> Option<super::super::data_structure::Entity> { None }
    fn requires_selection(&self) -> bool { false }
    fn get_required_entity_types(&self) -> &[&'static str] { &[] }
    fn is_undoable(&self) -> bool { false }
}

#[derive(Clone)]
struct DisplayControlCommand {
    name: String,
    description: String,
}

impl DisplayControlCommand {
    fn new(name: String, description: String) -> Self {
        Self { name, description }
    }
}

impl CommandClone for DisplayControlCommand {
    fn clone_command(&self) -> Box<dyn Command> { Box::new(self.clone()) }
}

impl Command for DisplayControlCommand {
    fn name(&self) -> &str { &self.name }
    fn description(&self) -> &str { &self.description }
    fn execute(&self, _: &mut CommandContext) -> CommandResult { CommandResult::Success }
    fn undo(&self, _: &mut CommandContext) -> CommandResult { CommandResult::Success }
    fn preview(&self, _: &CommandContext) -> Option<super::super::data_structure::Entity> { None }
    fn requires_selection(&self) -> bool { false }
    fn get_required_entity_types(&self) -> &[&'static str] { &[] }
    fn is_undoable(&self) -> bool { false }
}

#[derive(Clone)]
struct FileOperationCommand {
    name: String,
    description: String,
}

impl FileOperationCommand {
    fn new(name: String, description: String) -> Self {
        Self { name, description }
    }
}

impl CommandClone for FileOperationCommand {
    fn clone_command(&self) -> Box<dyn Command> { Box::new(self.clone()) }
}

impl Command for FileOperationCommand {
    fn name(&self) -> &str { &self.name }
    fn description(&self) -> &str { &self.description }
    fn execute(&self, _: &mut CommandContext) -> CommandResult { CommandResult::Success }
    fn undo(&self, _: &mut CommandContext) -> CommandResult { CommandResult::Success }
    fn preview(&self, _: &CommandContext) -> Option<super::super::data_structure::Entity> { None }
    fn requires_selection(&self) -> bool { false }
    fn get_required_entity_types(&self) -> &[&'static str] { &[] }
    fn is_undoable(&self) -> bool { false }
}

#[derive(Clone)]
struct LayerControlCommand {
    name: String,
    description: String,
}

impl LayerControlCommand {
    fn new(name: String, description: String) -> Self {
        Self { name, description }
    }
}

impl CommandClone for LayerControlCommand {
    fn clone_command(&self) -> Box<dyn Command> { Box::new(self.clone()) }
}

impl Command for LayerControlCommand {
    fn name(&self) -> &str { &self.name }
    fn description(&self) -> &str { &self.description }
    fn execute(&self, _: &mut CommandContext) -> CommandResult { CommandResult::Success }
    fn undo(&self, _: &mut CommandContext) -> CommandResult { CommandResult::Success }
    fn preview(&self, _: &CommandContext) -> Option<super::super::data_structure::Entity> { None }
    fn requires_selection(&self) -> bool { true }
    fn get_required_entity_types(&self) -> &[&'static str] { &[] }
    fn is_undoable(&self) -> bool { true }
}

#[derive(Clone)]
struct BlockOperationCommand {
    name: String,
    description: String,
}

impl BlockOperationCommand {
    fn new(name: String, description: String) -> Self {
        Self { name, description }
    }
}

impl CommandClone for BlockOperationCommand {
    fn clone_command(&self) -> Box<dyn Command> { Box::new(self.clone()) }
}

impl Command for BlockOperationCommand {
    fn name(&self) -> &str { &self.name }
    fn description(&self) -> &str { &self.description }
    fn execute(&self, _: &mut CommandContext) -> CommandResult { CommandResult::Success }
    fn undo(&self, _: &mut CommandContext) -> CommandResult { CommandResult::Success }
    fn preview(&self, _: &CommandContext) -> Option<super::super::data_structure::Entity> { None }
    fn requires_selection(&self) -> bool { true }
    fn get_required_entity_types(&self) -> &[&'static str] { &[] }
    fn is_undoable(&self) -> bool { true }
}

#[derive(Clone)]
struct DimensionCommand {
    name: String,
    description: String,
}

impl DimensionCommand {
    fn new(name: String, description: String) -> Self {
        Self { name, description }
    }
}

impl CommandClone for DimensionCommand {
    fn clone_command(&self) -> Box<dyn Command> { Box::new(self.clone()) }
}

impl Command for DimensionCommand {
    fn name(&self) -> &str { &self.name }
    fn description(&self) -> &str { &self.description }
    fn execute(&self, _: &mut CommandContext) -> CommandResult { CommandResult::Success }
    fn undo(&self, _: &mut CommandContext) -> CommandResult { CommandResult::Success }
    fn preview(&self, _: &CommandContext) -> Option<super::super::data_structure::Entity> { None }
    fn requires_selection(&self) -> bool { true }
    fn get_required_entity_types(&self) -> &[&'static str] { &["Dimension"] }
    fn is_undoable(&self) -> bool { true }
}

#[derive(Clone)]
struct TextCommand {
    name: String,
    description: String,
}

impl TextCommand {
    fn new(name: String, description: String) -> Self {
        Self { name, description }
    }
}

impl CommandClone for TextCommand {
    fn clone_command(&self) -> Box<dyn Command> { Box::new(self.clone()) }
}

impl Command for TextCommand {
    fn name(&self) -> &str { &self.name }
    fn description(&self) -> &str { &self.description }
    fn execute(&self, _: &mut CommandContext) -> CommandResult { CommandResult::Success }
    fn undo(&self, _: &mut CommandContext) -> CommandResult { CommandResult::Success }
    fn preview(&self, _: &CommandContext) -> Option<super::super::data_structure::Entity> { None }
    fn requires_selection(&self) -> bool { false }
    fn get_required_entity_types(&self) -> &[&'static str] { &[] }
    fn is_undoable(&self) -> bool { true }
}

#[derive(Clone)]
struct SelectionCommand {
    name: String,
    description: String,
}

impl SelectionCommand {
    fn new(name: String, description: String) -> Self {
        Self { name, description }
    }
}

impl CommandClone for SelectionCommand {
    fn clone_command(&self) -> Box<dyn Command> { Box::new(self.clone()) }
}

impl Command for SelectionCommand {
    fn name(&self) -> &str { &self.name }
    fn description(&self) -> &str { &self.description }
    fn execute(&self, _: &mut CommandContext) -> CommandResult { CommandResult::Success }
    fn undo(&self, _: &mut CommandContext) -> CommandResult { CommandResult::Success }
    fn preview(&self, _: &CommandContext) -> Option<super::super::data_structure::Entity> { None }
    fn requires_selection(&self) -> bool { false }
    fn get_required_entity_types(&self) -> &[&'static str] { &[] }
    fn is_undoable(&self) -> bool { false }
}

#[derive(Clone)]
pub struct GenericCommand {
    name: String,
    description: String,
    execute_fn: std::sync::Arc<dyn Fn(&mut CommandContext) -> CommandResult + Send + Sync>,
    undo_fn: std::sync::Arc<dyn Fn(&mut CommandContext) -> CommandResult + Send + Sync>,
    requires_selection: bool,
    required_entity_types: Vec<&'static str>,
}

impl GenericCommand {
    pub fn new(
        name: impl Into<String>,
        description: impl Into<String>,
        execute_fn: impl Fn(&mut CommandContext) -> CommandResult + 'static + Send + Sync,
        undo_fn: impl Fn(&mut CommandContext) -> CommandResult + 'static + Send + Sync,
    ) -> Self {
        Self {
            name: name.into(),
            description: description.into(),
            execute_fn: std::sync::Arc::new(execute_fn),
            undo_fn: std::sync::Arc::new(undo_fn),
            requires_selection: false,
            required_entity_types: Vec::new(),
        }
    }

    pub fn with_selection_requirement(mut self, required: bool, entity_types: Vec<&'static str>) -> Self {
        self.requires_selection = required;
        self.required_entity_types = entity_types;
        self
    }
}

impl CommandClone for GenericCommand {
    fn clone_command(&self) -> Box<dyn Command> { Box::new(self.clone()) }
}

impl Command for GenericCommand {

    fn name(&self) -> &str {
        &self.name
    }

    fn description(&self) -> &str {
        &self.description
    }

    fn execute(&self, context: &mut CommandContext) -> CommandResult {
        (self.execute_fn)(context)
    }

    fn undo(&self, context: &mut CommandContext) -> CommandResult {
        (self.undo_fn)(context)
    }

    fn preview(&self, _context: &CommandContext) -> Option<super::super::data_structure::Entity> {
        None
    }

    fn requires_selection(&self) -> bool {
        self.requires_selection
    }

    fn get_required_entity_types(&self) -> &[&'static str] {
        &self.required_entity_types
    }

    fn is_undoable(&self) -> bool {
        true
    }
}

impl fmt::Display for GenericCommand {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Command({})", self.name)
    }
}