//
//  PrivateAlertService.m
//  Open
//
//  Created by 雷俊博 on 2020/1/7.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "PrivateAlertService.h"
#import "PrivateAlertView.h"


@interface PrivateAlertService ()

@property (nonatomic, strong)UIView*maskrView;

@property (nonatomic, strong)PrivateAlertView*privateAlertView;

@end

@implementation PrivateAlertService
+ (PrivateAlertService *)shareInstance
{
    static PrivateAlertService *manager = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            manager = [[PrivateAlertService alloc] init];
        });
        return manager;
}

- (void)showWithContents:(NSArray<NSString *>*)contents inViewController:(UIViewController*)viewController Callback:(void (^)(NSInteger index)  )Callback 
{
    UIWindow* keyWindow = viewController.view.window;
    
    if (keyWindow == nil) {
        keyWindow = [UIApplication sharedApplication].keyWindow;
    }
    UIView* maskView = [[UIView alloc]init];
    self.maskrView = maskView;
    
    [viewController.view addSubview:maskView];
    
    //maskView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    maskView.backgroundColor = [UIColor colorWithHex:0x000000 alpha:0.5];
    [maskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(viewController.view);
    }];
    
    [maskView layoutIfNeeded];
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(maskViewTapAction:)];
    [maskView addGestureRecognizer:tap];
    
    PrivateAlertView* privateView = [[PrivateAlertView alloc]initWithFrame:CGRectZero contents:contents];
    [viewController.view addSubview:privateView];
    self.privateAlertView = privateView;
    
    [self.privateAlertView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(viewController.view);
        make.width.offset(280);
        make.height.offset(380);
    }];
    self.privateAlertView.layer.cornerRadius = 10;
    self.privateAlertView.layer.masksToBounds = YES;
    [self animationWithView:self.privateAlertView duration:0.5];
    
    privateView.agreeCallBack = ^{
        [self dismiss];
        Callback(200);
    };
    
    privateView.disAgreeCallBack = ^{
        [self dismiss];
        Callback(201);
    };
    
    privateView.userProtocol = ^{
        //[self dismiss];
        Callback(203);
    };
    
    privateView.privateProtocol = ^{
        //[self dismiss];
        Callback(204);
    };
    
}



- (void)animationWithView:(UIView *)view duration:(NSTimeInterval)duration
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = duration;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    
    NSMutableArray<NSValue *>*values = [[NSMutableArray alloc]init];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    [view.layer addAnimation:animation forKey:nil];
    
}

- (void)dismiss
{
    [self.privateAlertView removeFromSuperview];
    [self.maskrView removeFromSuperview];
}

- (void)maskViewTapAction:(UITapGestureRecognizer *)tap
{
    //[self dismiss];
}

- (CGFloat)getRealWidth:(CGFloat)width
{
    return (ScreenWidth/375) * width;
}

@end
