package nu.studer.teamcity.buildscan.internal;

import jetbrains.buildServer.serverSide.CustomDataStorage;
import jetbrains.buildServer.serverSide.SBuild;
import jetbrains.buildServer.util.StringUtil;
import nu.studer.teamcity.buildscan.BuildScanReference;

import java.util.ArrayList;
import java.util.List;

final class DefaultBuildScanDataStore implements BuildScanDataStore {

    private static final String BUILD_SCAN_STORAGE = "nu.studer.teamcity.buildscan.DefaultBuildScanDataStore";
    private static final String SEPARATOR = "|";

    @Override
    public void mark(SBuild build) {
        String buildId = String.valueOf(build.getBuildId());
        CustomDataStorage customDataStorage = build.getBuildType().getCustomDataStorage(BUILD_SCAN_STORAGE);
        String existing = customDataStorage.getValue(buildId);

        if (existing != null) {
            return;
        }

        customDataStorage.putValue(buildId, "");
    }

    @Override
    public void store(SBuild build, String buildScanUrl) {
        String buildId = String.valueOf(build.getBuildId());
        CustomDataStorage customDataStorage = build.getBuildType().getCustomDataStorage(BUILD_SCAN_STORAGE);
        String existing = customDataStorage.getValue(buildId);

        if (existing == null) {
            customDataStorage.putValue(buildId, buildScanUrl);
        } else {
            List<String> scans = StringUtil.split(existing, SEPARATOR);
            scans.add(buildScanUrl);

            customDataStorage.putValue(buildId, StringUtil.join(SEPARATOR, scans));
        }
    }

    @Override
    public List<BuildScanReference> fetch(SBuild build) {
        String buildId = String.valueOf(build.getBuildId());
        String rawScanList = build.getBuildType().getCustomDataStorage(BUILD_SCAN_STORAGE).getValue(buildId);

        if (rawScanList == null) {
            return null;
        }

        List<BuildScanReference> buildScanReferences = new ArrayList<>();
        List<String> scans = StringUtil.split(rawScanList, SEPARATOR);
        for (String scanUrl : scans) {
            if (!scanUrl.isEmpty()) {
                buildScanReferences.add(new BuildScanReference(Util.getBuildScanId(scanUrl), scanUrl));
            }
        }

        return buildScanReferences;
    }

}

@@ -249,16 +249,58 @@
 ference;
+%0Aimport org.jetbrains.annotations.NotNull;
 %0A%0Aimport
@@ -466,16 +466,19 @@
 _STORAGE
+_ID
  = %22nu.s
@@ -562,16 +562,32 @@
  String 
+BUILD_SCAN_URLS_
 SEPARATO
@@ -754,37 +754,16 @@
 orage = 
-build.getBuildType().
 getCusto
@@ -775,34 +775,21 @@
 Storage(
-BUILD_SCAN_STORAGE
+build
 );%0A     
@@ -868,17 +868,17 @@
 xisting 
-!
+=
 = null) 
@@ -887,35 +887,8 @@
     
-        return;%0A        %7D%0A%0A
     
@@ -932,16 +932,26 @@
 d, %22%22);%0A
+        %7D%0A
     %7D%0A%0A 
@@ -1129,37 +1129,16 @@
 orage = 
-build.getBuildType().
 getCusto
@@ -1150,34 +1150,21 @@
 Storage(
-BUILD_SCAN_STORAGE
+build
 );%0A     
@@ -1394,16 +1394,32 @@
 isting, 
+BUILD_SCAN_URLS_
 SEPARATO
@@ -1524,16 +1524,32 @@
 il.join(
+BUILD_SCAN_URLS_
 SEPARATO
@@ -1722,90 +1722,118 @@
     
-String rawScanList = build.getBuildType().getCustomDataStorage(BUILD_SCAN_STORAGE)
+CustomDataStorage customDataStorage = getCustomDataStorage(build);%0A        String existing = customDataStorage
 .get
@@ -1861,27 +1861,24 @@
     if (
-rawScanList
+existing
  == null
@@ -1907,33 +1907,39 @@
  null;%0A        %7D
-%0A
+ else %7B
 %0A        List%3CBu
@@ -1927,24 +1927,28 @@
 e %7B%0A        
+    
 List%3CBuildSc
@@ -2001,32 +2001,36 @@
 st%3C%3E();%0A        
+    
 List%3CString%3E sca
@@ -2055,21 +2055,34 @@
 lit(
-rawScanList, 
+existing, BUILD_SCAN_URLS_
 SEPA
@@ -2081,32 +2081,36 @@
 RLS_SEPARATOR);%0A
+    
         for (Str
@@ -2140,24 +2140,28 @@
             
+ 
+   
 if (!scanUrl
@@ -2170,24 +2170,28 @@
 sEmpty()) %7B%0A
+    
             
@@ -2298,18 +2298,25 @@
     
-%7D%0A
+    %7D%0A   
         
 %7D%0A%0A 
@@ -2307,27 +2307,32 @@
 %0A           
+ 
 %7D%0A%0A
+    
         retu
@@ -2355,16 +2355,236 @@
 rences;%0A
+        %7D%0A    %7D%0A%0A    @NotNull%0A    private CustomDataStorage getCustomDataStorage(SBuild build) %7B%0A        //noinspection ConstantConditions%0A        return build.getBuildType().getCustomDataStorage(BUILD_SCAN_STORAGE_ID);%0A
     %7D%0A%0A%7D

Simplify code a bit