//
//  InvitePackageView.m
//  Open
//
//  Created by 雷俊博 on 2019/9/17.
//  Copyright © 2019 黄江涛 . All rights reserved.
//

#import "InvitePackageView.h"

#import "UIView+frameAdjust.h"
#import "Constants.h"

@interface InvitePackageView ()

/** 弹窗主内容view */
@property (nonatomic,strong) UIView   *contentView;
/** 红包图片 */
@property (nonatomic,strong) UIImageView   *packageView;
/** 红包数额 */
@property (nonatomic,copy)   NSString *coinsNumber;
/** 好友数量 */
@property (nonatomic,copy)   NSString *friendsNumber;

/** 按钮title */
@property (nonatomic,copy)   NSString *buttonTitle;

@end

@implementation InvitePackageView
#pragma mark - 构造方法

- (instancetype)initWithCoinsNumber:(NSString *)coinsNumber friendsNumber:(NSString *)friendsNumber buttonTitle:(NSString *)buttonTitle
{
    if (self = [super init]) {
        self.coinsNumber = coinsNumber;
        self.friendsNumber = friendsNumber;
        self.buttonTitle = buttonTitle;
        [self setupUI];
    }
    return self;
}

#pragma mark - UI搭建
/** UI搭建 */
- (void)setupUI
{
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    
    //------- 弹窗主内容 -------//
    self.contentView = [[UIView alloc] init];
    self.contentView.frame = CGRectMake((ScreenWidth - 290) / 2, (ScreenHeight - 300) / 2, 290, 300);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor clearColor];
    
    //弹框红包背景图
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.width, self.contentView.height)];
    [self.contentView addSubview:imageView];
    imageView.userInteractionEnabled = YES;
    imageView.image = [UIImage imageNamed:@"邀请成功-红包"];
    
    
    UILabel *lb_left = [[UILabel alloc]initWithFrame:CGRectMake(50, 38, 20, 60)];
    [self.contentView addSubview:lb_left];
    lb_left.textColor = [UIColor colorWithHex:0xd1312c];
    lb_left.text = @"赠送";
    lb_left.numberOfLines = 0;
    lb_left.font = [UIFont boldSystemFontOfSize:20];

    UILabel *lb_coinsNumber = [[UILabel alloc]initWithFrame:CGRectMake(75, 38, self.contentView.width - 85, 60)];
    [self.contentView addSubview:lb_coinsNumber];
    lb_coinsNumber.textColor = [UIColor colorWithHex:0xd1312c];
    lb_coinsNumber.text = self.coinsNumber;
    lb_coinsNumber.font = [UIFont boldSystemFontOfSize:60];
    lb_coinsNumber.textAlignment = NSTextAlignmentLeft;
    
    UILabel *lb_friendsNumber = [[UILabel alloc]initWithFrame:CGRectMake(50, lb_coinsNumber.maxY + 20, self.contentView.width - 70, 18)];
    [self.contentView addSubview:lb_friendsNumber];
    lb_friendsNumber.textColor = [UIColor colorWithHex:0x78370c];
    lb_friendsNumber.text = [NSString stringWithFormat:@"恭喜成功邀请%@位好友",self.friendsNumber];
    lb_friendsNumber.font = [UIFont systemFontOfSize:20];
    lb_friendsNumber.textAlignment = NSTextAlignmentLeft;
    
//    UILabel *lb_message = [[UILabel alloc]initWithFrame:CGRectMake(0, lb_friendsNumber.maxY + 62, self.contentView.width, 14)];
//    [self.contentView addSubview:lb_message];
//    lb_message.textColor = [UIColor colorWithHex:0xa5231e];
//    lb_message.text = @"邀请一位好友奖励500金币";
//    lb_message.font = [UIFont systemFontOfSize:14];
//    lb_message.textAlignment = NSTextAlignmentCenter;
    
    
    // 取消按钮
    UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake((self.contentView.width - 100) / 2, lb_friendsNumber.maxY +45, 100, 100)];
    [self.contentView addSubview:cancelButton];
//    [cancelButton setTitle:self.buttonTitle forState:UIControlStateNormal];
//    [cancelButton setTitleColor:[UIColor colorWithHex:0x81290a] forState:UIControlStateNormal];
//    [cancelButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
//    cancelButton.layer.cornerRadius = 17.5;
//    cancelButton.backgroundColor = [UIColor colorWithHex:0xfed401];
    [cancelButton addTarget:self action:@selector(cancelButtonClicked) forControlEvents:UIControlEventTouchUpInside];
//
//    cancelButton.layer.shadowColor = [UIColor colorWithHex:0xffda08].CGColor;
//    // 阴影偏移，默认(0, -3)
//    cancelButton.layer.shadowOffset = CGSizeMake(0,0);
//    // 阴影透明度，默认0
//    cancelButton.layer.shadowOpacity = 0.7;
//    // 阴影半径，默认3
//    cancelButton.layer.shadowRadius = 3;
    
    
    
}

#pragma mark - 弹窗展示/隐藏

/** 弹出此弹窗 */
- (void)show {
    // 出场动画
    self.alpha = 0;
    self.contentView.transform = CGAffineTransformScale(self.contentView.transform, 1.3, 1.3);
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 1;
        self.contentView.transform = CGAffineTransformIdentity;
    }];
    
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window addSubview:self];
}

/** 移除此弹窗 */
- (void)dismiss {
    [self removeFromSuperview];
}



#pragma mark - event response
/** 取消按钮点击 */
- (void)cancelButtonClicked {
    if ([self.delegate respondsToSelector:@selector(InvitePackageView:clickButtonAtIndex:)]) {
        [self.delegate InvitePackageView:self clickButtonAtIndex:1];
    }
    [self dismiss];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[touches anyObject] locationInView:self];
    CGPoint tempoint = [self.contentView convertPoint:point fromView:self];
    
    if (CGRectContainsPoint(self.contentView.bounds, tempoint)) {
        
    }else{
        [self dismiss];
    }
    
    
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
