//
//  CTToast.m
//  Open
//
//  Created by 雷俊博 on 2019/10/31.
//  Copyright © 2019 黄江涛 . All rights reserved.
//

#import "CTToast.h"
#import "StartView.h"
@interface CTToast ()

@property (nonatomic, strong) UILabel      *messageLabel;
@property (nonatomic, strong) UIImageView  *imageView;

@property (nonatomic, strong)UILabel *messageLabel2;

@property (nonatomic, strong) CTToast *Toast;

@property (nonatomic, strong) StartView *startView;
@end

@implementation CTToast

#pragma mark - Initial
// 初始化方法里可以对各种toast的一致属性进行统一设置
- (instancetype)init {
    self = [super init];
    if (self) {
        
        [self addSubview:self.imageView];
        [self addSubview:self.messageLabel];
        [self addSubview:self.messageLabel2];
        
        //toast的一致属性
        self.layer.cornerRadius = 12;
        self.backgroundColor = [UIColor colorWithHex:0x000000 alpha:0.6];
        self.messageLabel.numberOfLines = 0;
        self.messageLabel.textAlignment = NSTextAlignmentCenter;
        self.messageLabel2.numberOfLines = 0;
        self.messageLabel2.textAlignment = NSTextAlignmentCenter;
    }
    return self;
}


#pragma mark - UI
- (void)p_setupImageTextToastWithMessage:(NSString *)message image:(NSString *)imageName
{
    // label
   self.messageLabel.text = message;
   self.messageLabel.font = [UIFont boldSystemFontOfSize:22];
    // imageView
    self.imageView.image = [UIImage imageNamed:imageName];
    
    // 设置toast的约束
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.superview);
        make.width.mas_equalTo(155);
        make.height.mas_equalTo(170);
    }];
    
    // 设置imageView的约束
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self);
         make.width.mas_equalTo(155);
        make.height.mas_equalTo(156);
    }];
    
    // 设置label的约束
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self.mas_centerY).offset(5);
    }];
    
    [self.messageLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
           make.centerX.equalTo(self);
           make.bottom.equalTo(self.mas_bottom).offset(-12);
       }];
}
- (instancetype)initAnimation {
    self = [super init];
    if (self) {
        self.startView = [[[NSBundle mainBundle] loadNibNamed:@"StartView" owner:nil options:nil] firstObject];
        self.backgroundColor = [UIColor clearColor];
        [self addSubview:self.startView];
    }
    return self;
}
- (void)p_setupAnimation{
    UIWindow *window = [UIApplication sharedApplication].windows.lastObject;
    UIImageView *NO1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    NO1.image = [UIImage imageNamed:@"Coin"];
    NO1.center = window.center;
    
    UIImageView *NO2 = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(NO1.frame) - 10, 0, 30, 30)];
    NO2.image = [UIImage imageNamed:@"Coin"];
    NO2.centerY = NO1.centerY;
    [window addSubview:NO1];
    [window addSubview:NO2];
    
    [self startNextAnimation:NO1];
    [self startNextAnimation:NO2];
}
#pragma mark - show toast
+ (void)showWithMessage:(NSString *)message image:(NSString *)imageName duration:(NSTimeInterval)duration
{
    //切换到主线程
    dispatch_async(dispatch_get_main_queue(), ^{
        CTToast *toast = [[CTToast alloc]init];
        [[UIApplication sharedApplication].windows.lastObject addSubview:toast];
        [toast p_setupImageTextToastWithMessage:message image:imageName];
       
//        CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
//        animation.duration = duration;
//        NSMutableArray *values = [NSMutableArray array];
//        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
//        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5, 0.5, 1.0)]];
//        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
//        animation.values = values;
//        animation.removedOnCompletion = NO;
//        animation.fillMode = kCAFillModeForwards;
//        [toast.layer addAnimation:animation forKey:nil];
        // 指定时间移除
//        WeakSelf
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration  * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 将要消失
            [UIView animateWithDuration:0.3 animations:^{
                toast.alpha = 0;
            } completion:^(BOOL finished) {
                [toast removeFromSuperview];
                // 已经消失
            }];
        });
    });
}

- (void)startNextAnimation:(UIView *)View{
    CAKeyframeAnimation *anima = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    anima.duration = 1;
    NSValue *value0 = [NSValue valueWithCGPoint:self.center];
    NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(ScreenWidth - 50, 50)];
    anima.values = [NSArray arrayWithObjects:value0,value1, nil];
    anima.removedOnCompletion = NO;
    anima.fillMode = kCAFillModeForwards;
    [View.layer addAnimation:anima forKey:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1  * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [View removeFromSuperview];
    });
}
#pragma mark - getters and setters
- (UILabel *)messageLabel
{
    if (!_messageLabel) {
        _messageLabel = [[UILabel alloc]init];
        _messageLabel.textColor = [UIColor whiteColor];
        _messageLabel.font = [UIFont systemFontOfSize:22];
    }
    return _messageLabel;
}

- (UILabel *)messageLabel2
{
    if (!_messageLabel2) {
        _messageLabel2 = [[UILabel alloc]init];
        _messageLabel2.textColor = [UIColor whiteColor];
        _messageLabel2.font = [UIFont systemFontOfSize:18];
        _messageLabel2.text = @"阅读奖励";
    }
    return _messageLabel2;
}

- (UIImageView *)imageView
{
    if (!_imageView) {
        _imageView = [[UIImageView alloc]init];
    }
    return _imageView;
}

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

@end
