/*-
 * ~~~~~~licensing~~~~~~
 * atlasmap-entaxy-libs
 * ==========
 * 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~~~~~~
 */
package io.atlasmap.actions;

import org.osgi.framework.ServiceException;
import org.osgi.framework.ServiceReference;

import io.atlasmap.entaxy.OsgiHelper;
import io.atlasmap.spi.AtlasActionProcessor;
import io.atlasmap.spi.AtlasFieldAction;
import io.atlasmap.v2.entaxy.MapData;
import ru.entaxy.platform.base.support.CommonUtils;
import ru.entaxy.platform.modules.datamapper.DataMapperService;

public class EntaxyActions implements AtlasFieldAction {

    @AtlasActionProcessor
    public static Object mapData(MapData mapData, Object input) {
        if (mapData == null) {
            throw new IllegalArgumentException("MapData action must be specified");
        }
        if (input == null) {
            return null;
        }

        Object result = null;

        if (!CommonUtils.isValid(mapData.getMapToUse()))
            throw new IllegalArgumentException("MapToUse must be specified");
        ServiceReference<DataMapperService> ref = null;
        try {
            ref = OsgiHelper.getServiceRef(DataMapperService.class);
            if (ref == null)
                throw new ServiceException("DataMapperService service not available");
            DataMapperService dataMapperService = OsgiHelper.getService(ref);
            result = dataMapperService.getValue(mapData.getMapToUse(), input, !mapData.isDisableTrimWhitespace());
            if (result == null)
                throw new IllegalArgumentException();
        } catch (Exception e) {
            switch (mapData.getNoDataAction()) {
                case RETURN_NULL:
                    result = null;
                    break;
                case THROW_EXCEPTION:
                    throw e;
                case USE_ORIGIN:
                    result = input;
                    break;
                case RETURN_VALUE:
                    result = mapData.getNoDataValue();
                default:
                    break;
            }
        } finally {
            try {
                OsgiHelper.ungetService(ref);
            } catch (Exception ignore) {
                // noop
            }
        }
        if (result == null)
            return result;

        switch (mapData.getReturnType()) {
            case STRING:
                result = result.toString();
                break;
            case BOOLEAN:
                try {
                    result = Boolean.valueOf(result.toString());
                } catch (Exception ignore) {
                    result = false;
                }
                break;
            case LONG:
                try {
                    result = Long.parseLong(result.toString());
                } catch (Exception ignore) {
                    result = null;
                }
                break;

            case DOUBLE:
                try {
                    result = Double.parseDouble(result.toString());
                } catch (Exception ignore) {
                    result = null;
                }
                break;
        }

        return result;
    }

}
