/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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
 *
 *    https://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.
 */
package org.grails.databinding.converters.web

import java.text.DecimalFormat
import java.text.NumberFormat
import java.text.ParsePosition

import groovy.transform.CompileStatic

import jakarta.servlet.http.HttpServletRequest

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.servlet.LocaleResolver

import grails.databinding.converters.ValueConverter
import org.grails.web.servlet.mvc.GrailsWebRequest

/**
 * A ValueConverter that knows how to convert a String to any numeric type and is Locale aware.  The
 * converter will use the Locale of the current request if being invoked as part of a
 * request, otherwise will use Locale.getDefault()
 *
 * @author Jeff Brown
 * @since 2.3
 *
 */
@CompileStatic
class LocaleAwareNumberConverter implements ValueConverter {

    Class<?> targetType

    @Autowired(required=false)
    LocaleResolver localeResolver

    @Override
    boolean canConvert(Object value) {
        value instanceof String
    }

    @Override
    Object convert(Object value) {
        def trimmedValue = value.toString().trim()
        def formatter = numberFormatter
        def valueToParse = replaceAsciiMinusWithLocaleMinus(formatter, trimmedValue)
        def parsePosition = new ParsePosition(0)
        def result = formatter.parse(valueToParse, parsePosition).asType(getTargetType())
        if (parsePosition.index != valueToParse.size()) {
            throw new NumberFormatException("Unable to parse number [${value}]")
        }
        result
    }

    protected String replaceAsciiMinusWithLocaleMinus(NumberFormat formatter, String value) {
        if (formatter instanceof DecimalFormat && value.startsWith('-')) {
            char minusSign = formatter.decimalFormatSymbols.minusSign
            if (minusSign != '-' as char) {
                return String.valueOf(minusSign) + value.substring(1)
            }
        }
        value
    }

    protected NumberFormat getNumberFormatter() {
        NumberFormat.getInstance(getLocale())
    }

    protected Locale getLocale() {
        def locale
        def request = GrailsWebRequest.lookup()?.currentRequest
        if (request instanceof HttpServletRequest) {
            locale = localeResolver?.resolveLocale(request)
        }
        if (locale == null) {
            locale = Locale.default
        }
        return locale
    }
}
