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
/*
Copyright (c) 2023 Michał Wilczek, Michał Margos

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the “Software”), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

use std::{fs::File, io::Write, path::Path, rc::Rc, sync::Arc};

use crate::{
    generator::{
        expression::{expr::AnglePoint, Expression, PointExpr, ScalarExpr},
        Complex,
    },
    projector::{
        Output, Rendered, RenderedAngle, RenderedCircle, RenderedLine, RenderedPoint, RenderedRay,
        RenderedSegment,
    },
    script::{
        figure::Mode::{self, Bolded, Dashed, Default, Dotted},
        HashableArc,
    },
};

/// Function getting the point's name (if it exists, otherwise it returns the point's coordinates).
fn get_point_name(expr: &Arc<Expression<PointExpr>>, output: &Output, point: Complex) -> String {
    match output.map.get(&HashableArc::new(Arc::clone(expr))) {
        Some(p) => p.label.to_string(),
        None => {
            format!("({}, {})", point.real, point.imaginary)
        }
    }
}

/// Function that assigns drawing modes to the rendered variants.
fn assign_mode(rendered: &Rendered, mode: Mode) -> String {
    match rendered {
        Rendered::Point(_) => unreachable!(),
        _ => match mode {
            Dotted => "dotted".to_string(),
            Dashed => "dashed".to_string(),
            Bolded => "bolded".to_string(),
            Default => "default".to_string(),
        },
    }
}

/// Function that handles the points.
fn points(mut file: &File, point: &Rc<RenderedPoint>) {
    file.write_all((format!("\npoint: label of the point - \"{}\", x and y coordinates of the point - ({:.3}, {:.3})", 
        point.label,
        point.position.real,
        point.position.imaginary))
            .as_bytes())
            .unwrap();
}

/// Function that handles the lines.
fn lines(mut file: &File, line: &RenderedLine, rendered: &Rendered) {
    let p1 = line.points.0;
    let p2 = line.points.1;
    file.write_all(
        format!(
            "\nline: label - \"{}\", 
            x and y coordinates of the two points - ({:.3}, {:.3}) and ({:.3}, {:.3}), mode: {}",
            line.label,
            p1.real,
            p1.imaginary,
            p2.real,
            p2.imaginary,
            assign_mode(rendered, line.mode)
        )
        .as_bytes(),
    )
    .unwrap();
}

/// Function that handles the angles.
fn angles(mut file: &File, angle: &RenderedAngle, output: &Output, rendered: &Rendered) {
    let p_1 = angle.points.0;
    let p_origin = angle.points.1;
    let p_2 = angle.points.2;
    let no_arcs = String::from("l"); // Requires a change later! It has to be based on info from the script
    match &angle.expr.kind {
        ScalarExpr::AnglePoint(AnglePoint { arm1, origin, arm2 }) => {
            file.write_all(format!("\nangle defined with 3 points: points with x and y coordinates: point1 - {}, origin - {}, point2 - {}. Number of arcs: {no_arcs}. Mode: {}",
                        get_point_name(arm1, output, p_1), get_point_name(origin, output, p_origin), get_point_name(arm2, output, p_2), assign_mode(rendered, angle.mode)).as_bytes()).unwrap();
        }
        ScalarExpr::AngleLine(_) => {
            file.write_all(format!("\nangle defined with 2 lines: coordinates of the points defining the angle: point1 - ({}, {}), origin - ({}, {}), point2 - ({}, {}), mode: {}", 
                            p_1.real, p_1. imaginary, p_origin.real, p_origin.imaginary, p_2.real, p_2.imaginary, assign_mode(rendered, angle.mode)
                        ).as_bytes()).unwrap();
        }
        _ => unreachable!(),
    }
}

/// Function that handles the segments.
fn segments(mut file: &File, segment: &RenderedSegment, rendered: &Rendered) {
    file.write_all(
        format!(
            "\nsegment defined by two points: point1 - ({:.3}, {:.3}), point2 - ({:.3}, {:.3}), mode: {}",
            segment.points.0.real,
            segment.points.1.real,
            segment.points.0.imaginary,
            segment.points.1.imaginary,
            assign_mode(rendered, segment.mode)
        )
        .as_bytes(),
    )
    .unwrap();
}

/// Function that handles the rays.
fn rays(mut file: &File, ray: &RenderedRay, rendered: &Rendered) {
    file.write_all(format!("\nray defined with two points: first point - ({:.3}, {:.3}) second point - ({:.3}. {:.3}). Mode: {}", 
        ray.points.0.real,
        ray.points.1.imaginary,
        ray.draw_point.real,
        ray.draw_point.imaginary,
        assign_mode(rendered, ray.mode)).
            as_bytes())
            .unwrap();
}

/// Function that handles the circles.
fn circles(mut file: &File, circle: &RenderedCircle, rendered: &Rendered) {
    file.write_all(
        format!(
            "\ncircle: center - ({:.3}, {:.3}) radius - {:.3}, mode: {}",
            circle.center.real,
            circle.center.imaginary,
            circle.radius,
            assign_mode(rendered, circle.mode)
        )
        .as_bytes(),
    )
    .unwrap();
}

/// Outputs a file which can be read
///
/// # Panics
/// Panics whenever there is a filesystem problem
pub fn draw(target: &Path, canvas_size: (usize, usize), output: &Output) {
    let mut file = File::create(target).unwrap();

    file.write_all(format!("canvas size: {} by {}", canvas_size.0, canvas_size.1).as_bytes())
        .unwrap();

    for item in &output.vec_rendered {
        match item {
            Rendered::Point(point) => {
                points(&file, point);
            }
            Rendered::Line(line) => {
                lines(&file, line, item);
            }
            Rendered::Angle(angle) => {
                angles(&file, angle, output, item);
            }
            Rendered::Segment(segment) => {
                segments(&file, segment, item);
            }
            Rendered::Ray(ray) => {
                rays(&file, ray, item);
            }
            Rendered::Circle(circle) => {
                circles(&file, circle, item);
            }
        }
    }
}