/*-
 * ~~~~~~licensing~~~~~~
 * data-mapper-runtime
 * ==========
 * 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 ru.entaxy.platform.modules.datamapper.camel;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;

import ru.entaxy.platform.modules.datamapper.DataMapper;
import ru.entaxy.platform.modules.datamapper.DataMapperService;

public class DataMapperCamelProcessor implements Processor {

    public enum NO_DATA_ACTION {
        USE_ORIGIN,
        RETURN_NULL,
        RETURN_VALUE,
        THROW_EXCEPTION
    }

    public static final String PROPERTY_DATA_MAPPER_INPUT = "NTX_data_mapper_input";
    public static final String PROPERTY_DATA_MAPPER_ID = "NTX_data_mapper_id";
    public static final String PROPERTY_DATA_MAPPER_MAP_NAME = "NTX_data_mapper_map_name";
    public static final String PROPERTY_DATA_MAPPER_NO_DATA_VALUE = "NTX_data_mapper_no_data_value";
    public static final String PROPERTY_DATA_MAPPER_NO_DATA_ACTION = "NTX_data_mapper_no_data_action";
    public static final String PROPERTY_DATA_MAPPER_RETURN_PROPERTY = "NTX_data_mapper_return_property";
    public static final String PROPERTY_DATA_MAPPER_TRIM_FOR_STRING = "NTX_data_mapper_trim_for_string";
    public static final String PROPERTY_DATA_MAPPER_RETURN_PROPERTY_DEFAULT = "NTX_DATA_MAPPER_RESULT";

    protected DataMapperService dataMapperService;

    @Override
    public void process(Exchange exchange) throws Exception {

        Object input = exchange.getProperties().get(PROPERTY_DATA_MAPPER_INPUT);

        Object result = null;

        if (input != null) {
            NO_DATA_ACTION action = NO_DATA_ACTION.USE_ORIGIN;
            try {
                action = NO_DATA_ACTION.valueOf(exchange.getProperties()
                        .getOrDefault(PROPERTY_DATA_MAPPER_NO_DATA_ACTION, NO_DATA_ACTION.USE_ORIGIN.name())
                        .toString()
                        .toUpperCase());
            } catch (Exception ignore) {
                // noop
            }
            String mapperId = exchange.getProperties()
                    .getOrDefault(PROPERTY_DATA_MAPPER_ID, DataMapperService.DEFAULT_MAPPER_ID)
                    .toString();
            String mapName = exchange.getProperties()
                    .getOrDefault(PROPERTY_DATA_MAPPER_MAP_NAME, DataMapper.DEFAULT_MAP)
                    .toString();
            String defaultValue = exchange.getProperties()
                    .getOrDefault(PROPERTY_DATA_MAPPER_NO_DATA_VALUE, "")
                    .toString();
            boolean isTrimForString = Boolean.parseBoolean(exchange.getProperties()
                    .getOrDefault(PROPERTY_DATA_MAPPER_TRIM_FOR_STRING, true)
                    .toString());
            try {
                result = dataMapperService.getValueUnsafe(mapperId, mapName, input, isTrimForString);
                if (result == null)
                    throw new IllegalArgumentException("Result is null");
            } catch (Exception e) {
                switch (action) {
                    case RETURN_NULL:
                        result = null;
                        break;
                    case RETURN_VALUE:
                        result = exchange.getProperty(PROPERTY_DATA_MAPPER_NO_DATA_VALUE);
                        break;
                    case THROW_EXCEPTION:
                        throw e;
                    case USE_ORIGIN:
                        result = input;
                }
            }

        }

        String returnProperty = exchange.getProperties()
                .getOrDefault(PROPERTY_DATA_MAPPER_RETURN_PROPERTY, PROPERTY_DATA_MAPPER_RETURN_PROPERTY_DEFAULT)
                .toString();
        exchange.setProperty(returnProperty, result);
    }

    public DataMapperService getDataMapperService() {
        return dataMapperService;
    }

    public void setDataMapperService(DataMapperService dataMapperService) {
        this.dataMapperService = dataMapperService;
    }

}
