package com.capgemini.reports.reports.parameters;

import com.capgemini.framework.common.StringHelper;
import com.capgemini.framework.common.TeamcenterDateFormatter;
import lombok.EqualsAndHashCode;
import lombok.Getter;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Represents a date.<br>
 * Can represent NOW or a specific date in time. Its {@link Calendar} attribute (dateTime ) may be null to represent 'now'.
 * 
 */
@Getter
@EqualsAndHashCode
public class EffectivityDate implements Serializable {

    private static final long serialVersionUID = -5328049346240627147L;

    /** Is null when {@code isNow == true}. */
    private final Calendar dateTime;
    private final boolean now;

    /**
     * Creates a date. If the Calendar is null, the date will be the current date.
     * 
     * @param dateTime the Calendar date
     */
    public EffectivityDate(final Calendar dateTime) {
        super();
        this.now = dateTime == null;
        this.dateTime = dateTime;
    }

    /**
     * Creates a date. If the input String is null or empty, the date will be the current date.
     * 
     * @param date the date with format : dd-MM-yyyy.
     */
    public EffectivityDate(final String date) {
        this.now = date == null || date.isEmpty();
        this.dateTime = now ? null : TeamcenterDateFormatter.makeTeamcenterCalendar(date);
    }

    /** Creates a {@link EffectivityDate} representing the current time. */
    public EffectivityDate() {
        super();
        this.dateTime = null;
        this.now = true;
    }

    /**
     * Returns a String representation of this effectivity in a format that Tc can recognize in a query. <br>
     * Ex {@code 09-Feb-2015}
     * 
     * @return the date as a {@link String}, using {@link Locale#US}
     */
    public String getFormattedDateForTcRequest() {
        return TeamcenterDateFormatter.translateDateFormatForRequest(dateTime);
    }

    /**
     * Returns a String representation of this effectivity in a format that a French User can understand. <br>
     * Uses : {@code dd mmmm yyyy} and {@link TimeZone}: Europe (Paris) and french Locale. Ex: {@code 08 Juillet 2015}
     * 
     * @return the date as a {@link String}, using {@link Locale#US}
     */
    public String getFormattedDateForFrenchUser() {
        return TeamcenterDateFormatter.formatCalendarToFrench(now ? Calendar.getInstance() : dateTime);
    }

    /**
     * Format date with the GUI format: dd-MM-yyyy.
     * 
     * @return String value of date
     */
    public String formatAsInputCriteria() {

        String theFormatedDate = StringHelper.EMPTY;

        if (dateTime != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy", Locale.FRENCH);
            sdf.setTimeZone(TeamcenterDateFormatter.getParisTimeZone());
            theFormatedDate = sdf.format(dateTime.getTime());
        }

        return theFormatedDate;
    }

    @Override
    public String toString() {
        return "TcEffectivity date : " + (now ? "now" : dateTime.getTime().toString());
    }



}
