//
//  GSEndpoint.h
//  GSEndpoint
//
//  Created by Florian Pircher on 03.02.22.
//  Copyright © 2022 schriftgestaltung.de. All rights reserved.
//

NS_ASSUME_NONNULL_BEGIN

@interface GSEndpoint : NSObject
/// The address of the endpoint URL excluding the user and password components.
@property (strong, nonatomic) NSString *address;
/// The user of the endpoint, or an empty string, if none.
@property (strong, nonatomic) NSString *user;
/// The password of the endpoint, or an empty string, if none.
@property (strong, nonatomic) NSString *password;
/// A URL representation suitable for display. Excludes the URL scheme, user, and password components.
@property (readonly) NSString *displayURL;
/// Whether the endpoint represents a valid URL.
@property (readonly, getter=isValid) BOOL valid;

- (instancetype)initWithAddress:(NSString *)address user:(nullable NSString *)user password:(nullable NSString *)password;
- (instancetype)initWithURL:(NSURL *)url;

/// If user or password components are included in the address property, they are removed from the address value and instead overwrite the user or password properties.
- (void)normalize;
/// Writes the password to the Keychain. Returns `NO` if accessing the Keychain failed, otherwise `YES`.
- (void)writeSecrets;
/// Reads the password from the Keychain. Returns `NO` if accessing the Keychain failed, otherwise `YES`.
- (void)readSecrets;
/// Deletes the associated Keychain item. Returns `NO` if accessing the Keychain failed, otherwise `YES`.
- (void)deleteSecrets;

- (nullable NSURLCredential *)credentialWithPersistence:(NSURLCredentialPersistence)persistence;

+ (nullable NSURLCredential *)credentialForURL:(NSURL *)URL persistence:(NSURLCredentialPersistence)persistence;
+ (nullable NSURLCredential *)promptCredentialForURL:(NSURL *)URL isRetry:(BOOL)isRetry persistence:(NSURLCredentialPersistence)persistence;
@end

NS_ASSUME_NONNULL_END
