package com.opentrain.app.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by noam on 06/07/15.
 */
public class MainModel {

    private static MainModel mInstance;

    public static MainModel getInstance() {
        if (mInstance == null) {
            reset();
        }
        return mInstance;
    }

    public static void reset() {
        mInstance = new MainModel();
    }

    // Map of BSSIDs to station names (future: change to station_id):
    private Map<String, String> bssidMap;
    // List of all the scanned stations at this trip:
    private List<Station> scannedStationList;
    private boolean inStation;
    // Test trip
    private ArrayList<ArrayList<WifiScanResultItem>> mockResultsList;
    // List of all train stations:
    private ArrayList<String> mStationList;

    private MainModel() {
        scannedStationList = new ArrayList<>();
        bssidMap = new HashMap<>();
        mockResultsList = new ArrayList<>();
        mStationList = new ArrayList<>();
    }

    public Map<String, String> getBssidMap() {
        return bssidMap;
    }

    public void setBssidMap(Map<String, String> bssidMap) {
        this.bssidMap = bssidMap;
    }

    public void updateMap(Map<String, String> results) {
        for (Map.Entry<String, String> serverEntry : results.entrySet()) {
            bssidMap.put(serverEntry.getKey(), serverEntry.getValue());
        }
    }

    public boolean isInStation() {
        return inStation;
    }

    public void setInStation(boolean inStation) {
        if (inStation && scannedStationList.isEmpty()) {
            throw new IllegalStateException("Cannot set isStation=true with empty station list.");
        }
        this.inStation = inStation;
    }

    // Returns null if not in station.
    public String getCurrentStationId() {
        if (inStation) {
            return scannedStationList.get(scannedStationList.size() - 1).getName();
        } else {
            return null;
        }
    }

    public List<Station> getScannedStationList() {
        return scannedStationList;
    }

    public void addStationAndSetInStation(Station station) {
        scannedStationList.add(station);
        inStation = true;
    }

    public void setScannedStationList(List<Station> scannedStationList) {
        this.scannedStationList = scannedStationList;
    }

    // Returns the last station's last seen time. If there is no last station, returns null.
    public Long getStationLastSeenTimeUnixMs() {
        if (scannedStationList.isEmpty()) {
            return null;
        }
        return scannedStationList.get(scannedStationList.size() - 1).lastSeenUnixTimeMs;
    }

    // Set the last station exit time to the last seen time. If the station list is empty, do nothing.
    public void setLastStationExitTimeIfItExists() {
        if (!scannedStationList.isEmpty()) {
            Station lastStation = scannedStationList.get(scannedStationList.size() - 1);
            lastStation.exitUnixTimeMs = lastStation.lastSeenUnixTimeMs;
        }
    }

    public void clearScannedItems() {
        scannedStationList.clear();
    }

    public ArrayList<ArrayList<WifiScanResultItem>> getMockResultsList() {
        return mockResultsList;
    }

    public void setMockResultsList(ArrayList<ArrayList<WifiScanResultItem>> mockResultsList) {
        this.mockResultsList = mockResultsList;
    }

    public void setStationList(ArrayList<String> stationList) {
        if (stationList != null && stationList.size() > 0) {
            this.mStationList = stationList;
        }
    }

    public ArrayList<String> getStationList() {
        return mStationList;
    }

    // Aligns @scannedStations to @gtfsStations for display purposes in the UI
    // Assume the order of gtfs stations as of scanned
    public List<MatchedStation> alignScannedTripToGtfsTrip(List<Station> scannedStations, List<GtfsStation> gtfsStations) {

        List<MatchedStation> result = new ArrayList<MatchedStation>();

        int gtfsIndex = 0;
        for (Station scanned : scannedStations) {
            while ((gtfsIndex < gtfsStations.size()) && (scanned.getId() != gtfsStations.get(gtfsIndex).id)) {
                gtfsIndex++;
            }
            if (gtfsIndex < gtfsStations.size()) {
                // Found a match
                result.add(new MatchedStation(scanned, gtfsStations.get(gtfsIndex)));
            } else {
                // No match - but still display the scanned station data
                result.add(new MatchedStation(scanned, null));
                gtfsIndex = 0; // Start from the beginning - there is some problem.
            }
        }

        return result;
    }
}

@@ -3809,14 +3809,27 @@
  // 
-A
+This method a
 ssume
+s
  the
@@ -3842,22 +3842,22 @@
  of 
+@
 gtfs
- s
+S
 tations 
 as o
@@ -3856,21 +3856,40 @@
 ons 
-as of scanned
+is the same as @scannedStations.
 %0A   
@@ -4009,17 +4009,16 @@
 ions) %7B%0A
-%0A
         
@@ -4080,17 +4080,16 @@
 ion%3E();%0A
-%0A
         
@@ -4214,16 +4214,17 @@
 ()) && (
+!
 scanned.
@@ -4230,20 +4230,24 @@
 .getId()
- != 
+.equals(
 gtfsStat
@@ -4270,16 +4270,17 @@
 ex).id))
+)
  %7B%0A     
@@ -4581,16 +4581,170 @@
 on data%0A
+                // TODO - rewrite this section: Perhaps we should add another int lastMatchedGtfsIndex and set gtfsIndex to it in this case instead of 0.%0A
         
@@ -4888,32 +4888,32 @@
 .%0A            %7D%0A
+
         %7D%0A%0A     
@@ -4902,25 +4902,24 @@
 %7D%0A        %7D%0A
-%0A
         retu

Update MainModel.java