/*
 * The MIT License
 *
 * Copyright 2014 Stephen Stafford <clothcat@gmail.com>.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.clothcat.hpoolauto;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/** Functionality that wraps JSON objects and writes them to the filsystem
 *
 * @author Stephen Stafford <clothcat@gmail.com>
 */
public class JsonFileHelper {
    public static void writeToFile(JSONObject jo, String filename){
        // an array wrapper that we use to allow us to put a comment at the top
        // of the file to tell people not to edit
        JSONArray ja = new JSONArray();
        ja.put("This file is part of the HyperPool Automation Tool (HAT)");
        ja.put("© 2014 Stephen Stafford — all rights wossnamed");
        ja.put("DO NOT EDIT THIS FILE BY HAND.  BAD THINGS WILL HAPPEN!!");
        ja.put(jo);
        String s = ja.toString();
        s=prettify(s);
        System.out.println(s);
        
        String filepath = System.getProperty("user.home")+"/.Hyperpool/JSON/";
        File f = new File(filepath);
        f.mkdirs();
        f=new File(filepath+filename);
        try(PrintWriter p = new PrintWriter(f)){
            p.println(s);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(JsonFileHelper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static String prettify(String json){
        return new GsonBuilder().setPrettyPrinting().create().toJson(new JsonParser().parse(json));
    }
    
    public static void main(String[] args) throws JSONException {
        String s = "{\"poolAddress\":\"PADDA\",\"investments\":[{\"datestamp\":1415838826084,\"amount\":1000,\"fromAddress\":\"A\"},{\"datestamp\":1415838826084,\"amount\":2000,\"fromAddress\":\"B\"}],\"startTimestamp\":0,\"poolName\":\"Pool1\",\"status\":\"FILLING\"}";
        JSONObject jo = new JSONObject(s);
        writeToFile(jo, "foo");
    }
}

Add functionality to write JSON objects to files. TODO: need to write the functionality to read them back again.