/*-
 * ~~~~~~licensing~~~~~~
 * atlasmap-entaxy-services
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
/*
 * Copyright (C) 2017 Red Hat, Inc.
 *
 * 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.
 */
package io.atlasmap.converters;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.CharBuffer;

import io.atlasmap.spi.AtlasConversionConcern;
import io.atlasmap.spi.AtlasConversionInfo;
import io.atlasmap.spi.AtlasConverter;
import io.atlasmap.v2.FieldType;

public class BooleanConverter implements AtlasConverter<Boolean> {

    private static final String STRING_VALUES = "true|false";

    @AtlasConversionInfo(sourceType = FieldType.BOOLEAN, targetType = FieldType.DECIMAL)
    public BigDecimal toBigDecimal(Boolean value) {
        return value != null ? BigDecimal.valueOf(value ? 1 : 0) : null;
    }

    @AtlasConversionInfo(sourceType = FieldType.BOOLEAN, targetType = FieldType.BIG_INTEGER)
    public BigInteger toBigInteger(Boolean value) {
        return value != null ? BigInteger.valueOf(value ? 1 : 0) : null;
    }

    @AtlasConversionInfo(sourceType = FieldType.BOOLEAN, targetType = FieldType.BOOLEAN)
    public Boolean toBoolean(Boolean value, String sourceFormat, String targetFormat) {
        return value != null ? Boolean.valueOf(value) : null;
    }

    @AtlasConversionInfo(sourceType = FieldType.BOOLEAN, targetType = FieldType.BYTE)
    public Byte toByte(Boolean value) {
        return value != null ? (byte) (value ? 1 : 0) : null;
    }

    @AtlasConversionInfo(sourceType = FieldType.BOOLEAN, targetType = FieldType.CHAR)
    public Character toCharacter(Boolean value) {
        return value != null ? (char) (value ? 1 : 0) : null;
    }

    @AtlasConversionInfo(sourceType = FieldType.BOOLEAN, targetType = FieldType.DOUBLE)
    public Double toDouble(Boolean value) {
        return value != null ? value ? 1.0d : 0.0d : null;
    }

    @AtlasConversionInfo(sourceType = FieldType.BOOLEAN, targetType = FieldType.FLOAT)
    public Float toFloat(Boolean value) {
        return value != null ? (value ? 1.0f : 0.0f) : null;
    }

    @AtlasConversionInfo(sourceType = FieldType.BOOLEAN, targetType = FieldType.INTEGER)
    public Integer toInteger(Boolean value) {
        return value != null ? (value ? 1 : 0) : null;
    }

    @AtlasConversionInfo(sourceType = FieldType.BOOLEAN, targetType = FieldType.LONG)
    public Long toLong(Boolean value) {
        return value != null ? (value ? 1L : 0L) : null;
    }

    @AtlasConversionInfo(sourceType = FieldType.BOOLEAN, targetType = FieldType.NUMBER)
    public Number toNumber(Boolean value) {
        return toShort(value);
    }

    @AtlasConversionInfo(sourceType = FieldType.BOOLEAN, targetType = FieldType.SHORT)
    public Short toShort(Boolean value) {
        return value != null ? (short) (value ? 1 : 0) : null;
    }

    @AtlasConversionInfo(sourceType = FieldType.BOOLEAN, targetType = FieldType.STRING, concerns = {
            AtlasConversionConcern.CONVENTION })
    public String toString(Boolean value, String sourceFormat, String targetFormat) {
        if (value == null) {
            return null;
        }
        // TODO optimize/save defaults
        String format = targetFormat != null && !"".equals(targetFormat) ? targetFormat : STRING_VALUES;
        String[] values = format.split("\\|");
        String trueValue = "";
        String falseValue = "";
        if (values.length == 2) {
            trueValue = values[0];
            falseValue = values[1];
        } else if (values.length == 1) {
            trueValue = values[0];
        }
        return String.valueOf((value ? trueValue : falseValue));
    }

    @AtlasConversionInfo(sourceType = FieldType.BOOLEAN, targetType = FieldType.STRING)
    public CharBuffer toCharBuffer(Boolean value, String sourceFormat, String targetFormat) {
        return value != null ? CharBuffer.wrap(toString(value, sourceFormat, targetFormat)) : null;
    }

    @AtlasConversionInfo(sourceType = FieldType.BOOLEAN, targetType = FieldType.STRING)
    public CharSequence toCharSequence(Boolean value, String sourceFormat, String targetFormat) {
        return value != null ? toString(value, sourceFormat, targetFormat) : null;
    }

    @AtlasConversionInfo(sourceType = FieldType.BOOLEAN, targetType = FieldType.STRING)
    public StringBuffer toStringBuffer(Boolean value, String sourceFormat, String targetFormat) {
        return value != null ? new StringBuffer(toString(value, sourceFormat, targetFormat)) : null;
    }

    @AtlasConversionInfo(sourceType = FieldType.BOOLEAN, targetType = FieldType.STRING)
    public StringBuilder toStringBuilder(Boolean value, String sourceFormat, String targetFormat) {
        return value != null ? new StringBuilder(toString(value, sourceFormat, targetFormat)) : null;
    }

}
