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();
                    randomAttributeGenerator.createRandomAttributeDTO(attributeConfig);
                    break;
                case PRIMITIVE_BASED:
                    randomAttributeGenerator = new PrimitiveBasedAttrGenerator(attrType);
                    randomAttributeGenerator.createRandomAttributeDTO(attributeConfig);
                    break;
                case PROPERTY_BASED:
                    randomAttributeGenerator = new PropertyBasedAttrGenerator();
                    randomAttributeGenerator.createRandomAttributeDTO(attributeConfig);
                    break;
                case REGEX_BASED:
                    randomAttributeGenerator = new RegexBasedAttrGenerator();
                    randomAttributeGenerator.createRandomAttributeDTO(attributeConfig);
                    break;
            }
            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(attributeConfig);
                    break;
                case PRIMITIVE_BASED:
                    new PrimitiveBasedAttrGenerator(attrType).validateAttributeConfiguration(attributeConfig);
                    break;
                case PROPERTY_BASED:
                    new PropertyBasedAttrGenerator().validateAttributeConfiguration(attributeConfig);
                    break;
                case REGEX_BASED:
                    new RegexBasedAttrGenerator().validateAttributeConfiguration(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());
        }
    }
}
