 import Foundation
 import MapboxDirections
 
-class Maneuver {
-    public let modifier: String?
-    public let type: String
-    public let exit: Int?
-    
-    internal init(modifier: String?, type: String, exit: Int?) {
-        self.modifier = modifier
-        self.type = type
-        self.exit = exit
-    }
-    
-    internal convenience init(json: [ String: AnyObject ]) {
-        self.init(
-            modifier: json["modifier"] as? String,
-            type: json["type"] as! String,
-            exit: json["exit"] as? Int
-        )
-    }
-}
-
-class Step {
-    public let maneuver: Maneuver
-    public let rotary_name: String?
+class OSRMStep {
+    public let rotaryName: String?
     public let name: String?
     public let destinations: String?
     public let mode: String?
+    public let maneuverModifier: String?
+    public let maneuverType: String
+    public let maneuverExit: Int?
     
-    internal init(maneuver: Maneuver, rotary_name: String?, name: String?, destinations: String?, mode: String?) {
-        self.maneuver = maneuver
-        self.rotary_name = rotary_name
+    internal init(rotaryName: String?, name: String?, destinations: String?, mode: String?, maneuverModifier: String?, maneuverType: String, maneuverExit: Int?) {
+        self.rotaryName = rotaryName
         self.name = name
         self.destinations = destinations
         self.mode = mode
+        self.maneuverModifier = maneuverModifier
+        self.maneuverType = maneuverType
+        self.maneuverExit = maneuverExit
     }
-    
+
     internal convenience init(json: [ String: AnyObject ]) {
+        let maneuver = json["maneuver"] as! [ String: AnyObject ]
+
         self.init(
-            maneuver: Maneuver(json: json["maneuver"] as! [ String: AnyObject ]),
-            rotary_name: json["type"] as? String,
+            rotaryName: json["rotary_name"] as? String,
             name: json["name"] as? String,
             destinations: json["destinations"] as? String,
-            mode: json["mode"] as? String
+            mode: json["mode"] as? String,
+            maneuverModifier: maneuver["modifier"] as? String,
+            maneuverType: maneuver["type"] as! String,
+            maneuverExit: maneuver["exit"] as? Int
         )
     }
-    
+
     // TODO
     //    internal convenience init(routeStep: RouteStep) {
     //        self.init()

        // NOOP if they don't exist
        let nthWaypoint = "" // TODO, add correct waypoint counting
        let destination = (step.destinations ?? "").components(separatedBy: ",")[0]
        let exit = step.maneuver.exit ?? 1 // TODO: ordinalize
        let modifierConstant =
            ((self.instructions.object(forKey: "constants") as! NSDictionary)
            .object(forKey: "modifier") as! NSDictionary)

            .replacingOccurrences(of: "{way_name}", with: wayName)
            .replacingOccurrences(of: "{destination}", with: destination)
            .replacingOccurrences(of: "{exit_number}", with: String(exit))
            .replacingOccurrences(of: "{rotary_name}", with: step.rotaryName ?? "")
            .replacingOccurrences(of: "{lane_instruction}", with: "") // TODO: implement correct lane instructions
            .replacingOccurrences(of: "{modifier}", with: modifierConstant)
            .replacingOccurrences(of: "{direction}", with: "") // TODO: integrate actual direction
