//
//  AuthorizationViewController.m
//  HotPocket (macOS)
//
//  Created by Tomek Wójcik on 20/09/2025.
//

#import "AuthorizationViewController.h"

#import "AppDelegate.h"
#import "AuthorizationProgressViewController.h"
#import "HPAuthFlow.h"
#import "ReplaceAnimator.h"

@interface AuthorizationViewController (AuthorizationViewControllerPrivate)

#pragma mark - Private interface

@end

@implementation AuthorizationViewController

#pragma mark - View lifecycle

-(void)viewDidLoad {
    [super viewDidLoad];
    self.baseURL = nil;
    self.authorizationSessionToken = nil;
}

#pragma mark - Actions

-(IBAction)doStartAuthorizationFlow:(id)sender {
    AppDelegate *appDelegate = [[NSApplication sharedApplication] delegate];
    appDelegate.authFlow.baseURL = [NSURL URLWithString:self.baseURL];
    
    NSURL *authURL = [appDelegate.authFlow start];
    if (authURL == nil) {
        NSBeep();
        return;
    }
    
    AuthorizationProgressViewController *authProgressViewController = [self.storyboard instantiateControllerWithIdentifier:@"AuthorizationProgressViewController"];
    authProgressViewController.authorizationURL = authURL;
    [self presentViewController:authProgressViewController animator:[[ReplaceAnimator alloc] init]];
}

# pragma mark - ASWebAuthenticationPresentationContextProviding implementation

-(ASPresentationAnchor)presentationAnchorForWebAuthenticationSession:(ASWebAuthenticationSession *)session {
    return self.view.window;
}

@end
