package common.models;

import common.contexts.UserContext;
import io.sphere.sdk.products.attributes.Attribute;
import io.sphere.sdk.products.attributes.AttributeAccess;
import io.sphere.sdk.products.attributes.AttributeExtraction;
import io.sphere.sdk.products.attributes.StringType;
import io.sphere.sdk.utils.functional.FunctionalUtils;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static org.apache.commons.lang3.ObjectUtils.firstNonNull;

public class ProductAttributeBean {
    private String label;
    private String key;
    private String value;

    public ProductAttributeBean() {
    }

    public ProductAttributeBean(final Attribute attr, final ProductDataConfig productDataConfig, final UserContext userContext) {
        this();
        final String label = productDataConfig.getMetaProductType()
                .findAttribute(attr.getName())
                .map(def -> def.getLabel().find(userContext.locales()).orElse(""))
                .orElse(null);
        setLabel(label);
        setKey(attr.getName());
        final String value = formatValue(attr, productDataConfig, userContext);
        setValue(value);
    }

    private String formatValue(final Attribute attr, final ProductDataConfig productDataConfig, final UserContext userContext) {
        return AttributeExtraction.<String>of(productDataConfig.getMetaProductType(), attr)
                    .ifIs(AttributeAccess.ofLocalizedString(), v -> v.find(userContext.locales()).orElse(""))
                    .ifIs(AttributeAccess.ofLocalizedEnumValue(), v -> v.getLabel().find(userContext.locales()).orElse(""))
                    .ifIs(AttributeAccess.ofEnumValue(), v -> v.getLabel())
                    .ifIs(AttributeAccess.ofString(), v -> v)
                    .findValue()
                    .orElse(null);
    }

    public static List<ProductAttributeBean> collect(final List<Attribute> attributes, final ProductDataConfig productDataConfig, final UserContext userContext) {
        return firstNonNull(attributes, Collections.<Attribute>emptyList()).stream()
                .filter(attr -> productDataConfig.getAttributeWhiteList().contains(attr.getName()))
                .map(attr -> new ProductAttributeBean(attr, productDataConfig, userContext))
                .collect(toList());
    }

    public String getKey() {
        return key;
    }

    public void setKey(final String key) {
        this.key = key;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(final String label) {
        this.label = label;
    }

    public String getValue() {
        return value;
    }

    public void setValue(final String value) {
        this.value = value;
    }
}

@@ -229,248 +229,62 @@
 on;%0A
-import io.sphere.sdk.products.attributes.StringType;%0Aimport io.sphere.sdk.utils.functional.FunctionalUtils;%0A%0Aimport java.util.Collections;%0Aimport java.util.List;%0Aimport java.util.Optional;%0A%0Aimport static java.util.stream.Collectors.joining;
+%0Aimport java.util.Collections;%0Aimport java.util.List;%0A
 %0Aimp
@@ -451,21 +451,20 @@
  String 
-label
+name
 ;%0A    pr
@@ -937,21 +937,20 @@
      set
-Label
+Name
 (label);
@@ -2397,21 +2397,20 @@
 ring get
-Label
+Name
 () %7B%0A   
@@ -2421,21 +2421,20 @@
  return 
-label
+name
 ;%0A    %7D%0A
@@ -2453,21 +2453,20 @@
 void set
-Label
+Name
 (final S
@@ -2471,21 +2471,20 @@
  String 
-label
+name
 ) %7B%0A    
@@ -2496,21 +2496,19 @@
 his.
-label = label
+name = name
 ;%0A  

Fix product attribute description label in checkout thankyou.