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
/*
 * Copyright 2013 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use crate::{
    common::{
        reedsolomon::{
            get_predefined_genericgf, GenericGFRef, PredefinedGenericGF, ReedSolomonEncoder,
        },
        BitArray, BitMatrix, CharacterSet, Result,
    },
    exceptions::Exceptions,
};

use super::{AztecCode, HighLevelEncoder};

/**
 * Generates Aztec 2D barcodes.
 *
 * @author Rustam Abdullaev
 */

pub const DEFAULT_EC_PERCENT: u32 = 33; // default minimal percentage of error check words
pub const DEFAULT_AZTEC_LAYERS: i32 = 0;
pub const MAX_NB_BITS: u32 = 32;
pub const MAX_NB_BITS_COMPACT: u32 = 4;

pub const WORD_SIZE: [u32; 33] = [
    4, 6, 6, 8, 8, 8, 8, 8, 8, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 12, 12, 12,
    12, 12, 12, 12, 12, 12, 12,
];

/**
 * Encodes the given string content as an Aztec symbol (without ECI code)
 *
 * @param data input data string; must be encodable as ISO/IEC 8859-1 (Latin-1)
 * @return Aztec symbol matrix with metadata
 */
pub fn encode_simple(data: &str) -> Result<AztecCode> {
    let Ok(bytes) =CharacterSet::ISO8859_1.encode_replace(data) else {
        return Err(Exceptions::illegal_argument_with(format!("'{data}' cannot be encoded as ISO_8859_1")));
    };
    encode_bytes_simple(&bytes)
}

/**
 * Encodes the given string content as an Aztec symbol (without ECI code)
 *
 * @param data input data string; must be encodable as ISO/IEC 8859-1 (Latin-1)
 * @param minECCPercent minimal percentage of error check words (According to ISO/IEC 24778:2008,
 *                      a minimum of 23% + 3 words is recommended)
 * @param userSpecifiedLayers if non-zero, a user-specified value for the number of layers
 * @return Aztec symbol matrix with metadata
 */
pub fn encode(data: &str, minECCPercent: u32, userSpecifiedLayers: i32) -> Result<AztecCode> {
    if let Ok(bytes) = CharacterSet::ISO8859_1.encode(data) {
        encode_bytes(&bytes, minECCPercent, userSpecifiedLayers)
    } else {
        Err(Exceptions::illegal_argument_with(format!(
            "'{data}' cannot be encoded as ISO_8859_1"
        )))
    }
}

/**
 * Encodes the given string content as an Aztec symbol
 *
 * @param data input data string
 * @param minECCPercent minimal percentage of error check words (According to ISO/IEC 24778:2008,
 *                      a minimum of 23% + 3 words is recommended)
 * @param userSpecifiedLayers if non-zero, a user-specified value for the number of layers
 * @param charset character set in which to encode string using ECI; if null, no ECI code
 *                will be inserted, and the string must be encodable as ISO/IEC 8859-1
 *                (Latin-1), the default encoding of the symbol.
 * @return Aztec symbol matrix with metadata
 */
pub fn encode_with_charset(
    data: &str,
    minECCPercent: u32,
    userSpecifiedLayers: i32,
    charset: CharacterSet,
) -> Result<AztecCode> {
    if let Ok(bytes) = charset.encode(data) {
        encode_bytes_with_charset(&bytes, minECCPercent, userSpecifiedLayers, charset)
    } else {
        Err(Exceptions::illegal_argument_with(format!(
            "'{data}' cannot be encoded as ISO_8859_1"
        )))
    }
}

/**
 * Encodes the given binary content as an Aztec symbol (without ECI code)
 *
 * @param data input data string
 * @return Aztec symbol matrix with metadata
 */
pub fn encode_bytes_simple(data: &[u8]) -> Result<AztecCode> {
    encode_bytes(data, DEFAULT_EC_PERCENT, DEFAULT_AZTEC_LAYERS)
}

/**
 * Encodes the given binary content as an Aztec symbol (without ECI code)
 *
 * @param data input data string
 * @param minECCPercent minimal percentage of error check words (According to ISO/IEC 24778:2008,
 *                      a minimum of 23% + 3 words is recommended)
 * @param userSpecifiedLayers if non-zero, a user-specified value for the number of layers
 * @return Aztec symbol matrix with metadata
 */
pub fn encode_bytes(
    data: &[u8],
    minECCPercent: u32,
    userSpecifiedLayers: i32,
) -> Result<AztecCode> {
    encode_bytes_with_charset(
        data,
        minECCPercent,
        userSpecifiedLayers,
        CharacterSet::ISO8859_1,
    )
}

/**
 * Encodes the given binary content as an Aztec symbol
 *
 * @param data input data string
 * @param minECCPercent minimal percentage of error check words (According to ISO/IEC 24778:2008,
 *                      a minimum of 23% + 3 words is recommended)
 * @param userSpecifiedLayers if non-zero, a user-specified value for the number of layers
 * @param charset character set to mark using ECI; if null, no ECI code will be inserted, and the
 *                default encoding of ISO/IEC 8859-1 will be assuming by readers.
 * @return Aztec symbol matrix with metadata
 */
pub fn encode_bytes_with_charset(
    data: &[u8],
    min_eccpercent: u32,
    user_specified_layers: i32,
    charset: CharacterSet,
) -> Result<AztecCode> {
    // High-level encode
    let bits = HighLevelEncoder::with_charset(data.into(), charset).encode()?;

    // stuff bits and choose symbol size
    let ecc_bits = bits.get_size() as u32 * min_eccpercent / 100 + 11;
    let total_size_bits = bits.get_size() as u32 + ecc_bits;
    let mut compact;
    let mut layers: u32;
    let mut total_bits_in_layer_var;
    let mut word_size;
    let mut stuffed_bits;
    if user_specified_layers != DEFAULT_AZTEC_LAYERS {
        compact = user_specified_layers < 0;
        layers = i32::abs(user_specified_layers) as u32;
        if layers
            > (if compact {
                MAX_NB_BITS_COMPACT
            } else {
                MAX_NB_BITS
            })
        {
            return Err(Exceptions::illegal_argument_with(format!(
                "Illegal value {user_specified_layers} for layers"
            )));
        }
        total_bits_in_layer_var = total_bits_in_layer(layers, compact);
        word_size = WORD_SIZE[layers as usize];
        let usable_bits_in_layers = total_bits_in_layer_var - (total_bits_in_layer_var % word_size);
        stuffed_bits = stuffBits(&bits, word_size as usize)?;
        if stuffed_bits.get_size() as u32 + ecc_bits > usable_bits_in_layers {
            return Err(Exceptions::illegal_argument_with(
                "Data to large for user specified layer",
            ));
        }
        if compact && stuffed_bits.get_size() as u32 > word_size * 64 {
            // Compact format only allows 64 data words, though C4 can hold more words than that
            return Err(Exceptions::illegal_argument_with(
                "Data to large for user specified layer",
            ));
        }
    } else {
        word_size = 0;
        stuffed_bits = BitArray::new();
        // We look at the possible table sizes in the order Compact1, Compact2, Compact3,
        // Compact4, Normal4,...  Normal(i) for i < 4 isn't typically used since Compact(i+1)
        // is the same size, but has more data.
        let mut i = 0;
        loop {
            // for (int i = 0; ; i++) {
            if i > MAX_NB_BITS {
                return Err(Exceptions::illegal_argument_with(
                    "Data too large for an Aztec code",
                ));
            }
            compact = i <= 3;
            layers = if compact { i + 1 } else { i };
            total_bits_in_layer_var = total_bits_in_layer(layers, compact);
            if total_size_bits > total_bits_in_layer_var {
                i += 1;
                continue;
            }
            // [Re]stuff the bits if this is the first opportunity, or if the
            // wordSize has changed
            if stuffed_bits.get_size() == 0 || word_size != WORD_SIZE[layers as usize] {
                word_size = WORD_SIZE[layers as usize];
                stuffed_bits = stuffBits(&bits, word_size as usize)?;
            }
            let usable_bits_in_layers =
                total_bits_in_layer_var - (total_bits_in_layer_var % word_size);
            if compact && stuffed_bits.get_size() as u32 > word_size * 64 {
                // Compact format only allows 64 data words, though C4 can hold more words than that
                i += 1;
                continue;
            }
            if stuffed_bits.get_size() as u32 + ecc_bits <= usable_bits_in_layers {
                break;
            }
            i += 1;
        }
    }
    let message_bits = generateCheckWords(
        &stuffed_bits,
        total_bits_in_layer_var as usize,
        word_size as usize,
    )?;

    // generate mode message
    let messageSizeInWords = stuffed_bits.get_size() as u32 / word_size;
    let modeMessage = generateModeMessage(compact, layers, messageSizeInWords)?;

    // allocate symbol
    let baseMatrixSize = (if compact { 11 } else { 14 }) + layers * 4; // not including alignment lines
    let mut alignmentMap = vec![0u32; baseMatrixSize as usize];
    let matrixSize;
    if compact {
        // no alignment marks in compact mode, alignmentMap is a no-op
        matrixSize = baseMatrixSize;
        // for i in 0..alignmentMap.len() {
        alignmentMap[..].copy_from_slice(&(0..baseMatrixSize).collect::<Vec<u32>>()[..]);
    } else {
        matrixSize = baseMatrixSize + 1 + 2 * ((baseMatrixSize / 2 - 1) / 15);
        let origCenter = (baseMatrixSize / 2) as usize;
        let center = matrixSize / 2;
        for i in 0..origCenter {
            // for (int i = 0; i < origCenter; i++) {
            let newOffset = (i + i / 15) as u32;
            alignmentMap[origCenter - i - 1] = center - newOffset - 1;
            alignmentMap[origCenter + i] = center + newOffset + 1;
        }
    }
    let mut matrix = BitMatrix::with_single_dimension(matrixSize)?;

    // dbg!(matrix.to_string());

    // draw data bits
    let mut rowOffset = 0;
    for i in 0..layers as usize {
        // for (int i = 0, rowOffset = 0; i < layers; i++) {
        let rowSize = (layers as usize - i) * 4 + (if compact { 9 } else { 12 });
        for j in 0..rowSize {
            // for (int j = 0; j < rowSize; j++) {
            let columnOffset = j * 2;
            for k in 0..2 {
                // for (int k = 0; k < 2; k++) {
                if message_bits.get(rowOffset + columnOffset + k) {
                    matrix.set(alignmentMap[i * 2 + k], alignmentMap[i * 2 + j]);
                }
                if message_bits.get(rowOffset + rowSize * 2 + columnOffset + k) {
                    matrix.set(
                        alignmentMap[i * 2 + j],
                        alignmentMap[baseMatrixSize as usize - 1 - i * 2 - k],
                    );
                }
                if message_bits.get(rowOffset + rowSize * 4 + columnOffset + k) {
                    matrix.set(
                        alignmentMap[baseMatrixSize as usize - 1 - i * 2 - k],
                        alignmentMap[baseMatrixSize as usize - 1 - i * 2 - j],
                    );
                }
                if message_bits.get(rowOffset + rowSize * 6 + columnOffset + k) {
                    matrix.set(
                        alignmentMap[baseMatrixSize as usize - 1 - i * 2 - j],
                        alignmentMap[i * 2 + k],
                    );
                }
            }
        }
        rowOffset += rowSize * 8;
    }

    // draw mode message
    drawModeMessage(&mut matrix, compact, matrixSize, modeMessage);

    // draw alignment marks
    if compact {
        drawBullsEye(&mut matrix, matrixSize / 2, 5);
    } else {
        drawBullsEye(&mut matrix, matrixSize / 2, 7);
        let mut i = 0;
        let mut j = 0;
        while i < baseMatrixSize / 2 - 1 {
            let mut k = (matrixSize / 2) & 1;
            while k < matrixSize {
                // for (int k = (matrixSize / 2) & 1; k < matrixSize; k += 2) {
                matrix.set(matrixSize / 2 - j, k);
                matrix.set(matrixSize / 2 + j, k);
                matrix.set(k, matrixSize / 2 - j);
                matrix.set(k, matrixSize / 2 + j);

                k += 2;
            }

            i += 15;
            j += 16;
        }
        // for (int i = 0, j = 0; i < baseMatrixSize / 2 - 1; i += 15, j += 16) {
        //   for (int k = (matrixSize / 2) & 1; k < matrixSize; k += 2) {
        //     matrix.set(matrixSize / 2 - j, k);
        //     matrix.set(matrixSize / 2 + j, k);
        //     matrix.set(k, matrixSize / 2 - j);
        //     matrix.set(k, matrixSize / 2 + j);
        //   }
        // }
    }

    // dbg!(matrix.to_string());

    let aztec = AztecCode::new(compact, matrixSize, layers, messageSizeInWords, matrix);
    // aztec.setCompact(compact);
    // aztec.setSize(matrixSize);
    // aztec.setLayers(layers);
    // aztec.setCodeWords(messageSizeInWords);
    // aztec.setMatrix(matrix);
    Ok(aztec)
}

fn drawBullsEye(matrix: &mut BitMatrix, center: u32, size: u32) {
    let mut i = 0;
    while i < size {
        // for (int i = 0; i < size; i += 2) {
        for j in (center - i)..=(center + i) {
            // for (int j = center - i; j <= center + i; j++) {
            matrix.set(j, center - i);
            matrix.set(j, center + i);
            matrix.set(center - i, j);
            matrix.set(center + i, j);
        }
        i += 2;
    }
    matrix.set(center - size, center - size);
    matrix.set(center - size + 1, center - size);
    matrix.set(center - size, center - size + 1);
    matrix.set(center + size, center - size);
    matrix.set(center + size, center - size + 1);
    matrix.set(center + size, center + size - 1);
}

pub fn generateModeMessage(
    compact: bool,
    layers: u32,
    messageSizeInWords: u32,
) -> Result<BitArray> {
    let mut mode_message = BitArray::new();
    if compact {
        mode_message.appendBits(layers - 1, 2)?;
        mode_message.appendBits(messageSizeInWords - 1, 6)?;
        mode_message = generateCheckWords(&mode_message, 28, 4)?;
    } else {
        mode_message.appendBits(layers - 1, 5)?;
        mode_message.appendBits(messageSizeInWords - 1, 11)?;
        mode_message = generateCheckWords(&mode_message, 40, 4)?;
    }
    Ok(mode_message)
}

fn drawModeMessage(matrix: &mut BitMatrix, compact: bool, matrixSize: u32, modeMessage: BitArray) {
    let center = matrixSize / 2;
    if compact {
        for i in 0..7_usize {
            // for (int i = 0; i < 7; i++) {
            let offset = (center as usize - 3 + i) as u32;
            if modeMessage.get(i) {
                matrix.set(offset, center - 5);
            }
            if modeMessage.get(i + 7) {
                matrix.set(center + 5, offset);
            }
            if modeMessage.get(20 - i) {
                matrix.set(offset, center + 5);
            }
            if modeMessage.get(27 - i) {
                matrix.set(center - 5, offset);
            }
        }
    } else {
        for i in 0..10_usize {
            // for (int i = 0; i < 10; i++) {
            let offset = (center as usize - 5 + i + i / 5) as u32;
            if modeMessage.get(i) {
                matrix.set(offset, center - 7);
            }
            if modeMessage.get(i + 10) {
                matrix.set(center + 7, offset);
            }
            if modeMessage.get(29 - i) {
                matrix.set(offset, center + 7);
            }
            if modeMessage.get(39 - i) {
                matrix.set(center - 7, offset);
            }
        }
    }
}

fn generateCheckWords(bitArray: &BitArray, totalBits: usize, wordSize: usize) -> Result<BitArray> {
    // bitArray is guaranteed to be a multiple of the wordSize, so no padding needed
    let message_size_in_words = bitArray.get_size() / wordSize;
    let mut rs = ReedSolomonEncoder::new(getGF(wordSize)?)?;
    let total_words = totalBits / wordSize;
    let mut message_words = bitsToWords(bitArray, wordSize, total_words);
    rs.encode(&mut message_words, total_words - message_size_in_words)?;
    let start_pad = totalBits % wordSize;
    let mut message_bits = BitArray::new();
    message_bits.appendBits(0, start_pad)?;
    for message_word in message_words {
        // for (int messageWord : messageWords) {
        message_bits.appendBits(message_word as u32, wordSize)?
    }
    // dbg!(message_bits.to_string());
    Ok(message_bits)
}

fn bitsToWords(stuffedBits: &BitArray, wordSize: usize, totalWords: usize) -> Vec<i32> {
    let mut message = vec![0; totalWords];
    let mut i = 0;
    let n = stuffedBits.get_size() / wordSize;
    while i < n {
        // for (i = 0, n = stuffedBits.getSize() / wordSize; i < n; i++) {
        let mut value = 0;
        for j in 0..wordSize {
            //   for (int j = 0; j < wordSize; j++) {
            value |= if stuffedBits.get(i * wordSize + j) {
                1 << (wordSize - j - 1)
            } else {
                0
            };
        }
        message[i] = value;

        i += 1;
    }
    message
}

fn getGF(wordSize: usize) -> Result<GenericGFRef> {
    match wordSize {
        4 => Ok(get_predefined_genericgf(PredefinedGenericGF::AztecParam)),
        6 => Ok(get_predefined_genericgf(PredefinedGenericGF::AztecData6)),
        8 => Ok(get_predefined_genericgf(PredefinedGenericGF::AztecData8)),
        10 => Ok(get_predefined_genericgf(PredefinedGenericGF::AztecData10)),
        12 => Ok(get_predefined_genericgf(PredefinedGenericGF::AztecData12)),
        _ => Err(Exceptions::illegal_argument_with(format!(
            "Unsupported word size {wordSize}"
        ))),
    }
}

pub fn stuffBits(bits: &BitArray, word_size: usize) -> Result<BitArray> {
    let mut out = BitArray::new();

    let n = bits.get_size() as isize;
    let mask = (1 << word_size) - 2;
    let mut i: isize = 0;
    while i < n {
        // for (int i = 0; i < n; i += wordSize) {
        let mut word = 0;
        for j in 0..word_size as isize {
            // for (int j = 0; j < wordSize; j++) {
            if i + j >= n || bits.get((i + j) as usize) {
                word |= 1 << (word_size as isize - 1 - j);
            }
        }
        if (word & mask) == mask {
            out.appendBits(word & mask, word_size)?;
            i -= 1;
        } else if (word & mask) == 0 {
            out.appendBits(word | 1, word_size)?;
            i -= 1;
        } else {
            out.appendBits(word, word_size)?;
        }

        i += word_size as isize;
    }
    Ok(out)
}

fn total_bits_in_layer(layers: u32, compact: bool) -> u32 {
    ((if compact { 88 } else { 112 }) + 16 * layers) * layers
}