package tutorials;

import io.sphere.sdk.client.SphereClient;
import io.sphere.sdk.models.Referenceable;
import io.sphere.sdk.models.TextInputHint;
import io.sphere.sdk.products.attributes.*;
import io.sphere.sdk.producttypes.ProductType;
import io.sphere.sdk.producttypes.ProductTypeDraft;
import io.sphere.sdk.producttypes.commands.ProductTypeCreateCommand;
import org.apache.commons.lang3.RandomStringUtils;

import java.util.concurrent.CompletionStage;

import static java.util.Arrays.asList;
import static tutorials.Utils.en;

public class ProductTypeExamples {

    public static CompletionStage<ProductType> createSimpleProductType(final SphereClient client) {
        final String typeKey = RandomStringUtils.randomAlphabetic(10);
        //startExample[createSimpleProductType]
        AttributeDefinition quantityContainedAttribute = AttributeDefinitionBuilder
                .of("quantityContained", en("quantity contained"), NumberAttributeType.of())
                .isRequired(true)
                .attributeConstraint(AttributeConstraint.NONE)
                .isSearchable(false)
                .build();
        AttributeDefinition nutrientTypeAttribute = AttributeDefinitionBuilder
                .of("nutrientTypeCode", en("nutrient type Code"), StringAttributeType.of())
                .isRequired(true)
                .attributeConstraint(AttributeConstraint.NONE)
                .isSearchable(false)
                .build();
        final ProductTypeDraft productTypeDraft =
                ProductTypeDraft.of(typeKey,
                        "nutrient-information",
                        "The nutrient-information product type.",
                        asList(quantityContainedAttribute, nutrientTypeAttribute));
        return client.execute(ProductTypeCreateCommand.of(productTypeDraft));
        //endExample[createSimpleProductType]
    }

    public static CompletionStage<ProductType> createAdvancedProductType(final SphereClient client, final Referenceable<ProductType> nestedProductType) {
        final String typeKey = RandomStringUtils.randomAlphabetic(10);
        //startExample[createAdvancedProductType]
        AttributeDefinition tasteAttribute = AttributeDefinitionBuilder
                .of("taste", en("taste"), StringAttributeType.of())
                .isRequired(true)
                .attributeConstraint(AttributeConstraint.NONE)
                .isSearchable(false)
                .build();
        AttributeDefinition nutrientsAttribute = AttributeDefinitionBuilder
                .of("nutrients", en("food nutrients"), SetAttributeType.of(NestedAttributeType.of(nestedProductType)))
                //nestedProductType is a Referenceable<ProductType>
                .isRequired(false)
                .attributeConstraint(AttributeConstraint.NONE)
                .isSearchable(false)
                .build();
        final ProductTypeDraft productTypeDraft = ProductTypeDraft
                .of(typeKey, "food-product-type", "The food product type.", asList(tasteAttribute, nutrientsAttribute));
        return client.execute(ProductTypeCreateCommand.of(productTypeDraft));
        //endExample[createAdvancedProductType]
    }

    public static CompletionStage<ProductType> createBundleProductType(final SphereClient client) {
        final String typeKey = RandomStringUtils.randomAlphabetic(10);
        //startExample[createBundleProductType]
        final AttributeDefinition boardAttributeDefinition = AttributeDefinitionBuilder.of("board", en("board"), ReferenceAttributeType.ofProduct())
                .isRequired(true)
                .isSearchable(true)
                .inputHint(TextInputHint.SINGLE_LINE)
                .build();
        final AttributeDefinition bindingAttributeDefinition = AttributeDefinitionBuilder.of("binding", en("binding"), ReferenceAttributeType.ofProduct())
                .isRequired(true)
                .isSearchable(true)
                .inputHint(TextInputHint.SINGLE_LINE)
                .build();
        final ProductTypeDraft productTypeDraft =
                ProductTypeDraft.of(typeKey, "SnowboardBundle", "this is the ProductType that contains the actual bundle", asList(boardAttributeDefinition, bindingAttributeDefinition));
        return client.execute(ProductTypeCreateCommand.of(productTypeDraft));
        //endExample[createBundleProductType]
    }
}