// TestUpdateSiteStatus tests updating the up/down status of the site.
func TestUpdateSiteStatus(t *testing.T) {
	db, err := database.InitializeTestDB("")
	if err != nil {
		t.Fatal("Failed to create database:", err)
	}
	defer db.Close()

	// First create a site to update status.
	s := database.Site{SiteID: 1, 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)
	}

	// Update the status of the site to down
	err = s.UpdateSiteStatus(db, false)
	if err != nil {
		t.Fatal("Failed to update site status:", err)
	}

	//Get the saved site
	var updatedSite database.Site
	err = updatedSite.GetSite(db, s.SiteID)
	if err != nil {
		t.Fatal("Failed to retrieve updated site:", err)
	}

	if updatedSite.IsSiteUp != false {
		t.Errorf("Site status should be down.")
	}

	// Update the status of the site to up
	err = s.UpdateSiteStatus(db, true)
	if err != nil {
		t.Fatal("Failed to update site status:", err)
	}

	// Update the first ping time of the site.
	firstPingTime := time.Date(2015, time.November, 10, 23, 22, 22, 00, time.UTC)
	err = s.UpdateSiteFirstPing(db, firstPingTime)
	if err != nil {
		t.Fatal("Failed to update first ping time:", err)
	}

	err = updatedSite.GetSite(db, s.SiteID)
	if err != nil {
		t.Fatal("Failed to retrieve updated site:", err)
	}

	if updatedSite.IsSiteUp != true {
		t.Errorf("Site status should be up.")
	}

	if updatedSite.FirstPing != firstPingTime {
		t.Errorf("Site first ping time %s does not match input %s.", updatedSite.FirstPing, firstPingTime)
	}

}
// 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()
		}
	}
}