/*
 LoginViewController.m
 
 View controller for login interface
 */

#import "LoginViewController.h"

@import XPMobileSDK;

@interface LoginViewController () <XPMobileSDKConnectionDelegate>

@property (weak, nonatomic) IBOutlet UITextField *address;
@property (weak, nonatomic) IBOutlet UITextField *port;
@property (weak, nonatomic) IBOutlet UITextField *username;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property UIActivityIndicatorView *activityIndicator;
@end

@implementation LoginViewController 

#pragma mark: Life cycle methods

- (void)viewDidLoad {
    [super viewDidLoad];
    [XPMobileSDK addDelegate:self];
    self.title = @"Mobile SDK Live Video Sample";
    self.port.text = @"8081";
    self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.activityIndicator.center = CGPointMake(self.view.frame.size.width / 2.0, self.view.frame.size.height / 2.0);
    [self.view addSubview: self.activityIndicator];

}

#pragma mark: IBActions
/**
 * Connect and login to server
 */
- (IBAction)login:(id)sender {
    
    NSString *urlString = [NSString stringWithFormat:@"http://%@:%@/XProtectMobile/Communication", self.address.text, self.port.text];
    [self.activityIndicator startAnimating];
    [XPMobileSDK connectAndLoginForURLString:urlString username:self.username.text password:self.password.text shouldSave:NO successHandler:^(XPSDKResponse * response) {
        [self.activityIndicator stopAnimating];
        [XPMobileSDK removeDelegate:self];
        [self performSegueWithIdentifier:@"goToCameras" sender:sender];      
    } failureHandler:^(NSError * error) {
        [self.activityIndicator stopAnimating];
        [self alertWithTitle:@"Error" andMessage:@"Unable to login"];
    }];
}

/**
 * Display application name & version number
 */
- (IBAction)openAboutDialog:(id)sender {
    [self alertWithTitle:@"About" andMessage:@"Milestone SDK Live Video Sample\nFor more information visit www.milestonesys.com."];
}

#pragma mark: Helper methods

/**
 * Helper method for presenting an alert controller
 - Parameters:
 - title: Title displayed in the alert dialog
 - message: Message displayed in the alert dialog
 */
- (void) alertWithTitle:(NSString *)title andMessage:(NSString *)message {
    UIAlertController* alert = [UIAlertController
                                alertControllerWithTitle:title
                                message:message
                                preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction
                                    actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                    handler:^(UIAlertAction * action) {}];
    
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark: XPMobileSDKConnectionDelegate methods

- (void)connectionCodeError:(XPSDKConnection * _Nonnull) connection {
    [self alertWithTitle:@"Error" andMessage:@"Unable to connect"];
}

- (void)connectionDidFailConnectWithError:(NSError *_Nonnull) error {
    [self alertWithTitle:@"Error" andMessage:@"Unable to connect"];
}

@end
