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
/*
 * Copyright (C) 2010 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 std::collections::HashMap;

use crate::{
    common::{BitArray, Result},
    point_f, BarcodeFormat, Exceptions, RXingResult, RXingResultMetadataType,
    RXingResultMetadataValue,
};

use super::{upc_ean_reader, UPCEANReader, STAND_IN};

/**
 * @see UPCEANExtension2Support
 */
#[derive(Default)]
pub struct UPCEANExtension5Support;

impl UPCEANExtension5Support {
    const CHECK_DIGIT_ENCODINGS: [usize; 10] =
        [0x18, 0x14, 0x12, 0x11, 0x0C, 0x06, 0x03, 0x0A, 0x09, 0x05];

    pub fn decodeRow(
        &self,
        rowNumber: u32,
        row: &BitArray,
        extensionStartRange: &[usize; 2],
    ) -> Result<RXingResult> {
        let mut result = String::new();

        let end = Self::decodeMiddle(row, extensionStartRange, &mut result)?;

        let resultString = result;
        let extensionData = Self::parseExtensionString(&resultString);

        let mut extensionRXingResult = RXingResult::new(
            &resultString,
            Vec::new(),
            vec![
                point_f(
                    (extensionStartRange[0] + extensionStartRange[1]) as f32 / 2.0,
                    rowNumber as f32,
                ),
                point_f(end as f32, rowNumber as f32),
            ],
            BarcodeFormat::UPC_EAN_EXTENSION,
        );

        if let Some(ed) = extensionData {
            extensionRXingResult.putAllMetadata(ed);
        }

        Ok(extensionRXingResult)
    }

    fn decodeMiddle(
        row: &BitArray,
        startRange: &[usize; 2],
        resultString: &mut String,
    ) -> Result<u32> {
        let mut counters = [0_u32; 4];
        let end = row.get_size();
        let mut rowOffset = startRange[1];

        let mut lgPatternFound = 0;

        let mut x = 0;
        while x < 5 && rowOffset < end {
            let bestMatch = STAND_IN.decodeDigit(
                row,
                &mut counters,
                rowOffset,
                &upc_ean_reader::L_AND_G_PATTERNS,
            )?;
            resultString
                .push(char::from_u32('0' as u32 + bestMatch as u32 % 10).ok_or(Exceptions::PARSE)?);

            rowOffset += counters.iter().sum::<u32>() as usize;

            if bestMatch >= 10 {
                lgPatternFound |= 1 << (4 - x);
            }
            if x != 4 {
                // Read off separator if not last
                rowOffset = row.getNextSet(rowOffset);
                rowOffset = row.getNextUnset(rowOffset);
            }

            x += 1;
        }

        if resultString.chars().count() != 5 {
            return Err(Exceptions::NOT_FOUND);
        }

        let checkDigit = Self::determineCheckDigit(lgPatternFound)?;
        if Self::extensionChecksum(resultString).ok_or(Exceptions::ILLEGAL_ARGUMENT)?
            != checkDigit as u32
        {
            return Err(Exceptions::NOT_FOUND);
        }

        Ok(rowOffset as u32)
    }

    fn extensionChecksum(s: &str) -> Option<u32> {
        let length = s.chars().count();
        let mut sum = 0;
        let mut i = length as isize - 2;
        while i >= 0 {
            // for (int i = length - 2; i >= 0; i -= 2) {
            sum += s.chars().nth(i as usize)? as u32 - '0' as u32;

            i -= 2;
        }
        sum *= 3;

        let mut i = length as isize - 1;
        while i >= 0 {
            // for (int i = length - 1; i >= 0; i -= 2) {
            sum += s.chars().nth(i as usize)? as u32 - '0' as u32;

            i -= 2;
        }
        sum *= 3;
        Some(sum % 10)
    }

    fn determineCheckDigit(lgPatternFound: usize) -> Result<usize> {
        for d in 0..10 {
            if lgPatternFound == Self::CHECK_DIGIT_ENCODINGS[d] {
                return Ok(d);
            }
        }
        Err(Exceptions::NOT_FOUND)
    }

    /**
     * @param raw raw content of extension
     * @return formatted interpretation of raw content as a {@link Map} mapping
     *  one {@link RXingResultMetadataType} to appropriate value, or {@code null} if not known
     */
    fn parseExtensionString(
        raw: &str,
    ) -> Option<HashMap<RXingResultMetadataType, RXingResultMetadataValue>> {
        if raw.chars().count() != 5 {
            return None;
        }
        let Some(value) = Self::parseExtension5String(raw) else {
            return None;
        };

        let mut result = HashMap::new();
        result.insert(
            RXingResultMetadataType::SUGGESTED_PRICE,
            RXingResultMetadataValue::SuggestedPrice(value),
        );

        Some(result)
    }

    fn parseExtension5String(raw: &str) -> Option<String> {
        let currency = match raw.chars().next()? {
            '0' => "£",
            '5' => "$",
            '9' => {
                // Reference: http://www.jollytech.com
                match raw {
                    "90000" =>
                    // No suggested retail price
                    {
                        return None
                    }
                    "99991" =>
                    // Complementary
                    {
                        return Some("0.00".to_string())
                    }
                    "99990" => return Some("Used".to_owned()),
                    _ => {}
                }
                // Otherwise... unknown currency?
                ""
            }
            _ => "",
        };

        let rawAmount = raw[1..].parse::<i32>().ok()?;
        let unitsString = (rawAmount / 100).to_string();
        let hundredths = rawAmount % 100;
        let hundredthsString = if hundredths < 10 {
            format!("0{hundredths}")
        } else {
            hundredths.to_string()
        };

        Some(format!("{currency}{unitsString}.{hundredthsString}"))
    }
}