/*
 * Copyright (c)  2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. 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
 *
 *     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 org.wso2.carbon.event.simulator.core.internal.util;

import org.json.JSONObject;
import org.wso2.carbon.event.simulator.core.exception.InvalidConfigException;
import org.wso2.carbon.event.simulator.core.internal.generator.random.RandomAttributeGenerator;
import org.wso2.carbon.event.simulator.core.internal.generator.random.util.CustomBasedAttrGenerator;
import org.wso2.carbon.event.simulator.core.internal.generator.random.util.PrimitiveBasedAttrGenerator;
import org.wso2.carbon.event.simulator.core.internal.generator.random.util.PropertyBasedAttrGenerator;
import org.wso2.carbon.event.simulator.core.internal.generator.random.util.RegexBasedAttrGenerator;
import org.wso2.siddhi.query.api.definition.Attribute;

import static org.wso2.carbon.event.simulator.core.internal.util.CommonOperations.checkAvailability;

/**
 * Factory class used to create random attribute generators
 */
public class RandomAttrGeneratorFactoryImpl implements RandomAttrGeneratorFactory {

    /**
     * createRandomAttrGenerator() creates randomAttributeGenerators
     *
     * @param attributeConfig attribute configuration of random attribute generator
     * @param attrType        attribute type to be generated by attribute generator
     */
    @Override
    public RandomAttributeGenerator createRandomAttrGenerator(JSONObject attributeConfig, Attribute.Type attrType)
            throws InvalidConfigException {
        if (checkAvailability(attributeConfig, EventSimulatorConstants.RANDOM_DATA_GENERATOR_TYPE)) {
            RandomAttributeGenerator.RandomDataGeneratorType type;
            try {
                type = RandomAttributeGenerator.RandomDataGeneratorType.valueOf(attributeConfig
                        .getString(EventSimulatorConstants.RANDOM_DATA_GENERATOR_TYPE));
            } catch (IllegalArgumentException e) {
                throw new InvalidConfigException("Invalid random attribute generation type. Generator type must " +
                        "be either '" + RandomAttributeGenerator.RandomDataGeneratorType.CUSTOM_DATA_BASED + "' or '"
                        + RandomAttributeGenerator.RandomDataGeneratorType.PRIMITIVE_BASED + "' or '" +
                        RandomAttributeGenerator.RandomDataGeneratorType.PROPERTY_BASED + "' or '" +
                        RandomAttributeGenerator.RandomDataGeneratorType.REGEX_BASED + "'. Invalid attribute " +
                        "configuration : " + attributeConfig.toString());
            }
            RandomAttributeGenerator randomAttributeGenerator = null;
            switch (type) {
                case CUSTOM_DATA_BASED:
                    randomAttributeGenerator = new CustomBasedAttrGenerator();
                    break;
                case PRIMITIVE_BASED:
                    randomAttributeGenerator = new PrimitiveBasedAttrGenerator();
                    break;
                case PROPERTY_BASED:
                    randomAttributeGenerator = new PropertyBasedAttrGenerator();
                    break;
                case REGEX_BASED:
                    randomAttributeGenerator = new RegexBasedAttrGenerator();
                    break;
            }
            randomAttributeGenerator.createRandomAttributeDTO(attrType, attributeConfig);
            return randomAttributeGenerator;
        } else {
            throw new InvalidConfigException("Random attribute generator type is required for random " +
                    "simulation. Generation type must be either '" +
                    RandomAttributeGenerator.RandomDataGeneratorType.CUSTOM_DATA_BASED + "' or '" +
                    RandomAttributeGenerator.RandomDataGeneratorType.PRIMITIVE_BASED + "' or '" +
                    RandomAttributeGenerator.RandomDataGeneratorType.PROPERTY_BASED + "' or '" +
                    RandomAttributeGenerator.RandomDataGeneratorType.REGEX_BASED + "'. Invalid attribute" +
                    " configuration : " + attributeConfig.toString());
        }
    }

    /**
     * validateRandomAttrGenerator() calls the validation method of the respective randomAttributeGenerators
     *
     * @param attributeConfig attribute configuration of random attribute generator
     * @param attrType        attribute type to be generated by attribute generator
     */
    @Override
    public void validateRandomAttrGenerator(JSONObject attributeConfig, Attribute.Type attrType)
            throws InvalidConfigException {
        if (checkAvailability(attributeConfig, EventSimulatorConstants.RANDOM_DATA_GENERATOR_TYPE)) {
            RandomAttributeGenerator.RandomDataGeneratorType type;
            try {
                type = RandomAttributeGenerator.RandomDataGeneratorType.valueOf(attributeConfig
                        .getString(EventSimulatorConstants.RANDOM_DATA_GENERATOR_TYPE));
            } catch (IllegalArgumentException e) {
                throw new InvalidConfigException("Invalid random attribute generation type. Generator type must " +
                        "be either '" + RandomAttributeGenerator.RandomDataGeneratorType.CUSTOM_DATA_BASED + "' or '"
                        + RandomAttributeGenerator.RandomDataGeneratorType.PRIMITIVE_BASED + "' or '" +
                        RandomAttributeGenerator.RandomDataGeneratorType.PROPERTY_BASED + "' or '" +
                        RandomAttributeGenerator.RandomDataGeneratorType.REGEX_BASED + "'. Invalid attribute " +
                        "configuration : " + attributeConfig.toString());
            }
            switch (type) {
                case CUSTOM_DATA_BASED:
                    new CustomBasedAttrGenerator().validateAttributeConfiguration(attrType, attributeConfig);
                    break;
                case PRIMITIVE_BASED:
                    new PrimitiveBasedAttrGenerator().validateAttributeConfiguration(attrType, attributeConfig);
                    break;
                case PROPERTY_BASED:
                    new PropertyBasedAttrGenerator().validateAttributeConfiguration(attrType, attributeConfig);
                    break;
                case REGEX_BASED:
                    new RegexBasedAttrGenerator().validateAttributeConfiguration(attrType, attributeConfig);
                    break;
            }
        } else {
            throw new InvalidConfigException("Random attribute generator type is required for random " +
                    "simulation. Generation type must be either '" +
                    RandomAttributeGenerator.RandomDataGeneratorType.CUSTOM_DATA_BASED + "' or '" +
                    RandomAttributeGenerator.RandomDataGeneratorType.PRIMITIVE_BASED + "' or '" +
                    RandomAttributeGenerator.RandomDataGeneratorType.PROPERTY_BASED + "' or '" +
                    RandomAttributeGenerator.RandomDataGeneratorType.REGEX_BASED + "'. Invalid attribute" +
                    " configuration : " + attributeConfig.toString());
        }
    }
}
