/*******************************************************************************
 * Copyright 2017-2025 TAXTELECOM, LLC
 *
 * Licensed 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 ru.taximaxim.codekeeper.ui.generators;

import java.text.MessageFormat;
import java.time.DateTimeException;
import java.time.Duration;
import java.time.LocalTime;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.Random;

import ru.taximaxim.codekeeper.ui.localizations.Messages;

/**
 * An implementation of a PostgreSql data generator for Time type.
 * <br><br>
 * Here step is stored as {@link LocalTime#toSecondOfDay()()} s of increment.
 *
 * @since 4.2.2
 * @author galiev_mr
 */
public final class TimeData extends DbData<LocalTime> {

    public TimeData() {
        super(DataType.TIME,
                LocalTime.MIN,
                LocalTime.of(23, 59, 59),
                LocalTime.ofSecondOfDay(1));
    }

    @Override
    public LocalTime generateValue() {
        return switch (generator) {
        case CONSTANT -> start;
        case INCREMENT -> {
            LocalTime current = currentInc;
            currentInc = current.plusNanos(step.toNanoOfDay());
            yield current;
        }
        case RANDOM -> generateRandom();
        default -> null;
        };
    }

    @Override
    public String generateAsString() {
        if (generator == DataGenerator.ANY) {
            return any;
        }
        LocalTime value = generateValue();
        return value == null ? "null" : ("'" + value + "'"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    }

    @Override
    protected LocalTime generateRandom(Random ran) {
        return start.plus((long)((((end.toSecondOfDay() - start.toSecondOfDay())
                * 1000) + 1) * ran.nextDouble()) , ChronoUnit.MILLIS);
    }

    @Override
    public int getMaxValues() {
        long beginTime = start.toSecondOfDay() * 1000L;
        long endTime = end.toSecondOfDay() * 1000L;
        long values = endTime - beginTime + 1;
        return values > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) values;
    }

    @Override
    protected LocalTime valueFromString(String s) {
        try {
            return LocalTime.parse(s);
        } catch (DateTimeParseException ex) {
            throw new DateTimeException(
                    EXP_FORMAT.formatted(ex.getParsedString(), "HH:MM:SS"), ex); //$NON-NLS-1$
        }
    }

    @Override
    public String getStepAsString() {
        return Duration.between(LocalTime.MIN, step).toString();
    }

    @Override
    public void setStepFromString(String step) {
        try {
            setStep(LocalTime.ofNanoOfDay((Duration.parse(step).toNanos())));
        } catch (DateTimeParseException ex) {
            throw new DateTimeException(
                    EXP_FORMAT.formatted(ex.getParsedString(), Messages.Duration_expected_format), ex);
        }
    }
}