/*
 * Copyright 2020 Johan Walles <johan.walles@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gmail.walles.johan.exactype.stats;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import timber.log.Timber;

class StatsTracker {
    private final File backingFile;

    public StatsTracker(File backingFile) {
        this.backingFile = backingFile;
    }

    public void countCharacter(char character) {
        Map<Character, Integer> counts;
        try {
            counts = readCountsFromFile(backingFile);
        } catch (IOException e) {
            Timber.w(e,
                "Failed reading stats from file: %s",
                backingFile.getAbsolutePath());
            return;
        }

        // Bump the count
        Integer count = counts.get(character);
        if (count == null) {
            count = 0;
        }
        counts.put(character, count + 1);

        writeCountsToFile(counts);
    }

    public static Map<Character, Integer> readCountsFromFile(File countsFile) throws IOException {
        // FIXME: Write code here
    }

    private void writeCountsToFile(Map<Character, Integer> counts) {
        // FIXME: Write code here
    }
}

@@ -682,40 +682,138 @@
 .io.
-File;%0Aimport java.io.IOException
+BufferedOutputStream;%0Aimport java.io.File;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.PrintWriter
 ;%0Aim
@@ -1535,16 +1535,34 @@
  + 1);%0A%0A
+        try %7B%0A    
         
@@ -1588,16 +1588,184 @@
 ounts);%0A
+        %7D catch (IOException e) %7B%0A            Timber.w(e,%0A                %22Failed writing stats to file: %25s%22,%0A                backingFile.getAbsolutePath());%0A        %7D%0A
     %7D%0A%0A 
@@ -1974,43 +1974,774 @@
 ts) 
-%7B%0A        // FIXME: Write code here
+throws IOException %7B%0A        File tempFile = new File(backingFile.getAbsolutePath(), %22.tmp%22);%0A        try (FileOutputStream fileOutputStream = new FileOutputStream(tempFile);%0A             BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);%0A             PrintWriter out = new PrintWriter(bufferedOutputStream)) %7B%0A            for (Map.Entry%3CCharacter, Integer%3E entry: counts.entrySet()) %7B%0A                out.print(entry.getKey());%0A                out.print(%22: %22);%0A                out.println(entry.getValue());%0A            %7D%0A        %7D%0A%0A        if (!tempFile.renameTo(backingFile)) %7B%0A            throw new IOException(%0A                %22Rename failed: %22 + tempFile.getAbsolutePath() + %22-%3E%22 + backingFile.getAbsolutePath());%0A        %7D
 %0A   

Implement writing stats to file