//
//  MVMCoreUICommonViewsUtility.m
//  myverizon
//
//  Created by Scott Pfeil on 12/16/14.
//  Copyright (c) 2014 Verizon Wireless. All rights reserved.
//

#import "MVMCoreUICommonViewsUtility.h"
#import "MFStyler.h"
#import "UIColor+MFConvenience.h"
#import "NSLayoutConstraint+MFConvenience.h"
#import "MVMCoreUIUtility.h"
@import MVMCore.NSDictionary_MFConvenience;
@import MVMCore.MVMCoreConstants;
@import MVMCore.MVMCoreGetterUtility;

static const CGFloat HorizontalShadowInset = 5;
static const CGFloat VertialShadowOffset = 6;

@interface MVMCoreUICommonViewsUtility ()

// Adds a dismiss toolbar to whatever the passed in object is.
+ (void)addDismissToolbarToObject:(id)object delegate:(id)delegate;

@end

@implementation MVMCoreUICommonViewsUtility

+ (nonnull UIView *)commonView {
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    view.translatesAutoresizingMaskIntoConstraints = NO;
    view.backgroundColor = [UIColor clearColor];
    view.directionalLayoutMargins = NSDirectionalEdgeInsetsZero;
    return view;
}

#pragma mark - Labels

+ (nonnull UILabel *)label {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.backgroundColor = [UIColor clearColor];
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.translatesAutoresizingMaskIntoConstraints = NO;
    return label;
}

+ (nonnull UILabel *)commonLabelHeadline {
    UILabel *label = [MVMCoreUICommonViewsUtility label];
    [MFStyler styleLabelH2:label];
    return label;
}

+ (nonnull UILabel *)commonLabelH1 {
    UILabel *label = [MVMCoreUICommonViewsUtility label];
    [MFStyler styleLabelH1:label];
    return label;
}

+ (nonnull UILabel *)commonLabelH2 {
    UILabel *label = [MVMCoreUICommonViewsUtility label];
    [MFStyler styleLabelH2:label];
    return label;
}

+ (nonnull UILabel *)commonLabelH3 {
    UILabel *label = [MVMCoreUICommonViewsUtility label];
    [MFStyler styleLabelH3:label];
    return label;
}

+ (nonnull UILabel *)commonLabelH32 {
    UILabel *label = [MVMCoreUICommonViewsUtility label];
    [MFStyler styleLabelH32:label];
    return label;
}

+ (nonnull UILabel *)commonLabelB1 {
    UILabel *label = [MVMCoreUICommonViewsUtility label];
    [MFStyler styleLabelB1:label];
    return label;
}

+ (nonnull UILabel *)commonLabelB2 {
    UILabel *label = [MVMCoreUICommonViewsUtility label];
    [MFStyler styleLabelB2:label];
    return label;
}

+ (nonnull UILabel *)commonLabelB3 {
    UILabel *label = [MVMCoreUICommonViewsUtility label];
    [MFStyler styleLabelB3:label];
    return label;
}

#pragma mark - Text Field

+ (nonnull UIPickerView *)addPickerToTextField:(nonnull UITextField *)textField delegate:(nullable id <UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource>)delegate {
    
    // Sets up the picker (same tag as the textfield)
    UIPickerView *picker = [MVMCoreUICommonViewsUtility commonPickerView];
    picker.delegate = delegate;
    picker.dataSource = delegate;
    picker.tag = [textField tag];
    [textField setInputView:picker];
    
    // Adds a dismiss toolbar, since all fields with pickers should have one.
    [MVMCoreUICommonViewsUtility addDismissToolbar:textField delegate:delegate];
    
    return picker;
}

+ (nonnull UIDatePicker *)addDatePickerToTextField:(nonnull UITextField *)textField {
    UIDatePicker *datePicker = [[UIDatePicker alloc]init];
    datePicker.backgroundColor = [UIColor whiteColor];
    datePicker.datePickerMode = UIDatePickerModeDate;
    NSLocale *locale = [NSLocale currentLocale];
    datePicker.locale = locale;
    datePicker.calendar = [locale objectForKey:NSLocaleCalendar];
    //To handle new style in ios 13.4 and above
    if (@available(iOS 13.4, *)) {
        [datePicker setPreferredDatePickerStyle:UIDatePickerStyleWheels];
    }
    datePicker = datePicker;
    textField.inputView = datePicker;
    return datePicker;
}

+ (nonnull UIDatePicker *)addTimeDatePickerToTextField:(nonnull UITextField *)textField {
    UIDatePicker *datePicker = [[UIDatePicker alloc]init];
    datePicker.backgroundColor = [UIColor whiteColor];
    datePicker.datePickerMode = UIDatePickerModeTime;
    datePicker = datePicker;
    //To handle new style in ios 13.4 and above
    if (@available(iOS 13.4, *)) {
        [datePicker setPreferredDatePickerStyle:UIDatePickerStyleWheels];
    }
    textField.inputView = datePicker;
    
    return datePicker;
}


#pragma mark - Text View

+ (nonnull UITextView *)commonFormTextView {
    
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectZero];
    textView.backgroundColor = [UIColor clearColor];
    //textView.font = [MFStyler fontForStandardMessageLabel];
    //textView.textColor = [UIColor mfCharcoalColor];
    textView.scrollEnabled = NO;
    textView.pagingEnabled = NO;
    textView.allowsEditingTextAttributes = NO;
    textView.selectable = YES;
    textView.editable = NO;
    textView.showsHorizontalScrollIndicator = NO;
    textView.showsVerticalScrollIndicator = NO;
    textView.bounces = NO;
    textView.bouncesZoom = NO;
    textView.delaysContentTouches = NO;
    textView.canCancelContentTouches = NO;
    // To Remove extra inner paddings and insets
    textView.textContainer.lineFragmentPadding = 0;
    textView.textContainerInset = UIEdgeInsetsZero;
    [textView sizeToFit];
    textView.translatesAutoresizingMaskIntoConstraints = NO;
    [MVMCoreUICommonViewsUtility setDataDetectorTypesForTextView:textView];
    textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor blackColor], NSUnderlineStyleAttributeName:[NSNumber numberWithInt:NSUnderlineStyleSingle]};
    return textView;
}

+ (void)setDataDetectorTypesForTextView:(nonnull UITextView *)textView {
    
    // Do not detect/call number on iPad
    if ([MVMCoreGetterUtility isOnIPad]) {
        
        // Except phone number it should detect all other data types
        [textView setDataDetectorTypes:UIDataDetectorTypeAddress | UIDataDetectorTypeCalendarEvent | UIDataDetectorTypeLink];
    } else {
        [textView setDataDetectorTypes:UIDataDetectorTypeAll];
    }
}

#pragma mark - Toolbar

+ (nonnull UIToolbar *)makeEmptyToolbar {
    CGRect screenFrame = [[UIScreen mainScreen] bounds];
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(screenFrame), 44.0)];
    [toolbar setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth)];
    toolbar.barStyle = UIBarStyleDefault;
    toolbar.barTintColor = [UIColor mfToolBarGrayColor];
    toolbar.translucent = YES;
    return toolbar;
}

+ (void)addDismissToolbarToObject:(id)object delegate:(id)delegate {
    UIToolbar *toolbar = [MVMCoreUICommonViewsUtility makeEmptyToolbar];
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *dismissButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:delegate action:@selector(dismissFieldInput:)];
    dismissButton.tintColor = [UIColor blackColor];
    [toolbar setItems:@[space,dismissButton]];
    [object setInputAccessoryView:toolbar];
}

+ (void)addDismissToolbar:(nonnull UITextField *)textField delegate:(nullable id <UITextFieldDelegate>)delegate {
    [self addDismissToolbarToObject:textField delegate:delegate];
}

+ (void)addDismissToolbarToTextView:(nonnull UITextView *)textView delegate:(nullable id <UITextViewDelegate>)delegate {
    [self addDismissToolbarToObject:textView delegate:delegate];
}

#pragma mark - Others

+ (UIPickerView *)commonPickerView {
    UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectZero];
    picker.backgroundColor = [UIColor whiteColor];
    picker.showsSelectionIndicator = YES;
    return picker;
}

+ (nullable UIView *)getAndSetupSafeAreaViewOnView:(nonnull UIView *)view {
    UIView *safeAreaView = [MVMCoreUICommonViewsUtility commonView];
    [view addSubview:safeAreaView];
    [safeAreaView.topAnchor constraintEqualToAnchor:view.safeAreaLayoutGuide.bottomAnchor].active = YES;
    [view.bottomAnchor constraintEqualToAnchor:safeAreaView.bottomAnchor].active = YES;
    [NSLayoutConstraint constraintPinSubview:safeAreaView pinTop:NO topConstant:0 pinBottom:NO bottomConstant:0 pinLeft:YES leftConstant:0 pinRight:YES rightConstant:0];
    return safeAreaView;
}

#pragma mark border

+ (nullable UIImage *)addBorderToImage:(nullable UIImage *)image {
    if (!image) {
        return nil;
    }
    CGSize size = [image size];
    UIGraphicsBeginImageContext(size);

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
    CGContextSetLineWidth(context, 1.0);
    CGContextMoveToPoint(context, 0, 0);
    CGContextAddLineToPoint(context, size.width, 0);
    CGContextAddLineToPoint(context, size.width, size.height);
    CGContextAddLineToPoint(context, 0, size.height);
    CGContextAddLineToPoint(context, 0, 0);
    CGContextStrokePath(context);
    
    //Comensating for the border so that none of the image is missed
    CGRect newImageRect = CGRectMake(1, 1, size.width - 2, size.height - 2);
    [image drawInRect: newImageRect];
    
    UIImage *imageWithBorder =  UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return imageWithBorder;

}

#pragma mark - shadows

+ (void)drawBottomCurvedShadowsOnRect:(CGRect)rect toView:(nullable UIView *)view {
    
    UIBezierPath *shadowPath = [UIBezierPath bezierPath];
    
    //get the variables for frame
    CGFloat x = rect.origin.x;
    CGFloat y = rect.origin.y;
    CGFloat width = CGRectGetWidth(rect);
    CGFloat height = CGRectGetHeight(rect);
    
    [shadowPath moveToPoint:CGPointMake(x + HorizontalShadowInset, y)];
    [shadowPath addLineToPoint:CGPointMake(x + width - HorizontalShadowInset, y)];
    [shadowPath addLineToPoint:CGPointMake(x + width - HorizontalShadowInset, height-VertialShadowOffset/2)];
    [shadowPath addQuadCurveToPoint:CGPointMake(x + HorizontalShadowInset, height - VertialShadowOffset/2) controlPoint:CGPointMake((x + width)/2.f, height - VertialShadowOffset * 1.5)];
    [shadowPath addLineToPoint:CGPointMake(x + HorizontalShadowInset, y)];
    [shadowPath closePath];
    
    view.layer.shadowColor = [UIColor blackColor].CGColor;
    view.layer.shadowOffset = CGSizeMake(0.0f, VertialShadowOffset);
    view.layer.shadowOpacity = 0.25f;
    view.layer.shadowPath = shadowPath.CGPath;
    
}

@end
