#import "CQBouncerCreationViewController.h"

#import "CQBouncerEditViewController.h"
#import "CQBouncerSettings.h"
#import "CQColloquyApplication.h"
#import "CQConnectionsController.h"

NS_ASSUME_NONNULL_BEGIN

@implementation CQBouncerCreationViewController {
	CQBouncerSettings *_settings;
}

- (instancetype) init {
	if (!(self = [super init]))
		return nil;

	_settings = [[CQBouncerSettings alloc] init];

	return self;
}

#pragma mark -

- (void) viewDidLoad {
	if (!_rootViewController) {
		CQBouncerEditViewController *editViewController = [[CQBouncerEditViewController alloc] init];
		editViewController.newBouncer = YES;
		editViewController.settings = _settings;

		_rootViewController = editViewController;
	}

	UIBarButtonItem *addItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add", @"Add button title") style:UIBarButtonItemStyleDone target:self action:@selector(commit:)];
	_rootViewController.navigationItem.rightBarButtonItem = addItem;

	_rootViewController.navigationItem.rightBarButtonItem.tag = UIBarButtonSystemItemSave;
	_rootViewController.navigationItem.rightBarButtonItem.enabled = NO;

	[super viewDidLoad];
}

#pragma mark -

- (void) commit:(__nullable id) sender {
	[(CQBouncerEditViewController *)_rootViewController endEditing];

	[[CQConnectionsController defaultController] addBouncerSettings:_settings];

	[[CQColloquyApplication sharedApplication] showConnections:nil];

	[[CQColloquyApplication sharedApplication] dismissModalViewControllerAnimated:YES];
}
@end

NS_ASSUME_NONNULL_END
