package org.cloudfoundry.autosleep.servicebroker.configuration;

import org.cloudfoundry.community.servicebroker.model.Catalog;
import org.cloudfoundry.community.servicebroker.model.Plan;
import org.cloudfoundry.community.servicebroker.model.ServiceDefinition;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@Configuration
public class CatalogConfiguration {
    @Bean(autowire = Autowire.BY_TYPE)
    public Catalog catalog() {
        return new Catalog(Arrays.asList(
                new ServiceDefinition(
                        "autosleep",
                        "autosleep",
                        "Service that put your application to sleep when inactive",
                        true,
                        false,
                        Arrays.asList(
                                new Plan("autosleep-plan",
                                        "Default autosleep Plan",
                                        "This is a default autosleep plan.  All services are created equally.",
                                        getPlanMetadata())),
                        Arrays.asList("autosleep", "document"),
                        getServiceDefinitionMetadata(),
                        null,
                        null)));
    }

    /* Used by Pivotal CF console */

    private Map<String, Object> getServiceDefinitionMetadata() {
        Map<String, Object> sdMetadata = new HashMap<>();
        sdMetadata.put("displayName", "Autosleep");
        sdMetadata.put("imageUrl", "https://en.wikipedia"
                + ".org/wiki/Sleep#/media/File:WLA_metmuseum_Bronze_statue_of_Eros_sleeping_7.jpg");
        sdMetadata.put("longDescription", "Autosleep Service");
        sdMetadata.put("providerDisplayName", "Orange");
        sdMetadata.put("documentationUrl", "https://github.com/Orange-OpenSource/autosleep");
        sdMetadata.put("supportUrl", "https://github.com/Orange-OpenSource/autosleep");
        return sdMetadata;
    }

    private Map<String, Object> getPlanMetadata() {
        Map<String, Object> planMetadata = new HashMap<>();
        planMetadata.put("costs", getCosts());
        planMetadata.put("bullets", getBullets());
        return planMetadata;
    }

    private List<Map<String, Object>> getCosts() {
        Map<String, Object> costsMap = new HashMap<>();

        Map<String, Object> amount = new HashMap<>();
        amount.put("eur", 0.0D);

        costsMap.put("amount", amount);
        costsMap.put("unit", "MONTHLY");

        return Collections.singletonList(costsMap);
    }

    private List<String> getBullets() {
        return Collections.singletonList("Shared autosleep service");
    }
}

@@ -451,120 +451,13 @@
 til.
-Arrays;%0Aimport java.util.Collections;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
+*
 ;%0A%0A%0A
-%0A
 @Con
@@ -503,16 +503,185 @@
 ation %7B%0A
+%0A    private static final String OPTIN_GUID=%2278C0A1DB-ACC9-4B6D-AF22-A1EF63C2CE06%22;%0A    private static final String OPTOUT_GUID=%22FE16E9E3-0D61-4D65-8A2F-2AFDD093E674%22;%0A%0A
     @Bea
@@ -1126,24 +1126,18 @@
 lan(
-%22autosleep-plan%22
+OPTIN_GUID
 ,%0A  
@@ -1179,29 +1179,13 @@
    %22
-Default autosleep Pla
+opt-i
 n%22,%0A
@@ -1264,101 +1264,254 @@
 n.  
-All services are created equally.%22,%0A                                        getPlanMetadata()
+Binded apps will be stopped when idle.%22),%0A                                new Plan(OPTOUT_GUID,%0A                                        %22opt-out%22,%0A                                        %22All apps will be stopped when idle, EXCEPTED binded apps.%22
 )),%0A
@@ -2413,705 +2413,7 @@
  %7D%0A%0A
-    private Map%3CString, Object%3E getPlanMetadata() %7B%0A        Map%3CString, Object%3E planMetadata = new HashMap%3C%3E();%0A        planMetadata.put(%22costs%22, getCosts());%0A        planMetadata.put(%22bullets%22, getBullets());%0A        return planMetadata;%0A    %7D%0A%0A    private List%3CMap%3CString, Object%3E%3E getCosts() %7B%0A        Map%3CString, Object%3E costsMap = new HashMap%3C%3E();%0A%0A        Map%3CString, Object%3E amount = new HashMap%3C%3E();%0A        amount.put(%22eur%22, 0.0D);%0A%0A        costsMap.put(%22amount%22, amount);%0A        costsMap.put(%22unit%22, %22MONTHLY%22);%0A%0A        return Collections.singletonList(costsMap);%0A    %7D%0A%0A    private List%3CString%3E getBullets() %7B%0A        return Collections.singletonList(%22Shared autosleep service%22);%0A    %7D
 %0A%7D%0A

Remove costs and bullets. Add opt-in and opt-ou plans