package com.angcyo.library;

import android.content.Context;
import android.text.TextUtils;
import android.text.format.Formatter;

import com.angcyo.library.utils.L;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.observables.SyncOnSubscribe;
import rx.schedulers.Schedulers;

/**
 * 应用程序缓存管理, 用来清理, 计算缓存大小
 * Created by angcyo on 2017/09/30 0030.
 */

public class CacheManager {

    /**
     * 缓存目录列表
     */
    List<String> cachePaths = new ArrayList<>();

    private CacheManager() {
    }

    public static CacheManager instance() {
        return Holder.instance;
    }

    private static class Holder {
        static CacheManager instance = new CacheManager();
    }

    /**
     * 获取整个Cache文件夹大小
     */
    public String getAllCacheFolderSize(Context context) {
        try {
            long size = 0;

            for (String path : cachePaths) {
                size += getFolderSize(new File(path));
            }

            return Formatter.formatFileSize(context, size);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "0";
    }

    /**
     * 打印log
     */
    public void log(Context context) {
        for (String path : cachePaths) {
            L.e(path + " -> " + Formatter.formatFileSize(context, getFolderSize(new File(path))));
        }
    }

    /**
     * 添加缓存目录, 需要用来统计大小的目录
     */
    public void addCachePath(String... paths) {
        for (String path : paths) {
            if (cachePaths.contains(path)) {
                continue;
            }
            cachePaths.add(path);
        }
    }


    /**
     * 清理整个cache文件夹的文件
     */
    public Observable<Boolean> clearCacheFolder() {
        return Observable
                .create(new SyncOnSubscribe<Integer, Boolean>() {
                    @Override
                    protected Integer generateState() {
                        return 1;
                    }

                    @Override
                    protected Integer next(Integer state, Observer<? super Boolean> observer) {
                        if (state > 0) {
                            for (String path : cachePaths) {
                                deleteFolderFile(path, false);
                            }
                            observer.onNext(true);
                            observer.onCompleted();
                        }
                        return -1;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    /**
     * 获取指定文件夹内所有文件大小的和
     *
     * @param file file
     * @return size
     */
    public static long getFolderSize(File file) {
        long size = 0;
        try {
            File[] fileList = file.listFiles();
            for (File aFileList : fileList) {
                if (aFileList.isDirectory()) {
                    size = size + getFolderSize(aFileList);
                } else {
                    size = size + aFileList.length();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return size;
    }

    /**
     * 删除指定目录下的文件，这里用于缓存的删除
     *
     * @param filePath       filePath
     * @param deleteThisPath deleteThisPath
     */
    private boolean deleteFolderFile(String filePath, boolean deleteThisPath) {
        if (!TextUtils.isEmpty(filePath)) {
            File file = new File(filePath);
            return deleteFolderFile(file, deleteThisPath);
        }
        return false;
    }

    private boolean deleteFolderFile(File file, boolean deleteThisPath) {
        try {
            if (file.isDirectory()) {
                File files[] = file.listFiles();
                for (File file1 : files) {
                    deleteFolderFile(file1.getAbsolutePath(), true);
                }
            }
            if (deleteThisPath) {
                if (!file.isDirectory()) {
                    file.delete();
                } else {
                    if (file.listFiles().length == 0) {
                        file.delete();
                    }
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}

@@ -2770,32 +2770,1005 @@
 d());%0D%0A    %7D%0D%0A%0D%0A
+    public static Observable%3CBoolean%3E clearCacheFolder(final String... paths) %7B%0D%0A        return Observable%0D%0A                .create(new SyncOnSubscribe%3CInteger, Boolean%3E() %7B%0D%0A                    @Override%0D%0A                    protected Integer generateState() %7B%0D%0A                        return 1;%0D%0A                    %7D%0D%0A%0D%0A                    @Override%0D%0A                    protected Integer next(Integer state, Observer%3C? super Boolean%3E observer) %7B%0D%0A                        if (state %3E 0) %7B%0D%0A                            for (String path : paths) %7B%0D%0A                                deleteFolderFile(path, false);%0D%0A                            %7D%0D%0A                            observer.onNext(true);%0D%0A                            observer.onCompleted();%0D%0A                        %7D%0D%0A                        return -1;%0D%0A                    %7D%0D%0A                %7D)%0D%0A                .subscribeOn(Schedulers.io())%0D%0A                .observeOn(AndroidSchedulers.mainThread());%0D%0A    %7D%0D%0A%0D%0A
     /**%0D%0A     * 
@@ -3752,32 +3752,32 @@
   %7D%0D%0A%0D%0A    /**%0D%0A
-
      * %E8%8E%B7%E5%8F%96%E6%8C%87%E5%AE%9A%E6%96%87%E4%BB%B6%E5%A4%B9%E5%86%85%E6%89%80
@@ -4510,32 +4510,39 @@
 */%0D%0A    private 
+static 
 boolean deleteFo
@@ -4779,32 +4779,32 @@
 alse;%0D%0A    %7D%0D%0A%0D%0A
-
     private bool
@@ -4799,16 +4799,23 @@
 private 
+static 
 boolean 

fix cache manager