package com.google.sps.data;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.appengine.tools.cloudstorage.GcsFileOptions;
import com.google.appengine.tools.cloudstorage.GcsFilename;
import com.google.appengine.tools.cloudstorage.GcsInputChannel;
import com.google.appengine.tools.cloudstorage.GcsOutputChannel;
import com.google.appengine.tools.cloudstorage.GcsService;
import com.google.appengine.tools.cloudstorage.GcsServiceFactory;
import com.google.appengine.tools.cloudstorage.RetryParams;
import com.google.appengine.api.blobstore.UploadOptions;
import com.google.appengine.api.blobstore.FileInfo;

import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageClass;
import com.google.cloud.storage.StorageOptions;

public class CloudStorageLibrary {

    //Generates GCS file path. Still working on this. Explain logic and explain what the file path looks like
    public static String getGcsFilePath(HttpServletRequest request, BlobstoreService blobstore) {
        Map<String, List<FileInfo>> files = blobstore.getFileInfos(request);
        Set< Map.Entry<String, List<FileInfo>> > fileSet = files.entrySet();

        for (Map.Entry<String, List<FileInfo>> fileMap : fileSet) { 
            try {
                return fileMap.getValue().get(0).getGsObjectName();
            } catch (Exception e) {
                return "";
            }
        }
        return "";
    }

    public static String getServingFileUrl(BlobstoreService blobstore, String GcsFilePath) {
        String tempFilePath = GcsFilePath.replaceFirst("/gs", "");

        return "https://storage.googleapis.com" + tempFilePath;
    }

    //Check file name here
    public static String getUploadedFileUrl(BlobstoreService blobstore, String GcsFilePath) {
        BlobKey blobKey = blobstore.createGsBlobKey(GcsFilePath);
        
        return "/getBlobstoreUrl?blobKey=" + blobKey.getKeyString();
    }

    //Creates a bucket with the given project and bucket name
    public static void CreateBucket(String projectId, String bucketName) {
        Storage storage = StorageOptions.newBuilder().setProjectId(projectId).
                          build().getService();
                          
        StorageClass storageClass = StorageClass.STANDARD;

        String location = "us-east1";

        Bucket bucket = storage.create(BucketInfo.newBuilder(bucketName).
                        setStorageClass(storageClass).setLocation(location).
                        build());

        System.out.println("Created bucket " + bucket.getName()
                           + " in " + bucket.getLocation()
                           + " with storage class "
                           + bucket.getStorageClass());
    }
}
@@ -1508,16 +1508,30 @@
 %0A%0A    //
+TODO(mrjwash) 
 Generate
@@ -1550,32 +1550,8 @@
 ath.
- Still working on this. 
 Expl
@@ -1646,142 +1646,44 @@
 ath(
-HttpServletRequest request, BlobstoreService blobstore) %7B%0A        Map%3CString, List%3CFileInfo%3E%3E files = blobstore.getFileInfos(request);
+Map%3CString, List%3CFileInfo%3E%3E files) %7B
 %0A   
@@ -1691,13 +1691,13 @@
     
-Set%3C 
+for (
 Map.
@@ -1729,20 +1729,18 @@
 fo%3E%3E
- %3E
  file
-Set =
+Map :
  fil
@@ -1756,75 +1756,8 @@
 et()
-;%0A%0A        for (Map.Entry%3CString, List%3CFileInfo%3E%3E fileMap : fileSet
 ) %7B 
@@ -1875,24 +1875,63 @@
 eption e) %7B%0A
+                System.out.println(e);%0A
             
@@ -2003,97 +2003,259 @@
     
-public static String getServingFileUrl(BlobstoreService blobstore, String GcsFilePath) %7B%0A
+//Gets a url to directly serve the image%0A    public static String getServingFileUrl(String gcsFilePath) %7B%0A        if(gcsFilePath == null) %7B%0A            return %22%22;%0A        %7D else if (gcsFilePath.isEmpty()) %7B%0A            return %22%22;%0A        %7D else %7B%0A    
     
@@ -2280,17 +2280,17 @@
 ePath = 
-G
+g
 csFilePa
@@ -2318,32 +2318,36 @@
 , %22%22);%0A%0A        
+ 
+   
 return %22https://
@@ -2386,16 +2386,26 @@
 lePath;%0A
+        %7D%0A
     %7D%0A%0A 
@@ -2409,16 +2409,30 @@
 %0A%0A    //
+TODO(mrjwash) 
 Check fi
@@ -2523,17 +2523,17 @@
  String 
-G
+g
 csFilePa
@@ -2590,17 +2590,17 @@
 BlobKey(
-G
+g
 csFilePa
@@ -2903,17 +2903,16 @@
 ojectId)
-.
 %0A       
@@ -2926,24 +2926,25 @@
             
+.
 build().getS
@@ -3151,17 +3151,16 @@
 ketName)
-.
 %0A       
@@ -3176,16 +3176,17 @@
         
+.
 setStora
@@ -3228,17 +3228,16 @@
 ocation)
-.
 %0A       
@@ -3253,16 +3253,17 @@
         
+.
 build())

Fix minor changes