package com.github.bingoohuang.westcache.snapshot;

import com.alibaba.fastjson.JSON;
import com.github.bingoohuang.westcache.base.WestCacheSnapshot;
import com.google.common.base.Charsets;
import com.google.common.base.Optional;
import com.google.common.io.Files;
import lombok.SneakyThrows;
import lombok.val;

import java.io.File;

import static com.alibaba.fastjson.serializer.SerializerFeature.WriteClassName;

/**
 * @author bingoohuang [bingoohuang@gmail.com] Created on 2016/12/22.
 */
public class FileCacheSnapshot implements WestCacheSnapshot {
    @Override @SneakyThrows
    public void saveSnapshot(String cacheKey, Object cacheValue) {
        val json = JSON.toJSONString(cacheValue, WriteClassName);

        File snapshotFile = getSnapshotFile(cacheKey);
        Files.write(json, snapshotFile, Charsets.UTF_8);
    }

    @Override @SneakyThrows @SuppressWarnings("unchecked")
    public <T> Optional<T> readSnapshot(String cacheKey) {
        File snapshotFile = getSnapshotFile(cacheKey);
        if (!snapshotFile.exists() || !snapshotFile.isFile()) return null;

        String json = Files.toString(snapshotFile, Charsets.UTF_8);
        Object object = JSON.parse(json);
        return (Optional<T>) Optional.fromNullable(object);
    }

    @Override public void deleteSnapshot(String cacheKey) {
        File snapshotFile = getSnapshotFile(cacheKey);
        if (!snapshotFile.exists() || !snapshotFile.isFile()) return;

        snapshotFile.delete();
    }

    public static String USER_HOME = System.getProperty("user.home");
    public static String EXTENSION = ".westcache";

    public static File getSnapshotFile(String cacheKey) {
        File westCacheHome = tryCreateWestCacheHome();
        return new File(westCacheHome, cacheKey + EXTENSION);
    }

    public static void deleteSnapshotFile(String cacheKey) {
        File westCacheHome = tryCreateWestCacheHome();
        val file = new File(westCacheHome, cacheKey + EXTENSION);
        file.delete();
    }

    private static File tryCreateWestCacheHome() {
        val westCacheHome = new File(USER_HOME, EXTENSION);
        westCacheHome.mkdir();
        return westCacheHome;
    }
}

@@ -1788,220 +1788,8 @@
  %7D%0A%0A
-    public static void deleteSnapshotFile(String cacheKey) %7B%0A        File westCacheHome = tryCreateWestCacheHome();%0A        val file = new File(westCacheHome, cacheKey + EXTENSION);%0A        file.delete();%0A    %7D%0A%0A
     
@@ -1831,24 +1831,24 @@
 cheHome() %7B%0A
+
         val 
@@ -1891,24 +1891,24 @@
 EXTENSION);%0A
-
         west
@@ -1922,16 +1922,17 @@
 me.mkdir
+s
 ();%0A    

use mkdirs instead of mkdir