package io.loli.sc.server.service;

import io.loli.sc.server.dao.BucketDao;
import io.loli.sc.server.entity.StorageBucket;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

@Named
@Singleton
public class BucketService {
    private static List<StorageBucket> bucketList;
    private static StorageBucket[] bucketArray;

    private static StorageBucket[] imageArray;
    private static StorageBucket[] fileArray;

    @Inject
    public BucketService(BucketDao bucketDao) {
        if (bucketList == null) {
            bucketList = bucketDao.list();
        }
        if (bucketArray == null) {
            bucketArray = bucketList.toArray(new StorageBucket[bucketList
                    .size()]);
        }
        if (imageArray == null) {
            Stream<StorageBucket> str = bucketList.stream().filter(
                    item -> item.getFileType().equals(StorageBucket.IMG_TYPE));
            fileArray = str.collect(Collectors.toList()).toArray(
                    new StorageBucket[(int) str.count()]);
        }
        if (fileArray == null) {
            Stream<StorageBucket> str = bucketList.stream().filter(
                    item -> item.getFileType().equals(StorageBucket.FILE_TYPE));
            fileArray = str.collect(Collectors.toList()).toArray(
                    new StorageBucket[(int) str.count()]);
        }

    }

    public List<StorageBucket> list() {
        return bucketList;
    }

    public StorageBucket[] listWithArray() {
        return bucketArray;
    }

    public StorageBucket randomImageBucket() {
        double d = Math.random();
        int i = (int) (d * imageArray.length);
        return imageArray[i];
    }

    public StorageBucket randomFileBucket() {
        double d = Math.random();
        int i = (int) (d * fileArray.length);
        return fileArray[i];
    }

    public StorageBucket randomBucket() {
        double d = Math.random();
        int i = (int) (d * bucketArray.length);
        return bucketArray[i];
    }

}

@@ -179,40 +179,8 @@
 ors;
-%0Aimport java.util.stream.Stream;
 %0A%0Aim
@@ -840,38 +840,36 @@
 ) %7B%0A            
-Stream
+List
 %3CStorageBucket%3E 
@@ -860,35 +860,36 @@
 %3CStorageBucket%3E 
+li
 st
-r
  = bucketList.st
@@ -877,32 +877,53 @@
 ist = bucketList
+%0A                    
 .stream().filter
@@ -911,32 +911,24 @@
    .stream()
-.filter(
 %0A           
@@ -928,32 +928,40 @@
                 
+.filter(
 item -%3E item.get
@@ -970,32 +970,61 @@
 leType().equals(
+%0A                            
 StorageBucket.IM
@@ -1027,25 +1027,24 @@
 t.IMG_TYPE))
-;
 %0A           
@@ -1040,39 +1040,32 @@
             
-fileArray = str
+        
 .collect(Col
@@ -1077,33 +1077,25 @@
 rs.toList())
-.toArray(
+;
 %0A           
@@ -1091,32 +1091,49 @@
             
-        
+fileArray = list.toArray(
 new StorageB
@@ -1134,39 +1134,33 @@
 orageBucket%5B
-(int) str.count
+list.size
 ()%5D);%0A      
@@ -1212,14 +1212,12 @@
     
-Stream
+List
 %3CSto
@@ -1228,19 +1228,20 @@
 Bucket%3E 
+li
 st
-r
  = bucke
@@ -1245,16 +1245,37 @@
 cketList
+%0A                    
 .stream(
@@ -1275,24 +1275,16 @@
 stream()
-.filter(
 %0A       
@@ -1296,16 +1296,24 @@
         
+.filter(
 item -%3E 
@@ -1338,16 +1338,45 @@
 .equals(
+%0A                            
 StorageB
@@ -1392,17 +1392,16 @@
 E_TYPE))
-;
 %0A       
@@ -1409,23 +1409,16 @@
     
-fileArray = str
+        
 .col
@@ -1442,25 +1442,17 @@
 oList())
-.toArray(
+;
 %0A       
@@ -1456,24 +1456,41 @@
         
-        
+fileArray = list.toArray(
 new Stor
@@ -1503,23 +1503,17 @@
 ket%5B
-(int) str.count
+list.size
 ()%5D)

Fix test bugs