/*
* Function main - Setting up httprouter and REST API handlers (GET, POST, PUT, DELETE)
 */
func main() {
	// Instantiate a new router
	r := httprouter.New()

	// Controller is going to need a mongo session to use in the CRUD methods.
	// It would be connected to MongoLab.
	lc := controllers.NewLocationController(GetRemoteMGOSession())

	// Add handlers for REST webservices on 'location' resource
	// Get all location resources
	r.GET("/locations", lc.GetAllLocations)

	// Get a location resource identified by id
	r.GET("/locations/:id", lc.GetLocation)

	// Create a location resource
	r.POST("/locations", lc.CreateLocation)

	// Update a location resource identified by id
	r.PUT("/locations/:id", lc.UpdateLocation)

	// Delete a location resource identified by id
	r.DELETE("/locations/:id", lc.RemoveLocation)

	// Fire up the server
	http.ListenAndServe("localhost:4000", r)
}
func main() {
	mux := routes.New()

	lc := controllers.NewLocationController(getSession())
	uc := controllers.NewUberController(getSession())

	mux.Get("/locations/:location_id", lc.GetLocation)
	mux.Post("/locations", lc.AddLocation)
	mux.Del("/locations/:location_id", lc.DeleteLocation)
	mux.Put("/locations/:location_id", lc.UpdateLocation)

	mux.Post("/trips", uc.AddTrip)
	mux.Get("/trips/:trip_id", uc.GetTrip)
	mux.Put("/trips/:trip_id/request", uc.RequestTrip)
	http.Handle("/", mux)
	http.ListenAndServe(":8080", nil)
}
func TestGetAll(t *testing.T) {
	//Setup httprouter and handlers for REST APIs for location service
	Router := httprouter.New()
	lc := controllers.NewLocationController(GetRemoteMGOSession())

	Router.GET("/locations", lc.GetAllLocations)
	Router.GET("/locations/:id", lc.GetLocation)
	Router.POST("/locations", lc.CreateLocation)
	Router.PUT("/locations/:id", lc.UpdateLocation)
	Router.DELETE("/locations/:id", lc.RemoveLocation)

	//Test - GET all locations
	req, _ := http.NewRequest("GET", "/locations", nil)
	w := httptest.NewRecorder()
	Router.ServeHTTP(w, req)

	if !(w.Code == http.StatusOK) {
		t.Errorf("Failed to get response from server %s\n", w.Body.String())
		t.FailNow()
	}
}
func TestAllRESTAPIs(t *testing.T) {
	//Setup httprouter and handlers for REST APIs for location service
	Router := httprouter.New()
	lc := controllers.NewLocationController(GetRemoteMGOSession())

	Router.GET("/locations", lc.GetAllLocations)
	Router.GET("/locations/:id", lc.GetLocation)
	Router.POST("/locations", lc.CreateLocation)
	Router.PUT("/locations/:id", lc.UpdateLocation)
	Router.DELETE("/locations/:id", lc.RemoveLocation)

	//Test - POST a new location
	locationJson := `{"name": "Marshal Eriksen", "address": "1024", "city": "Sunnyvale", "state": "CA","zipcode": "94086"}`
	reader := strings.NewReader(locationJson) //Convert string to reader

	req, _ := http.NewRequest("POST", "/locations", reader)

	w := httptest.NewRecorder()
	Router.ServeHTTP(w, req)

	if !(w.Code == 201) {
		t.Errorf("Failed to get response from server %s\n", w.Body.String())
		t.Errorf("Error Code %s\n", w.Code)
		t.FailNow()
	}

	responseFromPost := model.Location{}
	json.NewDecoder(w.Body).Decode(&responseFromPost)
	oid := fmt.Sprintf("%x", string(responseFromPost.Id))

	s := []string{"/locations/", string(oid)}
	uri := strings.Join(s, "")
	req, _ = http.NewRequest("GET", uri, nil)
	w = httptest.NewRecorder()
	Router.ServeHTTP(w, req)

	if !(w.Code == http.StatusOK) {
		t.Errorf("Failed to get response from server %s\n", w.Body.String())
		t.Errorf("Error Code %s\n", w.Code)
		t.FailNow()
	}

	//Test - PUT the above location
	locationJson = `{"address": "978", "city": "New York", "state": "New York","zipcode": ""}`
	reader = strings.NewReader(locationJson) //Convert string to reader

	req, _ = http.NewRequest("PUT", uri, reader)

	w = httptest.NewRecorder()
	Router.ServeHTTP(w, req)

	if !(w.Code == 201) {
		t.Errorf("Failed to get response from server %s\n", w.Body.String())
		t.Errorf("Error Code %s\n", w.Code)
		t.FailNow()
	}

	//Test - DELETE the above location
	req, _ = http.NewRequest("DELETE", uri, nil)
	w = httptest.NewRecorder()
	Router.ServeHTTP(w, req)

	if !(w.Code == http.StatusOK) {
		t.Errorf("Failed to get response from server %s\n", w.Body.String())
		t.Errorf("Error Code %s\n", w.Code)
		t.FailNow()
	}

	//Test - GET and verify that the above location resource does not exist
	req, _ = http.NewRequest("GET", uri, nil)
	w = httptest.NewRecorder()
	Router.ServeHTTP(w, req)

	if !(w.Code == 404) {
		t.Errorf("Failed to get response from server %s\n", w.Body.String())
		t.Errorf("Error Code %s\n", w.Code)
		t.FailNow()
	}
}