package tenant

/*
	func filterAndSortUsers(ctx context.Context, users []*central.User, emails []string, sortBy, order string) []*central.User {
		if len(users) == 0 {
			return users
		}
		// filter email
		users = filterUsers(users, emails)

		if len(users) == 0 {
			return users
		}

		if sortBy == "" {
			sortBy = "email"
		}
		if order == "" {
			order = "asc"
		}
		// sort users
		return sortUsers(ctx, users, sortBy, order)
	}

	func filterUsers(users []*central.User, emails []string) []*central.User {
		if len(emails) == 0 {
			return users
		}
		var retUsers []*central.User
		emailMap := map[string]struct{}{}
		for _, email := range emails {
			emailMap[email] = struct{}{}
		}
		for _, u := range users {
			if _, ok := emailMap[u.Email]; ok {
				retUsers = append(retUsers, u)
			}
		}
		return retUsers
	}

	func sortUsers(ctx context.Context, users []*central.User, sortBy, order string) []*central.User {
		// sort users by id、email、name、status、invite_time、role、rbac_role、login_time
		sortBy = strings.ToLower(sortBy)
		order = strings.ToLower(order)

		f := sortFuncMap[sortBy]
		if f == nil {
			tilog.Warn(ctx, fmt.Sprintf("field [%s] not support sort", sortBy))
			return users
		}
		return f(users, sortBy, order)
	}

var sortFuncMap = map[string]func(users []*central.User, sortBy, order string) []*central.User{
	"id":          intSort,
	"email":       stringSort,
	"name":        stringSort,
	"status":      stringSort,
	"invite_time": inviteTimeSort,
	"role":        stringSort,
	"rbac_role":   stringSort,
	"login_time":  loginTimeSort,
}

func intSort(users []*central.User, sortBy, order string) []*central.User {
	sort.SliceStable(users, func(i, j int) bool {
		intI := users[i].Id
		intJ := users[j].Id

		if order == defaultOrder {
			return intI < intJ
		}
		return intI > intJ
	})
	return users
}

func stringSort(users []*central.User, sortBy, order string) []*central.User {
	sort.SliceStable(users, func(i, j int) bool {
		uI := users[i]
		uJ := users[j]

		strI := getStrFieldValV2(uI, sortBy)
		strJ := getStrFieldValV2(uJ, sortBy)

		compare := strings.Compare(strI, strJ)

		if order == defaultOrder {
			return compare < 0
		}
		return compare > 0
	})
	return users
}

func getStrFieldValV2(user *central.User, sortBy string) string {
	if sortBy == "email" {
		return user.Email
	}
	if sortBy == "name" {
		return user.Name
	}
	if sortBy == "status" {
		return user.Status
	}
	if sortBy == "role" {
		return user.Role
	}
	if sortBy == "rbac_role" {
		return user.RbacRole
	}
	return ""
}
*/
/*func getStrFieldVal(user *central.User, sortBy string) string {
	u := *user
	t := reflect.TypeOf(u)
	v := reflect.ValueOf(u)

	var fName = ""
	for i := 0; i < t.NumField(); i++ {
		f := t.Field(i)
		tag := f.Tag
		jsonTag := tag.Get("json")
		if strings.HasPrefix(jsonTag, sortBy) {
			fName = f.Name
			break
		}
	}

	if fName != "" {
		return v.FieldByName(fName).String()
	}
	return ""
}


func loginTimeSort(users []*central.User, sortBy, order string) []*central.User {
	sort.SliceStable(users, func(i, j int) bool {
		uI := users[i]
		uJ := users[j]

		timeI := uI.LoginTime
		timeJ := uJ.LoginTime

		return comparePbTimestamp(timeI, timeJ, order)
	})
	return users
}

func inviteTimeSort(users []*central.User, sortBy, order string) []*central.User {
	sort.SliceStable(users, func(i, j int) bool {
		uI := users[i]
		uJ := users[j]

		timeI := uI.InviteTime
		timeJ := uJ.InviteTime

		return comparePbTimestamp(timeI, timeJ, order)
	})
	return users
}

func comparePbTimestamp(timeI, timeJ *timestamppb.Timestamp, order string) bool {
	if timeI == nil || timeJ == nil {
		return true
	}
	if timeI == nil {
		return false
	}
	if timeJ == nil {
		return true
	}

	utcI := timeI.AsTime().UTC()
	utcJ := timeJ.AsTime().UTC()

	if order == defaultOrder {
		return utcI.Before(utcJ)
	}
	return utcI.After(utcJ)
}
*/
