//
//  PNSBuildModelUtils.m
//  XinMai
//
//  Created by mac on 2020/10/19.
//

#import "PNSBuildModelUtils.h"
#import "UIImage+Common.h"
#import <ATAuthSDK/ATAuthSDK.h>

@implementation PNSBuildModelUtils


+ (CGFloat)safeAreaInsetsBottom {
    // 根据安全区域判断
    if (@available(iOS 11.0, *)) {
        CGFloat height = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom;
        return height;
    } else {
        return 0;
    }
}


+ (BOOL)isBlankString:(NSString *)aStr {
    if (!aStr ) {
        return YES;
    }
    if([aStr isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if(!aStr.length) {
        return YES;
    }
    if(aStr == nil) {
        return YES;
    }
    if(aStr == NULL) {
        return YES;
    }
    if ([aStr isEqualToString:@"NULL"]) {
        return YES;
    }
    NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSString *trimmedStr = [aStr stringByTrimmingCharactersInSet:set];
    if (!trimmedStr.length) {
        return YES;
        
    }
    return NO;
}

+ (BOOL)isBlankArr:(NSArray *)arr{
    if (!arr) {
        return YES;
    }
    if (![arr isKindOfClass:[NSArray class]]) {
        return YES;
    }
    if ([arr isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if (!arr.count) {
        return YES;
    }
    if (arr == nil || arr == NULL) {
        return YES;
    }
    return NO;
}
///全屏 竖屏
+ (TXCustomModel *)buildSheetPortraitModelWithLogoImageName:(NSString *)logoImageName registerPrivacyUrl:(NSString *)registerPrivacyUrl protectPrivacyUrl:(NSString *)protectPrivacyUrl checkBoxImage:(NSString *)checkBoxImage sloganText:(NSString *)sloganText loginBtnText:(NSString *)loginBtnText changeBtnTitle:(NSString *)changeBtnTitle loginBtnBgImgs:(NSArray *)loginBtnBgImgs{
    TXCustomModel *model = [[TXCustomModel alloc] init];
    NSDictionary *attributes = @{
        NSForegroundColorAttributeName : [UIColor colorWithRed:153.f/225.f green:153.f/225.f blue:153.f/225.f alpha:1.0],
        NSFontAttributeName : [UIFont systemFontOfSize:12.0]
    };
    if ([self isBlankString:logoImageName]) {
        logoImageName = @"default_logo";
    }
    model.navColor = [UIColor whiteColor];
    model.navTitle = [[NSAttributedString alloc] initWithString:@""];
    model.navBackImage = [UIImage imageNamed:@"icon_close_black"];
    model.sloganIsHidden = YES;
    model.logoImage = [UIImage imageNamed:logoImageName];
    if ([self isBlankString:registerPrivacyUrl]) {
        registerPrivacyUrl = @"REGISTER";
    }
    if ([self isBlankString:protectPrivacyUrl]) {
        protectPrivacyUrl = @"PROTECT";
    }
    model.privacyOne = @[@"《注册协议》", registerPrivacyUrl];
    model.privacyTwo = @[@"《隐私保护政策》", protectPrivacyUrl];
    model.privacyOperatorPreText = @"《";
    model.privacyOperatorSufText = @"》";
    model.privacyPreText = @"阅读并同意";
    model.privacySufText = @"";
    model.privacyAlignment = NSTextAlignmentCenter;
    //255,93,43
    model.privacyColors = @[[UIColor colorWithRed:126.f/225.f green:130.f/225.f blue:139.f/225.f alpha:1.0],[UIColor colorWithRed:63.f/225.f green:109.f/225.f blue:246.f/225.f alpha:1.0]];
    model.privacyNavBackImage = [UIImage imageNamed:@"icon_close_black"];
     
    model.logoFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
        frame.size.width = 104;
        frame.size.height = 106;
        frame.origin.y = 28;
        frame.origin.x = (superViewSize.width - 104) * 0.5;
        return frame;
    };
    if ([self isBlankString:sloganText]) {
        sloganText = @"本机号码一键登录";
    }
//    model.sloganText = [[NSAttributedString alloc] initWithString:sloganText attributes:attributes];
//    model.sloganFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
//        frame.origin.y =  106 + 15 + 28;
//        return frame;
//    };
    model.numberColor = [UIColor colorWithRed:10.f/225.f green:18.f/225.f blue:39.f/225.f alpha:1.0];
    model.numberFont = [UIFont boldSystemFontOfSize:23.f];
    model.numberFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
        frame.origin.y = 106 + 30 + 28;
        return frame;
    };
    NSDictionary *loginAttributes = @{
        NSForegroundColorAttributeName : [UIColor whiteColor],
        NSFontAttributeName : [UIFont systemFontOfSize:16.0]
    };
    if ([self isBlankString:loginBtnText]) {
        if ([self isBlankArr:loginBtnBgImgs]) {
            loginBtnText = @"本机号码一键登录";
        }else{
            loginBtnText = @"";
        }
    }
    model.loginBtnText = [[NSAttributedString alloc] initWithString:loginBtnText attributes:loginAttributes];
    UIImage *loginImage = [UIImage dh_imageWithColor:[UIColor colorWithRed:63/225.f green:109.f/225.f blue:246.f/225.f alpha:1.0] size:CGSizeMake([UIScreen mainScreen].bounds.size.width - 60.f, 44.f)];
    loginImage = [loginImage dh_getCornerRadius:10.f];
    if ([self isBlankArr:loginBtnBgImgs]) {
        loginBtnBgImgs = @[loginImage,loginImage,loginImage];
    }else{
        loginBtnBgImgs = @[[UIImage imageNamed:[loginBtnBgImgs firstObject]],[UIImage imageNamed:[loginBtnBgImgs firstObject]],[UIImage imageNamed:[loginBtnBgImgs firstObject]]];
    }
    model.loginBtnBgImgs = loginBtnBgImgs;
    model.loginBtnFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
        frame.origin.y = 106 + 30 + 28 + 40 + 20;
        frame.size.height = 44.f;
        frame.origin.x = 30.f;
        frame.size.width = [UIScreen mainScreen].bounds.size.width - 60.f;
        return frame;
    };
    NSDictionary *changeAttributes = @{
      NSForegroundColorAttributeName : [UIColor colorWithRed:10.f/225.f green:18.f/225.f blue:39.f/225.f alpha:1.0],NSFontAttributeName : [UIFont systemFontOfSize:16.0]
        };
    model.changeBtnIsHidden = NO;
    if ([self isBlankString:changeBtnTitle]) {
        changeBtnTitle = @"其他方式登录";
    }
    model.changeBtnTitle = [[NSAttributedString alloc] initWithString:changeBtnTitle attributes:changeAttributes];
    model.changeBtnFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
        frame.origin.y = 106 + 30 + 28 + 40 + 20 + 44 + 28;
        return frame;
    };
    model.supportedInterfaceOrientations = UIInterfaceOrientationMaskPortrait;
    return model;
}

///底部弹窗
+ (TXCustomModel *)buildSheetAlertModelWithLogoImageName:(NSString *)logoImageName registerPrivacyUrl:(NSString *)registerPrivacyUrl protectPrivacyUrl:(NSString *)protectPrivacyUrl checkBoxImage:(NSString *)checkBoxImage sloganText:(NSString *)sloganText loginBtnText:(NSString *)loginBtnText changeBtnTitle:(NSString *)changeBtnTitle loginBtnBgImgs:(NSArray *)loginBtnBgImgs{
    TXCustomModel *model = [[TXCustomModel alloc] init];
    model.supportedInterfaceOrientations = UIInterfaceOrientationMaskPortrait;
    model.alertCornerRadiusArray = @[@10, @0, @0, @10];
    model.alertBlurViewAlpha = 0.5;
    model.alertTitleBarColor = [UIColor whiteColor];
    NSDictionary *attributes = @{
        NSForegroundColorAttributeName : [UIColor colorWithRed:153.f/225.f green:153.f/225.f blue:153.f/225.f alpha:1.0],
        NSFontAttributeName : [UIFont systemFontOfSize:12.0]
    };
    model.alertTitle = [[NSAttributedString alloc] initWithString:@"" attributes:attributes];
    model.alertCloseImage = [UIImage imageNamed:@"login_close"];
    if ([self isBlankString:logoImageName]) {
        logoImageName = @"default_logo";
    }
    model.logoImage = [UIImage imageNamed:logoImageName];
    model.alertCloseItemIsHidden = NO;
    if ([self isBlankString:registerPrivacyUrl]) {
        registerPrivacyUrl = @"REGISTER";
    }
    if ([self isBlankString:protectPrivacyUrl]) {
        protectPrivacyUrl = @"PROTECT";
    }
    model.privacyOne = @[@"《注册协议》", registerPrivacyUrl];
    model.privacyTwo = @[@"《隐私保护政策》", protectPrivacyUrl];
    UIImage *unselectImage = [UIImage dh_imageWithColor:[UIColor whiteColor] size:CGSizeMake(1, 1.f)];
    UIImage *selectImage = [UIImage dh_imageWithColor:[UIColor whiteColor] size:CGSizeMake(1, 1.f)];
    model.checkBoxImages = @[unselectImage,selectImage];
    model.checkBoxIsChecked = YES;
    model.checkBoxIsHidden = YES;
    model.privacyOperatorPreText = @"《";
    model.privacyOperatorSufText = @"》";
    model.privacyPreText = @"点击“登录”即同意";
    model.privacySufText = @"并使用本机号码登录";
    model.privacyAlignment = NSTextAlignmentCenter;
    //255,93,43
    model.privacyColors = @[[UIColor colorWithRed:153.f/225.f green:153.f/225.f blue:153.f/225.f alpha:1.0],[UIColor colorWithRed:255.f/225.f green:93.f/225.f blue:43.f/225.f alpha:1.0],[UIColor colorWithRed:255.f/225.f green:93.f/225.f blue:43.f/225.f alpha:1.0]];
    model.privacyNavBackImage = [UIImage imageNamed:@"appBack"];
     
    NSMutableParagraphStyle *paragrapStyle = [[NSMutableParagraphStyle alloc]init];
    paragrapStyle.alignment = NSTextAlignmentCenter;//文字居中属性
    NSString *tipTitle = @"我们会对您的信息严格保密";
    NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:tipTitle];
    [attriStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:153.f/225.f green:153.f/225.f blue:153.f/225.f alpha:1.0] range:NSMakeRange(0, tipTitle.length)];
    [attriStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, tipTitle.length)];
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    // 表情图片
    if ([self isBlankString:checkBoxImage]) {
        checkBoxImage = @"checkBoxImages";
    }
    attch.image = [UIImage imageNamed:checkBoxImage];
    // 创建带有图片的富文本
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithAttributedString:[NSAttributedString attributedStringWithAttachment:attch]];
    [string appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
    [attriStr insertAttributedString:string atIndex:0];
    
    [attriStr addAttribute:NSParagraphStyleAttributeName value:paragrapStyle range:NSMakeRange(0, attriStr.length)];
    
    UILabel *tipLabel = [[UILabel alloc] init];
    tipLabel.attributedText = attriStr;
    
    model.customViewBlock = ^(UIView * _Nonnull superCustomView) {
        [superCustomView addSubview:tipLabel];
    };
    model.customViewLayoutBlock = ^(CGSize screenSize, CGRect contentViewFrame, CGRect navFrame, CGRect titleBarFrame, CGRect logoFrame, CGRect sloganFrame, CGRect numberFrame, CGRect loginFrame, CGRect changeBtnFrame, CGRect privacyFrame) {
        tipLabel.frame = CGRectMake(CGRectGetMinX(loginFrame),
                                   CGRectGetMinY(privacyFrame) - 20,
                                   CGRectGetWidth(loginFrame),
                                   20);
    };
    model.contentViewFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
        frame.size.width = superViewSize.width;
        frame.size.height =  385 + [self safeAreaInsetsBottom];
        frame.origin.x = 0;
        frame.origin.y = superViewSize.height - frame.size.height;
        return frame;
    };
    model.logoFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
        frame.size.width = 55;
        frame.size.height = 55;
        frame.origin.y = 0;
        frame.origin.x = (superViewSize.width - 55) * 0.5;
        return frame;
    };
    if ([self isBlankString:sloganText]) {
        sloganText = @"本机号码一键登录";
    }
    model.sloganText = [[NSAttributedString alloc] initWithString:sloganText attributes:attributes];
    model.sloganFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
        frame.origin.y =  55 + 15;
        return frame;
    };
    model.numberColor = [UIColor colorWithRed:51.f/225.f green:51.f/225.f blue:51.f/225.f alpha:1.0];
    model.numberFont = [UIFont boldSystemFontOfSize:18.f];
    model.numberFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
        frame.origin.y = 55 + 20 + 18;
        return frame;
    };
    NSDictionary *loginAttributes = @{
        NSForegroundColorAttributeName : [UIColor whiteColor],
        NSFontAttributeName : [UIFont systemFontOfSize:16.0]
    };
    if ([self isBlankString:loginBtnText]) {
        if ([self isBlankArr:loginBtnBgImgs]) {
            loginBtnText = @"本机号码一键登录";
        }else{
            loginBtnText = @"";
        }
    }
    model.loginBtnText = [[NSAttributedString alloc] initWithString:loginBtnText attributes:loginAttributes];
    UIImage *loginImage = [UIImage dh_imageWithColor:[UIColor colorWithRed:253/225.f green:90.f/225.f blue:39.f/225.f alpha:1.0] size:CGSizeMake([UIScreen mainScreen].bounds.size.width - 70.f, 44.f)];
    loginImage = [loginImage dh_getCornerRadius:22.f];
    if ([self isBlankArr:loginBtnBgImgs]) {
        loginBtnBgImgs = @[loginImage,loginImage,loginImage];
    }else{
        loginBtnBgImgs = @[[UIImage imageNamed:[loginBtnBgImgs firstObject]],[UIImage imageNamed:[loginBtnBgImgs firstObject]],[UIImage imageNamed:[loginBtnBgImgs firstObject]]];
    }
    model.loginBtnBgImgs = loginBtnBgImgs;
    model.loginBtnFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
        frame.origin.y = 55 + 10 + 18 + 35 + 30;
        frame.size.height = 44.f;
        frame.origin.x = (superViewSize.width - 300) * 0.5;
        frame.size.width = 300;
        return frame;
    };
    
    NSDictionary *changeAttributes = @{
      NSForegroundColorAttributeName : [UIColor colorWithRed:102/225.f green:102.f/225.f blue:102.f/225.f alpha:1.0],NSFontAttributeName : [UIFont systemFontOfSize:12.0]
        };
    model.changeBtnIsHidden = NO;
    if ([self isBlankString:changeBtnTitle]) {
        changeBtnTitle = @"其他方式登录";
    }
    model.changeBtnTitle = [[NSAttributedString alloc] initWithString:changeBtnTitle attributes:changeAttributes];
    model.changeBtnFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
        frame.origin.y = 55 + 10 + 18 + 35 + 30 + 15 + 44;
        return frame;
    };
    
    model.supportedInterfaceOrientations = UIInterfaceOrientationMaskPortrait;
    return model;
}

+ (UIImage *)getImageWithColor:(UIColor *)color {
    
    CGSize size = CGSizeMake(1, 1);
    CGRect rect = (CGRect){CGPointZero, size};
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(ctx, color.CGColor);
    CGContextFillRect(ctx, rect);
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}

@end
