/**
 * Copyright (c) 2015 Bosch Software Innovations GmbH and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.eclipse.hawkbit.ui.utils;

import org.eclipse.hawkbit.repository.model.DistributionSet;
import org.eclipse.hawkbit.repository.model.SoftwareModule;

import static org.eclipse.hawkbit.ui.utils.HawkbitCommonUtil.HTML_LI_CLOSE_TAG;
import static org.eclipse.hawkbit.ui.utils.HawkbitCommonUtil.HTML_LI_OPEN_TAG;
import static org.eclipse.hawkbit.ui.utils.HawkbitCommonUtil.HTML_UL_CLOSE_TAG;
import static org.eclipse.hawkbit.ui.utils.HawkbitCommonUtil.HTML_UL_OPEN_TAG;

import java.util.Set;

import com.vaadin.data.Item;
import com.vaadin.ui.AbstractSelect.ItemDescriptionGenerator;
import com.vaadin.ui.Component;
import com.vaadin.ui.Table;

public class AssignInstalledDSTooltipGenerator  implements ItemDescriptionGenerator {
    private static final long serialVersionUID = 688730421728162456L;
    
    private static final String ASSIGN_DIST_SET = "assignedDistributionSet";
    private static final String INSTALL_DIST_SET = "installedDistributionSet";

    @Override
    public String generateDescription(final Component source, final Object itemId, final Object propertyId) {
        final DistributionSet distributionSet;
        final Item item = ((Table)source).getItem(itemId);
        if (propertyId != null) {
            if (propertyId.equals(SPUILabelDefinitions.ASSIGNED_DISTRIBUTION_NAME_VER)) {
                distributionSet = (DistributionSet) item.getItemProperty(ASSIGN_DIST_SET).getValue();
                return getDSDetails(distributionSet);
            } else if (propertyId.equals(SPUILabelDefinitions.INSTALLED_DISTRIBUTION_NAME_VER)) {
                distributionSet = (DistributionSet) item.getItemProperty(INSTALL_DIST_SET).getValue();
                return getDSDetails(distributionSet);
            }
        }
        return null;
    }

    private String getDSDetails(final DistributionSet distributionSet) {
        final StringBuilder swModuleNames = new StringBuilder();
        final StringBuilder swModuleVendors = new StringBuilder();
        final Set<SoftwareModule> swModules = distributionSet.getModules();
        swModules.forEach(swModule -> {
            swModuleNames.append(swModule.getName());
            swModuleNames.append(" , ");
            swModuleVendors.append(swModule.getVendor());
            swModuleVendors.append(" , ");
        });
        final StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(HTML_UL_OPEN_TAG);
        stringBuilder.append(HTML_LI_OPEN_TAG);
        stringBuilder.append(" DistributionSet Description : ").append(distributionSet.getDescription());
        stringBuilder.append(HTML_LI_CLOSE_TAG);
        stringBuilder.append(HTML_LI_OPEN_TAG);
        stringBuilder.append(" DistributionSet Type : ").append((distributionSet.getType()).getName());
        stringBuilder.append(HTML_LI_CLOSE_TAG);
        stringBuilder.append(HTML_LI_OPEN_TAG);
        stringBuilder.append(" Required Migration step : ")
                .append(distributionSet.isRequiredMigrationStep() ? "Yes" : "No");
        stringBuilder.append(HTML_LI_CLOSE_TAG);
        stringBuilder.append(HTML_LI_OPEN_TAG);
        stringBuilder.append("SoftWare Modules : ").append(swModuleNames.toString());
        stringBuilder.append(HTML_LI_CLOSE_TAG);
        stringBuilder.append(HTML_LI_OPEN_TAG);
        stringBuilder.append("Vendor(s) : ").append(swModuleVendors.toString());
        stringBuilder.append(HTML_LI_CLOSE_TAG);
        stringBuilder.append(HTML_UL_CLOSE_TAG);
        return stringBuilder.toString();
    }
  }



@@ -360,130 +360,8 @@
 s;%0A%0A
-import org.eclipse.hawkbit.repository.model.DistributionSet;%0Aimport org.eclipse.hawkbit.repository.model.SoftwareModule;%0A%0A
 impo
@@ -428,32 +428,32 @@
 L_LI_CLOSE_TAG;%0A
+
 import static or
@@ -698,16 +698,138 @@
 l.Set;%0A%0A
+import org.eclipse.hawkbit.repository.model.DistributionSet;%0Aimport org.eclipse.hawkbit.repository.model.SoftwareModule;%0A%0A
 import c
@@ -1019,17 +1019,16 @@
 nerator 
- 
 implemen
@@ -1131,13 +1131,9 @@
 6L;%0A
-    
 %0A
+
     
@@ -1490,16 +1490,17 @@
 ((Table)
+ 
 source).
@@ -2172,24 +2172,98 @@
 utionSet) %7B%0A
+        if (distributionSet == null) %7B%0A            return null;%0A        %7D%0A
         fina
@@ -3920,14 +3920,10 @@
 ;%0A    %7D%0A
-  %7D%0A%0A
+%7D
 %0A

add null check for distribution set