 import Foundation
 
 
+internal class LocationFixes {
+    internal var builders: Array<Pogoprotos.Networking.Envelopes.Signature.LocationFix.Builder> =  []
+    internal var timestamp:UInt64 = 0
+    internal var lastTimesnap:UInt64 = 0
+    internal var count: Int = 3
+}
+
 internal class LocationFix {
-    internal var timestamp:UInt64 = 0
     private let api: PGoApiRequest
     
     internal init(api: PGoApiRequest) {

        self.update()
    }
    
    private func start() -> UInt64 {
        let msSinceLastLocationFix = UInt64.random(200, max: 300)
        self.api.Settings.LocationFixes = [generate(UInt64.random(500, max: 750)),
                                           generate(UInt64.random(350, max: 450)),
                                           generate(msSinceLastLocationFix)]
        return msSinceLastLocationFix
    }
    
    private func update() {
        var lastLocFix:UInt64 = 0
        if self.api.Settings.LocationFixes.count == 0 {
            lastLocFix = start()
        } else {
            if self.api.Settings.LocationFixes[0].timestampSnapshot + 1000 < self.api.getTimestamp() {
                self.api.Settings.LocationFixes[1].timestampSnapshot += self.api.getTimestampSinceStart()
                self.api.Settings.LocationFixes[2].timestampSnapshot += self.api.getTimestampSinceStart()
                lastLocFix = UInt64.random(200, max: 300)
                self.api.Settings.LocationFixes.removeFirst()
                self.api.Settings.LocationFixes.append(generate(lastLocFix))
            } else {
                self.api.Settings.LocationFixes[0].timestampSnapshot += self.api.getTimestampSinceStart()
                self.api.Settings.LocationFixes[1].timestampSnapshot += self.api.getTimestampSinceStart()
                self.api.Settings.LocationFixes[2].timestampSnapshot += self.api.getTimestampSinceStart()
                lastLocFix = self.api.Settings.LocationFixes.last!.timestampSnapshot
            }
        }
        self.timestamp = lastLocFix
    }
    
    private func generate(timeStamp: UInt64) -> Pogoprotos.Networking.Envelopes.Signature.LocationFix.Builder {
        let locFix = Pogoprotos.Networking.Envelopes.Signature.LocationFix.Builder()
        locFix.provider = "fused"
        locFix.timestampSnapshot = timeStamp
        locFix.latitude = Float(self.api.Location.lat) + Float.random(min: -0.01, max: 0.01)
        locFix.longitude = Float(self.api.Location.long) + Float.random(min: -0.01, max: 0.01)
        locFix.altitude = Float(self.api.Location.alt) + Float.random(min: -0.01, max: 0.01)

    }
    
    private func getLocationFixes() -> [Pogoprotos.Networking.Envelopes.Signature.LocationFix] {
        var fixes: [Pogoprotos.Networking.Envelopes.Signature.LocationFix] = []
        for i in 0..<self.api.locationFix.count {
            fixes.append(try! self.api.locationFix.builders[i].build())
        }
        return fixes
    }
    
    internal func build() -> Pogoprotos.Networking.Envelopes.RequestEnvelope.PlatformRequest {
