// ping does the actual pinging of the site and calls the notifications
func ping(s database.Site, db *sql.DB, requestURL URLRequester,
	sendEmail notifier.EmailSender, sendSms notifier.SmsSender, wg *sync.WaitGroup, stop chan struct{}) {
	defer wg.Done()
	// Initialize the previous state of site to the database value. On site creation will initialize to true.
	siteWasUp := s.IsSiteUp
	var statusChange bool
	var partialDetails string
	var partialSubject string
	for {
		// initialize statusChange to false and only notify on change of siteWasUp status
		statusChange = false
		// Check for a quit signal to stop the pinging
		select {
		case <-stop:
			log.Println("Stopping ", s.Name)
			return
		case <-time.After(time.Duration(s.PingIntervalSeconds) * time.Second):
			// Do nothing
		}
		if !s.IsActive {
			log.Println(s.Name, "Paused")
			continue
		}
		bodyContent, statusCode, responseTime, err := requestURL(s.URL, s.TimeoutSeconds)
		log.Println(s.Name, "Pinged")
		// Setup ping information for recording.
		p := database.Ping{SiteID: s.SiteID, TimeRequest: time.Now()}
		if err != nil {
			// Check if the error is due to the Internet not being Accessible
			if _, ok := err.(InternetAccessError); ok {
				log.Println(s.Name, "Unable to determine site status -", err)
				continue
			}
			log.Println(s.Name, "Error", err)
			if siteWasUp {
				statusChange = true
				partialSubject = "Site is Down"
				partialDetails = "Site is down, Error is " + err.Error()
			}
			siteWasUp = false

		} else if statusCode < 200 || statusCode > 299 { // Check if the status code is in the 2xx range.
			log.Println(s.Name, "Error - HTTP Status Code is", statusCode)
			if siteWasUp {
				statusChange = true
				partialSubject = "Site is Down"
				partialDetails = "Site is down, HTTP Status Code is " + strconv.Itoa(statusCode) + "."
			}
			siteWasUp = false

		} else {
			siteUp := true
			// if the site settings require check the content.
			if siteUp && s.ContentExpected != "" && !strings.Contains(bodyContent, s.ContentExpected) {
				siteUp = false
				log.Println(s.Name, "Error - required body content missing: ", s.ContentExpected)
				if siteWasUp {
					statusChange = true
					partialSubject = "Site is Down"
					partialDetails = "Site is Down, required body content missing: " + s.ContentExpected + "."
				}
			}
			if siteUp && s.ContentUnexpected != "" && strings.Contains(bodyContent, s.ContentUnexpected) {
				siteUp = false
				log.Println(s.Name, "Error - body content content has excluded content: ", s.ContentUnexpected)
				if siteWasUp {
					statusChange = true
					partialSubject = "Site is Down"
					partialDetails = "Site is Down, body content content has excluded content: " + s.ContentUnexpected + "."
				}
			}
			if siteUp && !siteWasUp {
				statusChange = true
				partialSubject = "Site is Up"
				partialDetails = fmt.Sprintf("Site is now up, response time was %v.", responseTime)
				siteWasUp = true
			}
			siteWasUp = siteUp
		}
		// Save the ping details
		p.Duration = int(responseTime.Nanoseconds() / 1e6)
		p.HTTPStatusCode = statusCode
		p.SiteDown = !siteWasUp
		// Save ping to db.
		err = p.CreatePing(db)
		if err != nil {
			log.Println("Error saving to ping to db:", err)
		}
		// Do the notifications if applicable
		if statusChange {
			// Update the site Status
			err = s.UpdateSiteStatus(db, siteWasUp)
			if err != nil {
				log.Println("Error updating site status:", err)
			}
			// Do the notifications if applicable
			subject := s.Name + ": " + partialSubject
			details := s.Name + " at " + s.URL + ": " + partialDetails
			log.Println("Will notify status change for", s.Name+":", details)

			n := notifier.NewNotifier(s, details, subject, sendEmail, sendSms)
			n.Notify()
		}
	}
}
// TestCreatePings tests creating the ping records for a given site.
func TestCreatePings(t *testing.T) {
	var err error
	db, err := database.InitializeTestDB("")
	defer db.Close()
	if err != nil {
		t.Fatal("Failed to create database:", err)
	}

	// First create a site to associate with the pings.
	s := database.Site{Name: "Test", IsActive: true, URL: "http://www.google.com", PingIntervalSeconds: 60, TimeoutSeconds: 30}
	err = s.CreateSite(db)
	if err != nil {
		t.Fatal("Failed to create new site:", err)
	}

	// Create a ping result
	p1 := database.Ping{SiteID: s.SiteID, TimeRequest: time.Date(2015, time.November, 10, 23, 22, 22, 00, time.UTC),
		Duration: 280, HTTPStatusCode: 200, SiteDown: false}
	err = p1.CreatePing(db)
	if err != nil {
		t.Fatal("Failed to create new ping:", err)
	}

	// Create a second ping result
	p2 := database.Ping{SiteID: s.SiteID, TimeRequest: time.Date(2015, time.November, 10, 23, 22, 20, 00, time.UTC),
		Duration: 290, HTTPStatusCode: 200, SiteDown: true}
	err = p2.CreatePing(db)
	if err != nil {
		t.Fatal("Failed to create new ping:", err)
	}

	//Get the saved Ping
	var saved database.Site
	err = saved.GetSitePings(db, s.SiteID, time.Date(2015, time.November, 10, 23, 00, 00, 00, time.UTC),
		time.Date(2015, time.November, 10, 23, 59, 00, 00, time.UTC))
	if err != nil {
		t.Fatal("Failed to retrieve saved pings:", err)
	}

	// Verify the first ping was Loaded with proper attibutes and sorted last.
	if !reflect.DeepEqual(p1, saved.Pings[1]) {
		t.Error("First saved ping not equal to input:\n", saved.Pings[1], p1)
	}

	// Verify the second ping was Loaded with proper attributes and sorted first.
	if !reflect.DeepEqual(p2, saved.Pings[0]) {
		t.Error("Second saved ping not equal to input:\n", saved.Pings[0], p2)
	}

	// Verify that the site reflects the last ping time.
	err = saved.GetSite(db, s.SiteID)
	if err != nil {
		t.Fatal("Failed to retrieve site:", err)
	}

	if saved.LastPing != p2.TimeRequest {
		t.Error("Last Ping on site does not match input:\n", saved.LastPing, p1.TimeRequest)
	}

	//Get the first ping for the site.
	firstping, err := s.GetFirstPing(db)
	if err != nil {
		t.Fatal("Failed to retrieve first ping for the site:", err)
	}
	if firstping != p2.TimeRequest {
		t.Error("First Ping on site does not match input:\n", firstping, p2.TimeRequest)
	}

	// Create a third ping with conflicting times should error.
	p3 := database.Ping{SiteID: s.SiteID, TimeRequest: time.Date(2015, time.November, 10, 23, 22, 20, 00, time.UTC),
		Duration: 300, HTTPStatusCode: 200, SiteDown: false}
	err = p3.CreatePing(db)
	if err == nil {
		t.Fatal("Conflicting pings should throw error.")
	}
}