package auth

import "fmt"

func compareErrors(err1, err2 RequestError) (success bool, field string, val1, val2 interface{}) {
	if err1.Slug != err2.Slug {
		return false, "Slug", err1.Slug, err2.Slug
	}
	if err1.Field != err2.Field {
		return false, "Field", err1.Field, err2.Field
	}
	if err1.Param != err2.Param {
		return false, "Param", err1.Param, err2.Param
	}
	if err1.Header != err2.Header {
		return false, "Header", err1.Header, err2.Header
	}
	return true, "", nil, nil
}

func compareResponses(resp1, resp2 Response) (success bool, field string, val1, val2 interface{}) {
	if len(resp1.Errors) != len(resp2.Errors) {
		return false, "Errors", resp1.Errors, resp2.Errors
	}
	if len(resp1.Logins) != len(resp2.Logins) {
		return false, "Logins", resp1.Logins, resp2.Logins
	}
	if len(resp1.Profiles) != len(resp2.Profiles) {
		return false, "Profiles", resp1.Profiles, resp2.Profiles
	}
	if len(resp1.Clients) != len(resp2.Clients) {
		return false, "Clients", resp1.Clients, resp2.Clients
	}
	if len(resp1.Endpoints) != len(resp2.Endpoints) {
		return false, "Endpoints", resp1.Endpoints, resp2.Endpoints
	}
	for pos := range resp1.Errors {
		success, field, val1, val2 = compareErrors(resp1.Errors[pos], resp2.Errors[pos])
		if !success {
			field = fmt.Sprintf("Error %d %s", pos, field)
			return
		}
	}
	for pos := range resp1.Logins {
		success, field, val1, val2 = compareLogins(resp1.Logins[pos], resp2.Logins[pos])
		if !success {
			field = fmt.Sprintf("Login %d %s", pos, field)
			return
		}
	}
	for pos := range resp1.Profiles {
		success, field, val1, val2 = compareProfiles(resp1.Profiles[pos], resp2.Profiles[pos])
		if !success {
			field = fmt.Sprintf("Profile %d %s", pos, field)
			return
		}
	}
	for pos := range resp1.Clients {
		success, field, val1, val2 = compareClients(resp1.Clients[pos], resp2.Clients[pos])
		if !success {
			field = fmt.Sprintf("Client %d %s", pos, field)
			return
		}
	}
	for pos := range resp1.Endpoints {
		success, field, val1, val2 = compareEndpoints(resp1.Endpoints[pos], resp2.Endpoints[pos])
		if !success {
			field = fmt.Sprintf("Endpoint %d %s", pos, field)
			return
		}
	}
	return true, "", nil, nil
}

func fillInServerGenerated(expectation, result Response) {
	if len(expectation.Profiles) > 0 {
		for pos, profile := range expectation.Profiles {
			profile.ID = result.Profiles[pos].ID
			profile.Created = result.Profiles[pos].Created
			profile.LastSeen = result.Profiles[pos].LastSeen
			expectation.Profiles[pos] = profile
		}
	}
	if len(expectation.Logins) > 0 {
		for pos, login := range expectation.Logins {
			login.ProfileID = result.Logins[pos].ProfileID
			login.Created = result.Logins[pos].Created
			login.LastUsed = result.Logins[pos].LastUsed
			expectation.Logins[pos] = login
		}
	}
	if len(expectation.Clients) > 0 {
		for pos, client := range expectation.Clients {
			client.ID = result.Clients[pos].ID
			client.Secret = result.Clients[pos].Secret
			client.OwnerID = result.Clients[pos].OwnerID
			expectation.Clients[pos] = client
		}
	}
	if len(expectation.Endpoints) > 0 {
		for pos, endpoint := range expectation.Endpoints {
			endpoint.ID = result.Endpoints[pos].ID
			endpoint.ClientID = result.Endpoints[pos].ClientID
			endpoint.Added = result.Endpoints[pos].Added
			expectation.Endpoints[pos] = endpoint
		}
	}
}
