//
//  UIViewController-ActionSheetSimulation.m
//  JasonDevelop
//
//  Created by Jason Lee on 10/23/14.
//  Copyright (c) jasondevelop. All rights reserved.
//

#import "UIViewController-ActionSheetSimulation.h"

@implementation UIViewController (ActionSheetSimulation)

- (UIView *)actionSheetSimulationWithPickerView:(UIView *)pickerView withToolbar:(UIToolbar *)pickerToolbar {

    UIView *simulatedActionSheetView = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                                                0,
                                                                                UIScreen.mainScreen.bounds.size.width,
                                                                                UIScreen.mainScreen.bounds.size.height)];
    [simulatedActionSheetView setBackgroundColor:[UIColor clearColor]];

    CGFloat pickerViewYpositionHidden = UIScreen.mainScreen.bounds.size.height + pickerToolbar.frame.size.height;

    [pickerView setFrame:CGRectMake(pickerView.frame.origin.x,
                                    pickerViewYpositionHidden,
                                    pickerView.frame.size.width,
                                    pickerView.frame.size.height)];

    CGRect tr = CGRectMake(pickerToolbar.frame.origin.x,
                           pickerViewYpositionHidden,
                           pickerToolbar.frame.size.width,
                           pickerToolbar.frame.size.height);
    [pickerToolbar setFrame:tr];
    pickerView.backgroundColor = [UIColor whiteColor];

    [simulatedActionSheetView addSubview:pickerView];
    [simulatedActionSheetView addSubview:pickerToolbar];

    return simulatedActionSheetView;
}

- (void)showActionSheetSimulation:(UIView *)actionSheetSimulation pickerView:(UIView *)pickerView withToolbar:(UIToolbar *)pickerToolbar  {
    CGFloat pickerViewYposition = UIScreen.mainScreen.bounds.size.height - pickerView.frame.size.height + UIApplication.sharedApplication.statusBarFrame.size.height;
    [UIApplication.sharedApplication.keyWindow ? UIApplication.sharedApplication.keyWindow:UIApplication.sharedApplication.windows[0]                                                                addSubview:actionSheetSimulation];

    [actionSheetSimulation.superview bringSubviewToFront:actionSheetSimulation];

    [UIView animateWithDuration:0.25f
                     animations:^{
                         [actionSheetSimulation setBackgroundColor:[[UIColor darkGrayColor] colorWithAlphaComponent:0.5]];
                         [self.view setTintAdjustmentMode:UIViewTintAdjustmentModeDimmed];
                         [self.navigationController.navigationBar setTintAdjustmentMode:UIViewTintAdjustmentModeDimmed];
                         [pickerView setFrame:CGRectMake(pickerView.frame.origin.x,
                                                         pickerViewYposition,
                                                         pickerView.frame.size.width,
                                                         pickerView.frame.size.height)];

                         CGRect tr = CGRectMake(pickerToolbar.frame.origin.x,
                                                pickerViewYposition,
                                                pickerToolbar.frame.size.width,
                                                pickerToolbar.frame.size.height);
                         [pickerToolbar setFrame:tr];
                     }
                     completion:nil];
}


-(void)dismissActionSheetSimulation:(UIView *)actionSheetSimulation {
    [UIView animateWithDuration:0.25f
                     animations:^{

                         [actionSheetSimulation setBackgroundColor:[UIColor clearColor]];
                         [self.view setTintAdjustmentMode:UIViewTintAdjustmentModeNormal];
                         [self.navigationController.navigationBar setTintAdjustmentMode:UIViewTintAdjustmentModeNormal];
                         [actionSheetSimulation.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                             UIView* v = (UIView*)obj;
                             [v setFrame:CGRectMake(v.frame.origin.x,
                                                    UIScreen.mainScreen.bounds.size.height,
                                                    v.frame.size.width,
                                                    v.frame.size.height)];
                         }];
                     }
                     completion:^(BOOL finished) {
                         [actionSheetSimulation removeFromSuperview];
                     }];

}
@end
