package com.capgemini.reports.reports.parameters.checker;

import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.reports.exceptions.InvalidUserInputException;
import com.capgemini.reports.exceptions.UserInputErrorEnum;
import com.capgemini.reports.reports.parameters.EffectivityDate;
import com.capgemini.reports.reports.parameters.IChecker;
import lombok.extern.slf4j.Slf4j;

import java.util.Calendar;

/**
 * Checks is a given {@link EffectivityDate} is in a valid state.
 */
@Slf4j
public class EffectivityChecker implements IChecker {

    private final EffectivityDate effectivity;

    /**
     * Builds a {@link IChecker} to verify the validity of the provided {@link EffectivityDate}.
     */
    public EffectivityChecker(final EffectivityDate effectivity) {
        this.effectivity = effectivity;
    }

    @Override
    public void check(final TcSession session) throws InvalidUserInputException {
        if (!effectivity.isNow()) {
            Calendar dateTime = effectivity.getDateTime();
            if (dateTime == null) {
                log.error("EffectivityDate state is invalid: Calendar is null but TcEffectivity is not NOW.");
                throw new InvalidUserInputException(UserInputErrorEnum.INTERNAL_ERROR);
            }
        }
    }
}
