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

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.capgemini.reports.reports.parameters.IChecker;
import com.teamcenter.soa.client.model.strong.Item;
import lombok.Getter;

import java.util.List;

/**
 * This class checks if a product exists by looking for its ID in Teamcenter.
 */
public class ProductChecker implements IChecker {

    private final String productID;
    @Getter
    private Item product;

    /**
     * Simple constructor.
     *
     * @param aProductID a String
     */
    public ProductChecker(final String aProductID) {
        productID = aProductID;
    }

    @Override
    public void check(final TcSession session) throws InvalidUserInputException {
        if (productID == null || productID.isEmpty()) {
            throw new InvalidUserInputException(getErrorMsg());
        }
        List<Item> queryObjects = new ItemQuery(session).searchID(productID).searchProduct().findAll();
        if ((queryObjects == null) || (queryObjects.isEmpty())) {
            throw new InvalidUserInputException(getErrorMsg());
        } else {
            product = queryObjects.get(0);
        }
    }

    private UserInputErrorEnum getErrorMsg() {
        return UserInputErrorEnum.PRODUCT_DOES_NOT_EXIST_NO_DATA_TO_PRINT;
    }
}
