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
use std::{
    alloc::Layout,
    error::Error,
    fmt::{Debug, Display},
    slice::Iter,
    sync::Arc,
};

use crate::{
    alg::{Alg, AlgNode, AlgParseError, Move},
    kpuzzle::{KPuzzleDefinition, KPuzzleOrbitName},
};

use super::{
    derived_moves_validator::DerivedMovesValidator,
    lookup_move::{lookup_move, MoveLookupResultSource},
    orientation_packer::OrientationPacker,
    packed_orbit_data::PackedOrbitData,
    InvalidKPatternDataError, InvalidKTransformationDataError, KPattern, KTransformation,
};

// TODO: allow certain values over 107?
const MAX_NUM_ORIENTATIONS_INCLUSIVE: u8 = 107;

/// An error due to the structure of a [`KPuzzleDefinition`] (such as a recursive derived move definition).
#[derive(Debug)]
pub struct InvalidDefinitionError {
    pub description: String,
}

// TODO: is Rust smart enough to optimize this using just the `From<&str>` definition?
impl From<String> for InvalidDefinitionError {
    fn from(description: String) -> Self {
        Self { description }
    }
}

impl From<&str> for InvalidDefinitionError {
    fn from(description: &str) -> Self {
        Self {
            description: description.to_owned(),
        }
    }
}

impl Display for InvalidDefinitionError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.description)
    }
}

#[derive(Debug)]
// An operation failed due to an invalid error. This usually occurs when applying an alg to a puzzle with incompatible notation.
pub struct InvalidMoveError {
    pub description: String,
}

// TODO: is Rust smart enough to optimize this using just the `From<&str>` definition?
impl From<String> for InvalidMoveError {
    fn from(description: String) -> Self {
        Self { description }
    }
}

impl From<&str> for InvalidMoveError {
    fn from(description: &str) -> Self {
        Self {
            description: description.to_owned(),
        }
    }
}

impl Display for InvalidMoveError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.description)
    }
}

/// An error type that can indicate multiple error causes, when parsing and applying an alg at the same time.
#[derive(derive_more::From, Debug, derive_more::Display)]
pub enum InvalidAlgError {
    AlgParse(AlgParseError),
    InvalidMove(InvalidMoveError),
}

impl Error for InvalidAlgError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        Some(self)
    }
}

fn identity_transformation(kpuzzle: &KPuzzle) -> KTransformation {
    let mut packed_orbit_data =
        unsafe { PackedOrbitData::new_with_uninitialized_bytes(kpuzzle.clone()) };
    for orbit_info in kpuzzle.orbit_info_iter() {
        // for orbit_definition in &kpuzzle.definition.orbits {
        let num_pieces = orbit_info.num_pieces;
        for i in 0..num_pieces {
            unsafe { packed_orbit_data.set_raw_piece_or_permutation_value(orbit_info, i, i) };
            unsafe { packed_orbit_data.set_raw_orientation_value(orbit_info, i, 0) };
        }
    }
    KTransformation { packed_orbit_data }
}

#[derive(Debug)]
pub struct KPuzzleOrbitInfo {
    pub name: KPuzzleOrbitName,
    pub pieces_or_permutations_offset: usize,
    pub orientations_offset: usize,
    pub num_pieces: u8,
    pub num_orientations: u8,
    pub orientation_packer: OrientationPacker,
}

#[derive(Debug)]
pub struct KPuzzleData {
    /// Use `.definition()` directly on `KPuzzle` instead, when possible.
    pub definition: Arc<KPuzzleDefinition>,
    /// Use `.orbit_iter()` directly on `KPuzzle` instead, when possible.
    pub ordered_orbit_info: Vec<KPuzzleOrbitInfo>,

    // Private cached values.
    pub(crate) num_bytes: usize,
    pub(crate) layout: Layout,
    // TODO: compute lazily while being thread-safe?
    // cached_identity_transformation_data: PackedOrbitData, // TODO
}

#[derive(Clone)]
pub struct KPuzzle {
    pub data: Arc<KPuzzleData>, // TODO
                                // pub data: PackedKPuzzleData,
}

/// An error type that can indicate multiple error causes, when parsing and applying an alg at the same time.
#[derive(derive_more::From, Debug, derive_more::Display)]
pub enum ConversionError {
    InvalidAlg(InvalidAlgError),
    InvalidDefinition(InvalidDefinitionError),
    InvalidKPatternData(InvalidKPatternDataError),
    InvalidKTransformationData(InvalidKTransformationDataError),
}

fn transformation_from_alg(
    kpuzzle: &KPuzzle,
    alg: &Alg,
) -> Result<KTransformation, InvalidAlgError> {
    let mut t = kpuzzle.identity_transformation();
    for node in alg.nodes.iter() {
        let node_transformation = transformation_from_alg_node(kpuzzle, node)?;
        t = t.apply_transformation(&node_transformation);
    }
    Ok(t)
}

fn transformation_from_alg_node(
    kpuzzle: &KPuzzle,
    alg_node: &AlgNode,
) -> Result<KTransformation, InvalidAlgError> {
    match alg_node {
        AlgNode::MoveNode(key_move) => kpuzzle.transformation_from_move(key_move),
        AlgNode::PauseNode(_pause) => Ok(kpuzzle.identity_transformation()),
        AlgNode::NewlineNode(_pause) => Ok(kpuzzle.identity_transformation()),
        AlgNode::LineCommentNode(_pause) => Ok(kpuzzle.identity_transformation()),
        AlgNode::GroupingNode(grouping) => {
            Ok(transformation_from_alg(kpuzzle, &grouping.alg)?.self_multiply(grouping.amount))
        }
        AlgNode::CommutatorNode(commutator) => {
            let a = transformation_from_alg(kpuzzle, &commutator.a)?;
            let b = transformation_from_alg(kpuzzle, &commutator.b)?;
            let a_prime = transformation_from_alg(kpuzzle, &commutator.a.invert())?; // TODO: invert the transformation instead of the alg!
            let b_prime = transformation_from_alg(kpuzzle, &commutator.b.invert())?; // TODO: invert the transformation instead of the alg!
            Ok(a.apply_transformation(&b)
                .apply_transformation(&a_prime)
                .apply_transformation(&b_prime))
        }
        AlgNode::ConjugateNode(conjugate) => {
            let a = transformation_from_alg(kpuzzle, &conjugate.a)?;
            let b = transformation_from_alg(kpuzzle, &conjugate.b)?;
            let a_prime = transformation_from_alg(kpuzzle, &conjugate.a.invert())?; // TODO: invert the transformation instead of the alg!
            Ok(a.apply_transformation(&b).apply_transformation(&a_prime))
        }
    }
}

impl KPuzzle {
    pub fn try_new(
        definition: impl Into<Arc<KPuzzleDefinition>>,
    ) -> Result<Self, InvalidDefinitionError> {
        let definition = definition.into();
        // let cached_identity_transformation_data = identity_transformation_data(&definition).into(); // TODO

        DerivedMovesValidator::check(&definition)?;

        let mut bytes_offset = 0;
        let mut ordered_orbit_info: Vec<KPuzzleOrbitInfo> = vec![];

        for orbit_definition in &definition.orbits {
            let num_orientations = orbit_definition.num_orientations;
            if num_orientations > MAX_NUM_ORIENTATIONS_INCLUSIVE {
                return Err(InvalidDefinitionError { description: format!("`num_orientations` for orbit {} is too large ({}). Maximum is {} for the current build." , orbit_definition.orbit_name, num_orientations, MAX_NUM_ORIENTATIONS_INCLUSIVE)});
            }
            ordered_orbit_info.push({
                KPuzzleOrbitInfo {
                    name: orbit_definition.orbit_name.clone(),
                    num_pieces: orbit_definition.num_pieces,
                    num_orientations,
                    pieces_or_permutations_offset: bytes_offset,
                    orientations_offset: bytes_offset + (orbit_definition.num_pieces as usize),
                    orientation_packer: OrientationPacker::new(orbit_definition.num_orientations),
                }
            });
            bytes_offset += (orbit_definition.num_pieces as usize) * 2;
        }

        Ok(Self {
            data: Arc::new(KPuzzleData {
                definition,
                num_bytes: bytes_offset,
                ordered_orbit_info,
                layout: Layout::array::<u8>(bytes_offset).map_err(|_| InvalidDefinitionError {
                    description: "Could not construct packed layout.".to_owned(),
                })?,
            }),
        })
    }

    pub fn try_from_json(json_bytes: &[u8]) -> Result<KPuzzle, InvalidDefinitionError> {
        // TODO: implement this directly
        let definition: KPuzzleDefinition = match serde_json::from_slice(json_bytes) {
            Ok(kpuzzle_data) => kpuzzle_data,
            Err(e) => {
                return Err(InvalidDefinitionError {
                    description: e.to_string().to_owned(),
                })
            }
        };
        KPuzzle::try_new(definition)
    }

    pub fn definition(&self) -> &KPuzzleDefinition {
        &self.data.definition
    }

    pub fn orbit_info_iter(&self) -> Iter<'_, KPuzzleOrbitInfo> {
        self.data.ordered_orbit_info.iter()
    }

    pub fn default_pattern(&self) -> KPattern {
        // TODO: check/cache at construction time.
        KPattern::try_from_data(self, &self.definition().default_pattern)
            .expect("Invalid default pattern")
    }

    // TODO: design a much much more efficient API.
    pub fn lookup_orbit(&self, orbit_name: &KPuzzleOrbitName) -> Option<&KPuzzleOrbitInfo> {
        self.orbit_info_iter()
            .find(|&orbit| &orbit.name == orbit_name)
    }

    pub fn identity_transformation(&self) -> KTransformation {
        identity_transformation(self)
    }

    // TODO: implement this as a `TryFrom`?
    pub fn transformation_from_move(
        &self, // TODO: Any issues with not using `&self`?
        key_move: &Move,
    ) -> Result<KTransformation, InvalidAlgError> {
        let move_lookup_result = match lookup_move(self.definition(), key_move) {
            Some(move_lookup_result) => move_lookup_result,
            None => {
                return Err(InvalidMoveError {
                    description: format!("Move does not exist on this puzzle: {}", key_move),
                }
                .into())
            }
        };
        let transformation = match move_lookup_result.source {
            // TODO: Avoid constructing this `KTransformation`.
            MoveLookupResultSource::DirectlyDefined(transformation_data) => {
                KTransformation::try_from_data(self, transformation_data)
                    .expect("TODO: invalid definition — this should be caught earlier")
            }
            MoveLookupResultSource::DerivedFromAlg(alg) => self.transformation_from_alg(alg)?,
        };
        Ok(transformation.self_multiply(move_lookup_result.relative_amount))
    }

    // TODO: implement this directly
    pub fn transformation_from_alg(&self, alg: &Alg) -> Result<KTransformation, InvalidAlgError> {
        transformation_from_alg(self, alg)
    }
}

impl From<&KPuzzle> for KPuzzle {
    fn from(value: &KPuzzle) -> Self {
        value.clone()
    }
}

impl TryFrom<KPuzzleDefinition> for KPuzzle {
    type Error = InvalidDefinitionError;

    fn try_from(definition: KPuzzleDefinition) -> Result<Self, Self::Error> {
        KPuzzle::try_new(definition)
    }
}

impl Debug for KPuzzle {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{{ … name: \"{}\" … }}", &self.definition().name)
    }
}