// TestNewNotifier tests building the pinger object.
func TestNewNotifier(t *testing.T) {
	site := getTestSite()
	n := notifier.NewNotifier(site, "Site 1 responding OK", "Site 1 Up", notifier.SendEmailMock, notifier.SendSmsMock)
	// Verify the first contact was Loaded with proper attributes and sorted last.
	if !reflect.DeepEqual(site.Contacts, n.Site.Contacts) {
		t.Error("Incoming site contacts are not the same as the notifier contacts:\n", site.Contacts, n.Site.Contacts)
	}
}
// TestNotifyError tests calling the Notifications with errors on each send method.
func TestNotifyError(t *testing.T) {
	site := getTestSite()
	n := notifier.NewNotifier(site, "Site 1 responding OK", "Site 1 Up", notifier.SendEmailErrorMock, notifier.SendSmsErrorMock)
	n.Notify()

	results, err := pinger.GetLogContent()
	if err != nil {
		t.Fatal("Failed to get log results.", err)
	}

	if !strings.Contains(results, "Error sending SMS: Error - no response from server.") {
		t.Error("Failed to report error in SMS send to Jack Contact.")
	}

	if !strings.Contains(results, "Error sending email: Error - no response from server.") {
		t.Error("Failed to report error in email send to Joe Contact.")
	}
}
// TestNotify tests calling the Notifications successfully.
func TestNotify(t *testing.T) {
	pinger.CreatePingerLog("", true)
	site := getTestSite()
	n := notifier.NewNotifier(site, "Site 1 responding OK", "Site 1 Up", notifier.SendEmailMock, notifier.SendSmsMock)
	n.Notify()

	results, err := pinger.GetLogContent()
	if err != nil {
		t.Fatal("Failed to get log results.", err)
	}

	if !strings.Contains(results, "Sending notifications for Jack Contact Site 1 Up Site 1 responding OK") {
		t.Error("Failed to report successful send to Jack Contact.")
	}

	if !strings.Contains(results, "Sending notifications for Joe Contact Site 1 Up Site 1 responding OK") {
		t.Error("Failed to report successful send to Joe Contact.")
	}
}
// 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()
		}
	}
}