//  SyncViewController.m

#import "SyncScreenViewController.h"

@interface SyncScreenViewController ()

@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UILabel *lastUpdatedLabel;

@property (nonatomic, strong) UIStackView *cardStack;

@property (nonatomic, strong) UIView *offlineUnitCard;
@property (nonatomic, strong) UIView *pendingItemsCard;
@property (nonatomic, strong) UIView *historyCard;

@property (nonatomic, strong) UIButton *syncButton;

@end

@implementation SyncScreenViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = UIColor.whiteColor;

    [self setupHeader];
    [self setupMainContent];
    [self setupSyncButton];
}

#pragma mark - Header

- (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 imageNamed:@"back24"] forState:UIControlStateNormal];
    self.backButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.headerView addSubview:self.backButton];

    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.text = @"SYNC";
    self.titleLabel.textColor = UIColor.whiteColor;
    self.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.headerView addSubview:self.titleLabel];

    // Constraints
    [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.titleLabel.centerXAnchor constraintEqualToAnchor:self.headerView.centerXAnchor],
        [self.titleLabel.centerYAnchor constraintEqualToAnchor:self.headerView.centerYAnchor],
    ]];
}

#pragma mark - Main Content

- (void)setupMainContent {
    UIView *content = [[UIView alloc] init];
    content.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:content];

    [NSLayoutConstraint activateConstraints:@[
        [content.topAnchor constraintEqualToAnchor:self.headerView.bottomAnchor constant:16],
        [content.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:16],
        [content.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-16],
    ]];

    // Last Updated
    self.lastUpdatedLabel = [[UILabel alloc] init];
    self.lastUpdatedLabel.numberOfLines = 2;
    self.lastUpdatedLabel.text = @"Last Updated:\n--";
    self.lastUpdatedLabel.textColor = [UIColor grayColor];
    self.lastUpdatedLabel.font = [UIFont boldSystemFontOfSize:16];
    self.lastUpdatedLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [content addSubview:self.lastUpdatedLabel];

    [NSLayoutConstraint activateConstraints:@[
        [self.lastUpdatedLabel.topAnchor constraintEqualToAnchor:content.topAnchor],
        [self.lastUpdatedLabel.leadingAnchor constraintEqualToAnchor:content.leadingAnchor],
        [self.lastUpdatedLabel.trailingAnchor constraintEqualToAnchor:content.trailingAnchor],
    ]];

    // Cards Stack
    self.cardStack = [[UIStackView alloc] init];
    self.cardStack.axis = UILayoutConstraintAxisVertical;
    self.cardStack.spacing = 16;
    self.cardStack.translatesAutoresizingMaskIntoConstraints = NO;
    [content addSubview:self.cardStack];

    [NSLayoutConstraint activateConstraints:@[
        [self.cardStack.topAnchor constraintEqualToAnchor:self.lastUpdatedLabel.bottomAnchor constant:16],
        [self.cardStack.leadingAnchor constraintEqualToAnchor:content.leadingAnchor],
        [self.cardStack.trailingAnchor constraintEqualToAnchor:content.trailingAnchor],
    ]];

    // Add 3 cards
    [self.cardStack addArrangedSubview:[self createCardWithTitle:@"Offline Units" count:@"0"]];
    [self.cardStack addArrangedSubview:[self createCardWithTitle:@"Pending Items" count:@"0"]];
    [self.cardStack addArrangedSubview:[self createCardWithTitle:@"History" count:@"0"]];
}

#pragma mark - Card Factory

- (UIView *)createCardWithTitle:(NSString *)title count:(NSString *)countText {
    UIView *card = [[UIView alloc] init];
    card.backgroundColor = UIColor.whiteColor;
    card.layer.borderWidth = 1;
    card.layer.borderColor = [UIColor lightGrayColor].CGColor;
    card.layer.cornerRadius = 10;
    card.translatesAutoresizingMaskIntoConstraints = NO;

    [NSLayoutConstraint activateConstraints:@[
        [card.heightAnchor constraintEqualToConstant:120]
    ]];

    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = title;
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    titleLabel.textColor = [UIColor colorWithRed:0.0 green:0.5 blue:0.6 alpha:1.0];
    titleLabel.translatesAutoresizingMaskIntoConstraints = NO;

    UILabel *countLabel = [[UILabel alloc] init];
    countLabel.text = countText;
    countLabel.font = [UIFont boldSystemFontOfSize:28];
    countLabel.textColor = [UIColor colorWithRed:0.0 green:0.5 blue:0.6 alpha:1.0];
    countLabel.textAlignment = NSTextAlignmentRight;
    countLabel.translatesAutoresizingMaskIntoConstraints = NO;

    [card addSubview:titleLabel];
    [card addSubview:countLabel];

    [NSLayoutConstraint activateConstraints:@[
        [titleLabel.leadingAnchor constraintEqualToAnchor:card.leadingAnchor constant:16],
        [titleLabel.topAnchor constraintEqualToAnchor:card.topAnchor constant:16],

        [countLabel.trailingAnchor constraintEqualToAnchor:card.trailingAnchor constant:-16],
        [countLabel.bottomAnchor constraintEqualToAnchor:card.bottomAnchor constant:-16],
    ]];

    return card;
}

#pragma mark - Sync Button

- (void)setupSyncButton {
    self.syncButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.syncButton.backgroundColor = [UIColor colorWithRed:0.0 green:0.5 blue:0.6 alpha:1.0];
    [self.syncButton setTitle:@"SYNC" forState:UIControlStateNormal];
    [self.syncButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    self.syncButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    self.syncButton.layer.cornerRadius = 8;
    self.syncButton.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addSubview:self.syncButton];

    [NSLayoutConstraint activateConstraints:@[
        [self.syncButton.topAnchor constraintEqualToAnchor:self.cardStack.bottomAnchor constant:16],
        [self.syncButton.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:16],
        [self.syncButton.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-16],
        [self.syncButton.heightAnchor constraintEqualToConstant:50],
        [self.syncButton.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor constant:-16],
    ]];
}

@end

