/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF 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.apache.flink.connector.base.table.util;

import org.apache.flink.annotation.PublicEvolving;
import org.apache.flink.connector.base.table.options.ConfigurationValidator;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Properties;

/** Class containing validation utils needed by {@link ConfigurationValidator}. */
@PublicEvolving
public class ConfigurationValidatorUtil {
    public static void validateOptionalBooleanProperty(
            Properties config, String key, String message) {
        if (config.containsKey(key)) {
            if (!config.getProperty(key).equals("true")
                    && !config.getProperty(key).equals("false")) {
                throw new IllegalArgumentException(message);
            }
        }
    }

    public static void validateOptionalPositiveIntProperty(
            Properties config, String key, String message) {
        if (config.containsKey(key)) {
            try {
                int value = Integer.parseInt(config.getProperty(key));
                if (value < 0) {
                    throw new NumberFormatException();
                }
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException(message);
            }
        }
    }

    public static void validateOptionalPositiveLongProperty(
            Properties config, String key, String message) {
        if (config.containsKey(key)) {
            try {
                long value = Long.parseLong(config.getProperty(key));
                if (value < 0) {
                    throw new NumberFormatException();
                }
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException(message);
            }
        }
    }

    public static void validateOptionalPositiveDoubleProperty(
            Properties config, String key, String message) {
        if (config.containsKey(key)) {
            try {
                double value = Double.parseDouble(config.getProperty(key));
                if (value < 0) {
                    throw new NumberFormatException();
                }
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException(message);
            }
        }
    }

    public static void validateOptionalDateProperty(
            Properties config, String timestampKey, String format, String message) {
        if (config.containsKey(timestampKey)) {
            try {
                SimpleDateFormat customDateFormat = new SimpleDateFormat(format);
                customDateFormat.parse(config.getProperty(timestampKey));
            } catch (IllegalArgumentException | NullPointerException exception) {
                throw new IllegalArgumentException(message);
            } catch (ParseException exception) {
                try {
                    double value = Double.parseDouble(config.getProperty(timestampKey));
                    if (value < 0) {
                        throw new IllegalArgumentException(message);
                    }
                } catch (NumberFormatException numberFormatException) {
                    throw new IllegalArgumentException(message);
                }
            }
        }
    }
}
