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

#import "AuthorizationViewController.h"

#import "AppDelegate.h"
#import "AuthorizationProgressViewController.h"
#import "HPAuthFlow.h"
#import "HPCredentialsHelper.h"
#import "MainViewController.h"
#import "NSBundle+HotPocketExtensions.h"
#import "NSURL+HotPocketExtensions.h"
#import "UnameLabel.h"

@interface AuthorizationViewController (AuthorizationViewControllerPrivate)

#pragma mark - Private interface

@end

@implementation AuthorizationViewController

#pragma mark - View lifecycle

-(void)viewDidLoad {
    [super viewDidLoad];
    
    self.invalidURLWarningView = [[UIImageView alloc] initWithImage:[UIImage systemImageNamed:@"exclamationmark.circle.fill"]];
    self.invalidURLWarningView.contentMode = UIViewContentModeScaleAspectFit;
    self.invalidURLWarningView.frame = CGRectMake(0, 0, 16, 16);
    self.invalidURLWarningView.tintColor = [UIColor colorNamed:@"WarningColor"];
    
    self.unameLabel.text = [NSBundle uname];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.instanceURLField.rightView = self.invalidURLWarningView;
    self.instanceURLField.rightViewMode = UITextFieldViewModeNever;
    
    [self.instanceURLField addTarget:self action:@selector(onInstanceURLFieldChanged:) forControlEvents:UIControlEventEditingChanged];
}

#pragma mark - Actions

-(IBAction)doStartAuthorizationFlow:(id)sender {
#ifdef DEBUG
    NSLog(@"-[AuthorizationViewController doStartAuthorizationFlow:] instanceURL=`%@`", self.instanceURLField.text);
#endif
    if (!self.instanceURLField.text) {
        // ???
        return;
    }
    
    NSURL *instanceURL = [NSURL URLWithString:self.instanceURLField.text];
    if (instanceURL.isUsableInHotPocket == NO) {
        // ???
        return;
    }
    
    UIApplication *application = [UIApplication sharedApplication];
    AppDelegate *appDeleate = [application delegate];
    appDeleate.authFlow.baseURL = instanceURL;
    
    NSURL *authURL = [appDeleate.authFlow start];
    if (authURL == nil) {
        // ???
        return;
    }
    
    AuthorizationProgressViewController *authorizationProgressViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"AuthorizationProgressViewController"];
    authorizationProgressViewController.authorizationURL = authURL;
    [self.navigationController pushViewController:authorizationProgressViewController animated:YES];
}

#pragma mark - Event handlers

-(void)onInstanceURLFieldChanged:(UITextField *)sender {
    sender.rightViewMode = UITextFieldViewModeNever;

    if (!sender.text || [@"" isEqualToString:sender.text]) {
        sender.rightViewMode = UITextFieldViewModeAlways;
        return;
    }
    
    NSURL *url = [NSURL URLWithString:sender.text];
    if (url == nil) {
        sender.rightViewMode = UITextFieldViewModeAlways;
        return;
    }
    
    if (url.isUsableInHotPocket == NO) {
        sender.rightViewMode = UITextFieldViewModeAlways;
        return;
    }
}

@end
