//  DetailsViewController.mm
#import "DetailsViewController.h"

@interface AddIssueDetailsViewController ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UITextField *locationField;
@property (nonatomic, strong) UITextView *commentField;
@property (nonatomic, strong) UIButton *submitButton;
@end

@implementation AddIssueDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    [self setupHeader];
    [self setupForm];
}

- (void)setupHeader {
    CGFloat topInset = self.view.safeAreaInsets.top;
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, topInset, self.view.bounds.size.width, 48)];
    header.backgroundColor = [UIColor colorWithRed:0/255.0 green:109/255.0 blue:141/255.0 alpha:1];
    header.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:header];

    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 8, header.bounds.size.width, 32)];
    title.text = @"Add Issue Details";
    title.textColor = UIColor.whiteColor;
    title.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
    title.textAlignment = NSTextAlignmentCenter;
    [header addSubview:title];

    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(12, 8, 32, 32);
    [backButton setImage:[UIImage imageNamed:@"back24"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(dismissSelf) forControlEvents:UIControlEventTouchUpInside];
    [header addSubview:backButton];
}

- (void)setupForm {
    CGFloat topInset = self.view.safeAreaInsets.top + 48;
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, topInset, self.view.bounds.size.width, self.view.bounds.size.height - topInset)];
    [self.view addSubview:self.scrollView];

    UILabel *locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 200, 20)];
    locationLabel.text = @"*Location";
    locationLabel.font = [UIFont boldSystemFontOfSize:16];
    [self.scrollView addSubview:locationLabel];

    self.locationField = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, self.view.bounds.size.width - 40, 40)];
    self.locationField.borderStyle = UITextBorderStyleRoundedRect;
    [self.scrollView addSubview:self.locationField];

    UILabel *commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 110, 200, 20)];
    commentLabel.text = @"Comment";
    commentLabel.font = [UIFont boldSystemFontOfSize:16];
    [self.scrollView addSubview:commentLabel];

    self.commentField = [[UITextView alloc] initWithFrame:CGRectMake(20, 140, self.view.bounds.size.width - 40, 100)];
    self.commentField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.commentField.layer.borderWidth = 1.0;
    self.commentField.layer.cornerRadius = 8;
    [self.scrollView addSubview:self.commentField];

    self.submitButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.submitButton.frame = CGRectMake(20, 260, self.view.bounds.size.width - 40, 48);
    self.submitButton.backgroundColor = [UIColor colorWithRed:0/255.0 green:109/255.0 blue:141/255.0 alpha:1];
    [self.submitButton setTitle:@"Submit" forState:UIControlStateNormal];
    [self.submitButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    self.submitButton.layer.cornerRadius = 8;
    [self.submitButton addTarget:self action:@selector(handleSubmit) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:self.submitButton];
}

- (void)dismissSelf {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)handleSubmit {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Submitted"
                                                                   message:@"Issue has been added (mock)."
                                                            preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}

@end

