package com.capgemini.reports.reports.parameters;

import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.framework.teamcenter.queries.saved.modelobject.ItemQuery;
import com.capgemini.reports.exceptions.InvalidUserInputException;
import com.capgemini.reports.exceptions.UserInputErrorEnum;
import com.teamcenter.soa.client.model.strong.Item;
import com.teamcenter.soa.client.model.strong.ItemRevision;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

/**
 * Represents a Product-unit association. <br>
 */
@Slf4j
@EqualsAndHashCode
public class ProductUnit implements Serializable {
    private static final long serialVersionUID = -2970983080244741025L;

    private static final String EMPTY = "";

    private final String productID;

    /**
     * This is the raw user input String.
     */
    private final String unitStr;

    /**
     * This is the validated user input integer.
     */
    private int unit;

    public ProductUnit(String productID) {
        this(productID, null);
    }

    /**
     * Basic constructor.
     *
     * @param productID the ID of the product
     * @param unit      a unit
     */
    public ProductUnit(final String productID, final int unit) {
        super();
        this.productID = productID;
        this.unit = unit;
        this.unitStr = String.valueOf(unit);
    }

    /**
     * Basic constructor.
     *
     * @param productID the ID of the product
     * @param unitStr   a unit (String representation)
     */
    public ProductUnit(final String productID, final String unitStr) {
        super();
        this.productID = productID == null ? EMPTY : productID;
        this.unitStr = unitStr == null ? EMPTY : unitStr;
    }

    /**
     * Parse the unit String. After the parsing, the integer unit becomes available for further processing.
     *
     * @throws InvalidUserInputException if the unit could not be understood
     */
    public void parseUserUnit() throws InvalidUserInputException {
        try {
            this.unit = Integer.parseInt(unitStr);
        } catch (NumberFormatException e) {
            UserInputErrorEnum unitNotNumeric = UserInputErrorEnum.UNIT_NOT_NUMERIC;
            log.error(unitNotNumeric.getMsgEn() + ": \"" + unitStr + "\" ", e);
            throw new InvalidUserInputException(unitNotNumeric, e);
        }
    }

    /**
     * Returns the corresponding Teamcenter item (a product, Generic or Generic Assemb).
     *
     * @param session the {@link TcSession} to perform the query.
     * @return the {@link ItemRevision} representing the end item.
     */
    public Item getProduct(final TcSession session) {
        return new ItemQuery(session).searchID(productID).findAll().get(0);
    }

    public String getProductID() {
        return productID;
    }

    public int getUnit() {
        return unit;
    }

    public String getUnitStr() {
        return unitStr;
    }

    @Override
    public String toString() {
        return productID + "-" + unitStr;
    }
}
