/*
 * Copyright (C) 2012 Gregory S. Meiste  <http://gregmeiste.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.meiste.greg.ptw;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import android.content.Context;

import com.google.gson.Gson;

public final class Races {

    private static final String FILENAME = "schedule";
    private static final Object sRacesSync = new Object();
    private static Race[] sRaces;

    public static Race[] get(Context context) {
        synchronized (sRacesSync) {
            if (sRaces == null) {
                Util.log("Populating race array");

                sRaces = getLatest(context);
                if (sRaces == null) {
                    // User doesn't have updated schedule, so use included schedule
                    sRaces = getIncluded(context);
                }
            }
        }
        return sRaces;
    }

    public static void update(Context context, String json) {
        synchronized (sRacesSync) {
            try {
                FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
                fos.write(json.getBytes());
                fos.close();
                sRaces = null;
            } catch (Exception e) {
                Util.log("Failed to save update to schedule");
            }
        }
    }

    private static Race[] getIncluded(Context context) {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(context.getAssets().open(FILENAME)));
            String line;
            StringBuilder buffer = new StringBuilder();
            while ((line = in.readLine()) != null)
                buffer.append(line).append('\n');
            in.close();
            return new Gson().fromJson(buffer.toString(), Race[].class);
        } catch (IOException e) {
            Util.log("Unable to open included schedule: " + e);
        }

        return null;
    }

    private static Race[] getLatest(Context context) {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(context.openFileInput(FILENAME)));
            String line;
            StringBuilder buffer = new StringBuilder();
            while ((line = in.readLine()) != null)
                buffer.append(line).append('\n');
            in.close();
            return new Gson().fromJson(buffer.toString(), Race[].class);
        } catch (Exception e) {
            Util.log("No updated schedule found");
        }
        return null;
    }
}

@@ -1094,32 +1094,82 @@
 ext context) %7B%0D%0A
+        Race%5B%5D lRaces;%0D%0A        Race%5B%5D iRaces;%0D%0A%0D%0A
         synchron
@@ -1286,33 +1286,33 @@
                 
-s
+l
 Races = getLates
@@ -1316,32 +1316,80 @@
 test(context);%0D%0A
+                iRaces = getIncluded(context);%0D%0A
                 
@@ -1384,33 +1384,34 @@
             if (
-s
+(l
 Races == null) %7B
@@ -1400,32 +1400,57 @@
 (lRaces == null)
+ %7C%7C (lRaces.length %3C= 0))
  %7B%0D%0A            
@@ -1551,36 +1551,444 @@
 Races = 
-getIncluded(context)
+iRaces;%0D%0A                %7D else if (lRaces%5BlRaces.length-1%5D.getStartTimestamp() %3C iRaces%5B0%5D.getStartTimestamp()) %7B%0D%0A                    // Included schedule is newer than updated schedule%0D%0A                    sRaces = iRaces;%0D%0A                    Util.log(%22Removing downloaded schedule because included is newer%22);%0D%0A                    context.deleteFile(FILENAME);%0D%0A                %7D else %7B%0D%0A                    sRaces = lRaces
 ;%0D%0A     

Add check if included schedule is newer than last downloaded