/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

#import "ExampleViewController.h"

#include "../../ExampleAppList.hpp"

@implementation MainMenuViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
	
	CGFloat buttonHeight = 50;
	CGFloat buttonSpacing = 10;
	CGFloat topMargin = 50;
	
	int i = 0;
	for (const auto& app : OpenVulkano::EXAMPLE_APPS)
	{
		UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
		button.frame = CGRectMake(20,
								  topMargin + i * (buttonHeight + buttonSpacing),
								  self.view.bounds.size.width - 40,
								  buttonHeight);
		[button setTitle:[NSString stringWithUTF8String:app.first] forState:UIControlStateNormal];
		button.tag = i;
		[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
		[self.view addSubview:button];
		i++;
	}
	
	self.view.frame = [UIScreen mainScreen].bounds;
	self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}

- (void)buttonPressed:(UIButton *)sender {
	ExampleViewController* example = [[ExampleViewController alloc] init];
	example.exampleId = sender.tag;
	example.modalPresentationStyle = UIModalPresentationFullScreen;
	[self presentViewController:example animated:true completion:nil];
}

- (void)viewDidLayoutSubviews {
	[super viewDidLayoutSubviews];
	self.view.frame = self.view.superview.bounds;
}
@end

@implementation ExampleViewController
- (void)viewDidLoad {
	self.renderView = [[OpenVulkanoView alloc] initWithFrame:self.view.frame];
	self.openVulkanoView = self.renderView;
	[self.view addSubview:self.renderView];
	[super viewDidLoad];
	
	self.view.frame = [UIScreen mainScreen].bounds;
	self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}

- (void)viewDidLayoutSubviews {
	[super viewDidLayoutSubviews];
	self.view.frame = self.view.superview.bounds;
	self.renderView.frame = self.view.frame;
}

- (BOOL)prefersStatusBarHidden { return true; }

- (void *)makeGraphicsApp {
	return OpenVulkano::EXAMPLE_APPS[self.exampleId].second();
}
@end
