//
//  DropboxViewController.m
//  AppBox
//
//  Created by Vineet Choudhary on 28/11/16.
//  Copyright © 2016 Developer Insider. All rights reserved.
//

#import "DropboxViewController.h"


@implementation DropboxViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //DB Authentication Notification
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleLoggedInNotification:) name:abDropBoxLoggedInNotification object:nil];
	
	//Set quit button title
	[buttonQuit setTitle:[UserData isLoggedIn] ? @"Cancel" : @"Quit"];
}

- (IBAction)buttonConnectDropboxTapped:(NSButton *)sender {
	//Authenticate user
	[DBClientsManager authorizeFromControllerDesktopV2:[NSWorkspace sharedWorkspace] controller:self loadingStatusDelegate:nil openURL:^(NSURL * _Nonnull url) {
		[[NSWorkspace sharedWorkspace] openURL:url];
	} scopeRequest:nil];
}

- (IBAction)buttonQuitTapped:(NSButton *)sender {
    [self dismissController:self];
	if (![UserData isLoggedIn]) {
		[NSApp terminate:self];
	}
}

//MARK: - Event Handler
- (void)handleLoggedInNotification:(NSNotification *)notification{
    [[NSApplication sharedApplication] updateAccountsMenu];
    [self dismissController:self];
}



@end
