//
//  MFLoadingViewController.m
//  myverizon
//
//  Created by Scott Pfeil on 11/20/14.
//  Copyright (c) 2014 Verizon Wireless. All rights reserved.
//

#import "MFLoadingViewController.h"
#import "MFLoadingSpinner.h"
#import "NSLayoutConstraint+MFConvenience.h"
#import "UIColor+MFConvenience.h"
#import "MFStyler.h"
#import "MVMCoreUICommonViewsUtility.h"
#import <MVMCoreUI/MVMCoreUI-Swift.h>

@interface MFLoadingViewController ()

@property (nullable, weak, nonatomic) MFLoadingSpinner *activityIndicator;
@property (nullable, weak, nonatomic) UIView *transparentBackgroundView;
@property (nullable, weak, nonatomic) Label *indicatorText;

@end

@implementation MFLoadingViewController

- (void)loadView {
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    view.backgroundColor = [UIColor clearColor];
    self.view = view;
    
    UIStackView *loadingStack = [[UIStackView alloc] initWithFrame:CGRectZero];
    loadingStack.axis = UILayoutConstraintAxisVertical;
    loadingStack.alignment = UIStackViewAlignmentCenter;
    loadingStack.spacing = 20;
    
    // Sets up the loading view.
    MFLoadingSpinner *activityIndicatorView = [[MFLoadingSpinner alloc] initWithFrame:CGRectMake(0, 0, 36, 36)];
    activityIndicatorView.backgroundColor = [UIColor clearColor];
    activityIndicatorView.translatesAutoresizingMaskIntoConstraints = NO;
    self.activityIndicator = activityIndicatorView;
    self.activityIndicator.accessibilityIdentifier = @"Loader";
    [activityIndicatorView pinWidthAndHeight];

    Label *infoLabel = [Label label];
    infoLabel.textAlignment = NSTextAlignmentCenter;
    infoLabel.translatesAutoresizingMaskIntoConstraints = NO;
    infoLabel.hidden = true;
    self.indicatorText = infoLabel;
    
    [loadingStack addArrangedSubview:infoLabel];
    [loadingStack addArrangedSubview:activityIndicatorView];
    
    loadingStack.translatesAutoresizingMaskIntoConstraints = NO;
    [view addSubview:loadingStack];
    // Sets the constraints for the activityIndicatorView
    
    [NSLayoutConstraint constraintWithItem:infoLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:1.0];
    [NSLayoutConstraint constraintPinSubview:loadingStack pinCenterX:YES pinCenterY:YES];
    [NSLayoutConstraint constraintPinSubview:loadingStack pinTop:NO topConstant:0 pinBottom:NO bottomConstant:0 pinLeft:YES leftConstant:0 pinRight:YES rightConstant:0];
    
    // Sets up the transparent background view.
    UIView *transparentBackground = [MVMCoreUICommonViewsUtility commonView];
    transparentBackground.backgroundColor = [UIColor mfBackgroundGray];
    transparentBackground.alpha = 0.9;
    [view insertSubview:transparentBackground belowSubview:loadingStack];
    self.transparentBackgroundView = transparentBackground;
    
    // Sets the constraints of the transparent background view to be the same as the activity indicator view.
    [NSLayoutConstraint constraintPinSubviewToSuperview:transparentBackground];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - LoadingView Functions

- (void)startLoading {
    [self.activityIndicator resumeSpinner];
}

- (void)startLoadingWith:(nullable NSAttributedString *) text{
    if(text != nil){
        self.indicatorText.attributedText = text;
        self.indicatorText.accessibilityLabel = text.string;
        self.indicatorText.hidden = false;
    } else {
        self.indicatorText.attributedText = nil;
        self.indicatorText.accessibilityLabel = @"";
        self.indicatorText.hidden = true;
    }
    [self.activityIndicator resumeSpinner];
}

- (void)stopLoading {
    [self.activityIndicator pauseSpinner];
    self.indicatorText.hidden = true;
    self.indicatorText.attributedText = nil;
    self.indicatorText.accessibilityLabel = @"";
}

@end
