package account

import (
	"net/http"
	"regexp"

	"code.nonshy.com/nonshy/website/pkg/config"
	"code.nonshy.com/nonshy/website/pkg/models"
	"code.nonshy.com/nonshy/website/pkg/session"
	"code.nonshy.com/nonshy/website/pkg/templates"
)

var UserFriendsRegexp = regexp.MustCompile(`^/friends/u/([^@]+?)$`)

// User friends page (/friends/u/username)
func UserFriends() http.HandlerFunc {
	tmpl := templates.Must("account/friends.html")
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// Parse the username out of the URL parameters.
		var username string
		m := UserFriendsRegexp.FindStringSubmatch(r.URL.Path)
		if m != nil {
			username = m[1]
		}

		// Find this user.
		user, err := models.FindUser(username)
		if err != nil {
			templates.NotFoundPage(w, r)
			return
		}

		// Get the viewer.
		currentUser, err := session.CurrentUser(r)
		if err != nil {
			session.FlashError(w, r, "Unexpected error: could not get currentUser.")
			templates.Redirect(w, "/")
			return
		}

		var isSelf = currentUser.ID == user.ID

		// Banned or disabled? Only admin can view then.
		if user.Status != models.UserStatusActive && !currentUser.IsAdmin {
			templates.NotFoundPage(w, r)
			return
		}

		// Is either one blocking?
		if models.IsBlocking(currentUser.ID, user.ID) && !currentUser.IsAdmin {
			templates.NotFoundPage(w, r)
			return
		}

		// Get their friends.
		pager := &models.Pagination{
			PerPage: config.PageSizeFriends,
			Sort:    "updated_at desc",
		}
		pager.ParsePage(r)
		friends, err := models.PaginateOtherUserFriends(currentUser, user, pager)
		if err != nil {
			session.FlashError(w, r, "Couldn't paginate friends: %s", err)
			templates.Redirect(w, "/")
			return
		}

		var vars = map[string]interface{}{
			"User":        user,
			"IsSelf":      isSelf,
			"PhotoCount":  models.CountPhotosICanSee(user, currentUser),
			"NoteCount":   models.CountNotesAboutUser(currentUser, user),
			"FriendCount": models.CountFriends(user.ID),
			"Friends":     friends,
			"Pager":       pager,

			// Map our friendships to these users.
			"FriendMap": models.MapFriends(currentUser, friends),
		}
		if err := tmpl.Execute(w, r, vars); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	})
}
