/**
 * hub-common
 *
 * Copyright (C) 2018 Black Duck Software, Inc.
 * http://www.blackducksoftware.com/
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.blackducksoftware.integration.hub.notification.content.detail;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.blackducksoftware.integration.hub.api.generated.enumeration.NotificationType;
import com.blackducksoftware.integration.hub.api.view.CommonNotificationState;
import com.blackducksoftware.integration.hub.notification.content.NotificationContent;
import com.blackducksoftware.integration.hub.notification.content.PolicyOverrideNotificationContent;
import com.blackducksoftware.integration.hub.notification.content.RuleViolationClearedNotificationContent;
import com.blackducksoftware.integration.hub.notification.content.RuleViolationNotificationContent;
import com.blackducksoftware.integration.hub.notification.content.VulnerabilityNotificationContent;

public class NotificationContentDetailFactory {
    public List<NotificationContentDetail> generateNotificationContentDetails(final CommonNotificationState commonNotificationState) {
        return generateNotificationContentDetails(commonNotificationState.getType(), commonNotificationState.getContent());
    }

    public List<NotificationContentDetail> generateNotificationContentDetails(final NotificationType notificationType, final NotificationContent notificationContent) {
        List<NotificationContentDetail> detailsList;
        if (NotificationType.POLICY_OVERRIDE.equals(notificationType)) {
            detailsList = generatePolicyOverrideContentDetails((PolicyOverrideNotificationContent) notificationContent);
        } else if (NotificationType.RULE_VIOLATION.equals(notificationType)) {
            detailsList = generateRuleViolationContentDetails((RuleViolationNotificationContent) notificationContent);
        } else if (NotificationType.RULE_VIOLATION_CLEARED.equals(notificationType)) {
            detailsList = generateRuleViolationClearedContentDetails((RuleViolationClearedNotificationContent) notificationContent);
        } else if (NotificationType.VULNERABILITY.equals(notificationType)) {
            detailsList = generateVulnerabilityContentDetails((VulnerabilityNotificationContent) notificationContent);
        } else {
            detailsList = Collections.emptyList();
        }
        return detailsList;
    }

    public List<NotificationContentDetail> generatePolicyOverrideContentDetails(final PolicyOverrideNotificationContent content) {
        final List<NotificationContentDetail> details = new ArrayList<>();
        content.policyInfos.forEach(policyInfo -> {
            details.add(NotificationContentDetail.createDetail(content, NotificationContentDetail.CONTENT_KEY_GROUP_POLICY, content.projectName, content.projectVersionName, content.projectVersion, content.componentName,
                    content.component, content.componentVersionName, content.componentVersion, policyInfo.policyName, policyInfo.policy, null, null, null));
        });
        return details;
    }

    public List<NotificationContentDetail> generateRuleViolationContentDetails(final RuleViolationNotificationContent content) {
        final Map<String, String> uriToName = content.policyInfos.stream().collect(Collectors.toMap(policyInfo -> policyInfo.policy, policyInfo -> policyInfo.policyName));
        final List<NotificationContentDetail> details = new ArrayList<>();
        content.componentVersionStatuses.forEach(componentVersionStatus -> {
            componentVersionStatus.policies.forEach(policyUri -> {
                final String policyName = uriToName.get(policyUri);
                details.add(NotificationContentDetail.createDetail(content, NotificationContentDetail.CONTENT_KEY_GROUP_POLICY, content.projectName, content.projectVersionName, content.projectVersion, componentVersionStatus.componentName,
                        componentVersionStatus.component, componentVersionStatus.componentVersionName, componentVersionStatus.componentVersion, policyName, policyUri, null, componentVersionStatus.componentIssueLink, null));
            });
        });
        return details;
    }

    public List<NotificationContentDetail> generateRuleViolationClearedContentDetails(final RuleViolationClearedNotificationContent content) {
        final Map<String, String> uriToName = content.policyInfos.stream().collect(Collectors.toMap(policyInfo -> policyInfo.policy, policyInfo -> policyInfo.policyName));
        final List<NotificationContentDetail> details = new ArrayList<>();
        content.componentVersionStatuses.forEach(componentVersionStatus -> {
            componentVersionStatus.policies.forEach(policyUri -> {
                final String policyName = uriToName.get(policyUri);
                details.add(NotificationContentDetail.createDetail(content, NotificationContentDetail.CONTENT_KEY_GROUP_POLICY, content.projectName, content.projectVersionName, content.projectVersion, componentVersionStatus.componentName,
                        componentVersionStatus.component, componentVersionStatus.componentVersionName, componentVersionStatus.componentVersion, policyName, policyUri, null, componentVersionStatus.componentIssueLink, null));
            });
        });
        return details;
    }

    public List<NotificationContentDetail> generateVulnerabilityContentDetails(final VulnerabilityNotificationContent content) {
        final List<NotificationContentDetail> details = new ArrayList<>();
        content.affectedProjectVersions.forEach(projectVersion -> {
            details.add(NotificationContentDetail.createDetail(content, NotificationContentDetail.CONTENT_KEY_GROUP_VULNERABILITY, projectVersion.projectName, projectVersion.projectVersionName, projectVersion.projectVersion,
                    content.componentName, null, content.versionName, content.componentVersion, null, null, content.componentVersionOriginName, projectVersion.componentIssueUrl, content.componentVersionOriginId));
        });
        return details;
    }

}

@@ -3492,24 +3492,237 @@
 cyInfo -%3E %7B%0A
+            String componentValue;%0A            if (content.componentVersion != null) %7B%0A                componentValue = null;%0A            %7D else %7B%0A                componentValue = content.component;%0A            %7D%0A
             
@@ -3945,32 +3945,24 @@
             
-content.
 component, c
@@ -3954,24 +3954,29 @@
    component
+Value
 , content.co
@@ -4706,32 +4706,299 @@
 get(policyUri);%0A
+                String componentValue;%0A                if (componentVersionStatus.componentVersion != null) %7B%0A                    componentValue = null;%0A                %7D else %7B%0A                    componentValue = componentVersionStatus.component;%0A                %7D%0A
                 
@@ -5254,38 +5254,20 @@
 mponentV
-ersionStatus.component
+alue
 , compon
@@ -6079,32 +6079,299 @@
 get(policyUri);%0A
+                String componentValue;%0A                if (componentVersionStatus.componentVersion != null) %7B%0A                    componentValue = null;%0A                %7D else %7B%0A                    componentValue = componentVersionStatus.component;%0A                %7D%0A
                 
@@ -6627,38 +6627,20 @@
 mponentV
-ersionStatus.component
+alue
 , compon

Add check for componentVersion in detail generation.