 import SwiftyJSON
 
 enum TKTTPifierCache {
-  // TODO: Re-use TKJSONCache
-
+  private static let problemsDir = "problems"
+  private static let solutionsDir = "solutions"
   
   static func problemId(forParas paras: [String: AnyObject]) -> String? {
     let hash = inputHash(paras)

    let hash = inputHash(paras)
    
    if let id = problemId(hash) {
      let solutionURL = cacheURL("solutions", filename: id)
      try? NSFileManager.defaultManager().removeItemAtURL(solutionURL)
    }
    
    // Now clear the problem ID
    let problemURL = cacheURL("problems", filename: "\(hash)")
    try? NSFileManager.defaultManager().removeItemAtURL(problemURL)
  }

  static func solutionJson(forId id: String) -> JSON? {
    let filePath = cacheURL("solutions", filename: id)
    guard let data = NSData(contentsOfURL: filePath),
          let dict = NSKeyedUnarchiver.unarchiveObjectWithData(data) as? [String: AnyObject] else {
      return nil
    }
    return JSON(dict)
  }
  
  static func save(solutionJson json: JSON, forId id: String) {
    let filePath = cacheURL("solutions", filename: id)
    
    guard let dict = json.dictionaryObject else {
      SGKLog.warn("TKTTPifierCache", text: "Could not turn json into dictionary. JSON: \(json)")
      return
    }
    
    let data = NSKeyedArchiver.archivedDataWithRootObject(dict)
    data.writeToURL(filePath, atomically: true)
  }
  
  private static func inputHash(input: [String: AnyObject]) -> UInt {

  }
  
  private static func problemId(hash: UInt) -> String? {
    let dict = TKJSONCache.read("\(hash)", directory: .Cache, subdirectory: problemsDir) as? [String: String]
    return dict?["id"]
  }
  
  static func save(problemId id: String, forParas paras: [String: AnyObject]) {
    let hash = inputHash(paras)
    let dict = ["id": id]
    TKJSONCache.save("\(hash)", dictionary: dict, directory: .Cache, subdirectory: problemsDir)
    assert(problemId(forParas: paras) == id)
  }
  
