//
//  WSBKAlertViewController.m
#import "WSBKAlertViewController.h"
#import "WSBKAlertViewPresentationAnimationController.h"
#import "WSBKAlertViewDismissalAnimationController.h"

@interface WSBKAlertViewController () <UIViewControllerTransitioningDelegate>

@property (nonatomic, assign) WSBKAlertViewControllerTransitionStyle transitionStyle;
@property (nonatomic, assign) UIInterfaceOrientationMask supportedOrientations;
@property (nonatomic, weak) id<UIViewControllerTransitioningDelegate> transitioningDelegate;


@end

@implementation WSBKAlertViewController

- (instancetype)initWithTransitionStyle:(WSBKAlertViewControllerTransitionStyle)transitionStyle supportedOrientations:(UIInterfaceOrientationMask)supportedOrientations {
    self = [super initWithNibName:nil bundle:nil];

    if (self) {
        self.transitionStyle = transitionStyle;
        self.supportedOrientations = supportedOrientations;
        self.modalPresentationStyle = UIModalPresentationCustom;
        self.transitioningDelegate = self;
    }

    return self;
}

- (void)dealloc {
    NSLog(@"%@ dealloc", self);
}

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

- (void)setAlertViewContentView:(UIView *)alertViewContentView {
    [self.alertViewContentView removeFromSuperview];
    
    _alertViewContentView = alertViewContentView;
    if (alertViewContentView) {
        [self.view addSubview:alertViewContentView];
        alertViewContentView.translatesAutoresizingMaskIntoConstraints = NO;
//        [alertViewContentView.superview addConstraint:[NSLayoutConstraint constraintWithItem:alertViewContentView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:alertViewContentView.superview attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
//        [alertViewContentView.superview addConstraint:[NSLayoutConstraint constraintWithItem:alertViewContentView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:alertViewContentView.superview attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
        
//    [alertViewContentView.superview addConstraint:[NSLayoutConstraint constraintWithItem:alertViewContentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:alertViewContentView.superview attribute:NSLayoutAttributeBottom multiplier:1 constant:-(WSBKSafeAreaBottomHeight)]];
//    [alertViewContentView.superview addConstraint:[NSLayoutConstraint constraintWithItem:alertViewContentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:alertViewContentView.superview attribute:NSLayoutAttributeLeading multiplier:1 constant:10]];
//    [alertViewContentView.superview addConstraint:[NSLayoutConstraint constraintWithItem:alertViewContentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:alertViewContentView.superview attribute:NSLayoutAttributeTrailing multiplier:1 constant:-10]];
        
    }
}

#pragma mark - UIViewControllerTransitioningDelegate

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
                                                                   presentingController:(UIViewController *)presenting
                                                                       sourceController:(UIViewController *)source {
    WSBKAlertViewPresentationAnimationController *presentationAnimationController = [[WSBKAlertViewPresentationAnimationController alloc] init];
    presentationAnimationController.transitionStyle = self.transitionStyle;
    return presentationAnimationController;
}

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    WSBKAlertViewDismissalAnimationController *dismissalAnimationController = [[WSBKAlertViewDismissalAnimationController alloc] init];
    dismissalAnimationController.transitionStyle = self.transitionStyle;
    return dismissalAnimationController;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return self.supportedOrientations;
}

//#pragma mark-更新约束适配
//- (void)viewSafeAreaInsetsDidChange {
//    [super viewSafeAreaInsetsDidChange];
//    [self updateConstraints];
//}
//
//- (void)updateConstraints {
//    UIEdgeInsets safeAreaInsets = [self currentViewSafeAreaInsets];
////    [self.identifyHeadView mas_updateConstraints:^(MASConstraintMaker *make) {
////        make.top.mas_equalTo(self.view).offset(safeAreaInsets.top);
////    }];
//
//    [_alertViewContentView.superview addConstraint:[NSLayoutConstraint constraintWithItem:_alertViewContentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_alertViewContentView.superview attribute:NSLayoutAttributeBottom multiplier:1 constant:WSBKTabbarHeight+WSBKSafeAreaBottomHeight]];
//
////    [_alertViewContentView mas_updateConstraints:^(MASConstraintMaker *make) {
////        make.bottom.mas_equalTo(self.view).offset(-(safeAreaInsets.bottom+52));
////    }];
//}

//- (UIEdgeInsets)currentViewSafeAreaInsets {
//    UIEdgeInsets safeAreaInsets = UIEdgeInsetsZero;
//    if (@available(iOS 11.0, *)) {
//        safeAreaInsets = self.view.safeAreaInsets;
//    }
//    return safeAreaInsets;
//}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches.allObjects lastObject];
    BOOL result = [touch.view isDescendantOfView:self.alertViewContentView];
    if (!result) {
        if (self.tapClicked) {
            self.tapClicked(self);
        }
//        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

@end
