//  IssueTutorialViewController.mm
#import "IssueTutorialViewController.h"

@interface IssuesTutorialViewController ()

@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *skipButton;
@property (nonatomic, assign) NSInteger currentPage;

@end

@implementation IssuesTutorialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.systemBackgroundColor;

    [self setupHeader];
}

#pragma mark - Header Setup
- (void)setupHeader {
  self.headerView = [[UIView alloc] init];
  self.headerView.backgroundColor = [UIColor colorWithRed:0.0 green:0.5 blue:0.6 alpha:1.0];
  self.headerView.translatesAutoresizingMaskIntoConstraints = NO;
  [self.view addSubview:self.headerView];
  
  self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
  [self.backButton setImage:[UIImage systemImageNamed:@"chevron.backward"]
                   forState:UIControlStateNormal];
  self.backButton.tintColor = UIColor.whiteColor;
  [self.backButton addTarget:self
                      action:@selector(onBack)
            forControlEvents:UIControlEventTouchUpInside];
  self.backButton.translatesAutoresizingMaskIntoConstraints = NO;
  [self.headerView addSubview:self.backButton];
  
  self.titleLabel = [[UILabel alloc] init];
  self.titleLabel.text = @"Add Issue Tutorial";
  self.titleLabel.textColor = UIColor.whiteColor;
  self.titleLabel.font = [UIFont boldSystemFontOfSize:18];
  self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
  [self.headerView addSubview:self.titleLabel];
  
  self.skipButton = [UIButton buttonWithType:UIButtonTypeSystem];
  [self.skipButton setTitle:@"Skip" forState:UIControlStateNormal];
  self.skipButton.tintColor = UIColor.whiteColor;
  self.skipButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
  [self.skipButton addTarget:self
                      action:@selector(onSkip)
            forControlEvents:UIControlEventTouchUpInside];
  self.skipButton.translatesAutoresizingMaskIntoConstraints = NO;
  [self.headerView addSubview:self.skipButton];

  // Initially visible on first screen
  self.skipButton.hidden = NO;

  [NSLayoutConstraint activateConstraints:@[
    [self.headerView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor],
    [self.headerView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
    [self.headerView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
    [self.headerView.heightAnchor constraintEqualToConstant:48],
    
    [self.backButton.leadingAnchor constraintEqualToAnchor:self.headerView.leadingAnchor constant:8],
    [self.backButton.centerYAnchor constraintEqualToAnchor:self.headerView.centerYAnchor],
    [self.backButton.widthAnchor constraintEqualToConstant:32],
    [self.backButton.heightAnchor constraintEqualToConstant:32],
    
    [self.skipButton.trailingAnchor constraintEqualToAnchor:self.headerView.trailingAnchor constant:-16],
    [self.skipButton.centerYAnchor constraintEqualToAnchor:self.headerView.centerYAnchor],
    
    [self.titleLabel.centerXAnchor constraintEqualToAnchor:self.headerView.centerXAnchor],
    [self.titleLabel.centerYAnchor constraintEqualToAnchor:self.headerView.centerYAnchor],
  ]];
}

#pragma mark - Helper
- (void)onBack {
  [self.navigationController popViewControllerAnimated:YES];
}

-(void)onSkip {
  [self completeTutorial];
}

-(void)completeTutorial {
  [self onBack];
}

-(void)updateHeaderButtons {
  self.skipButton.hidden = (self.currentPage == 2);
}

- (void)nextButtonTapped {
    if (self.currentPage < 2) {
        self.currentPage++;
//        [self showPage:self.currentPage];
        [self updateHeaderButtons];
    } else {
        [self completeTutorial];
    }
}

- (void)backButtonTapped {
    if (self.currentPage > 0) {
        self.currentPage--;
//        [self showPage:self.currentPage];
        [self updateHeaderButtons];
    }
}


@end
