//
//  CarRefreshHeadView.m
//  HaoChe-App-iOS
//
//  Created by Sunyi on 16/10/19.
//  Copyright © 2016年 Fuzhou Qiankunhaoche E-commerce Technology. All rights reserved.
//

#import "CarRefreshHeadView.h"
#import "XTSystem.h"

@interface CarRefreshHeadView ()

@property (nonatomic, strong) UIView *centerView;

@end

@implementation CarRefreshHeadView

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

//- (void)startAnimation{
//    [self startAnimationCarRefreshBackgroundType:kCarRefreshBackgroundTypeBlack];
//}


- (void)startAnimationCarRefreshBackgroundType:(CarRefreshBackgroundType)groundType{
    
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.duration = 1;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = NSIntegerMax;
    rotationAnimation.removedOnCompletion = NO;
    
    [self.mj_OneShoeView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    [self.mj_TowShoeView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation1"];
    
    
    
    CABasicAnimation* rotationAnimation2;
    rotationAnimation2 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation2.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation2.duration = 3;
    rotationAnimation2.cumulative = YES;
    rotationAnimation2.repeatCount = NSIntegerMax;
    rotationAnimation2.removedOnCompletion = NO;
    
    switch (groundType) {
        case kCarRefreshBackgroundTypeBlack:{
            [self.mj_SunView setImage:[UIImage imageNamed:@"mj_sun"]];
            [self.mj_AirView setImage:[UIImage imageNamed:@"mj_air"]];
        }break;
        case kCarRefreshBackgroundTypeWhite:{
            [self.mj_SunView setImage:[UIImage imageNamed:@"mj_sun_black"]];
            [self.mj_AirView setImage:[UIImage imageNamed:@"mj_air_black"]];
        }break;
        default:
            break;
    }
    
    [self.mj_SunView.layer addAnimation:rotationAnimation2 forKey:@"rotationAnimation2"];
    
    if (self.centerView == nil) {
        self.centerView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2 - 70, 42/2 - 8, 142, 25)];
        [self.centerView setClipsToBounds:YES];
        [self.centerView setBackgroundColor:[UIColor clearColor]];
        [self insertSubview:self.centerView atIndex:0];
        
        switch (groundType) {
            case kCarRefreshBackgroundTypeBlack:{
                self.mj_HouseView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mj_house"]];
                [self.mj_HouseView setFrame:CGRectMake(0, 0, 142, 25)];
                [self.centerView addSubview:self.mj_HouseView];
                
                self.mj_HouseView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mj_house"]];
                [self.mj_HouseView1 setFrame:CGRectMake(142, 0, 142, 25)];
                [self.centerView addSubview:self.mj_HouseView1];
            }break;
            case kCarRefreshBackgroundTypeWhite:{
                self.mj_HouseView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mj_house_black"]];
                [self.mj_HouseView setFrame:CGRectMake(0, 0, 142, 25)];
                [self.centerView addSubview:self.mj_HouseView];
                
                self.mj_HouseView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mj_house_black"]];
                [self.mj_HouseView1 setFrame:CGRectMake(142, 0, 142, 25)];
                [self.centerView addSubview:self.mj_HouseView1];
            }break;
            default:
                break;
        }
      
        
    }
    
    CABasicAnimation *animation =
    [CABasicAnimation animationWithKeyPath:@"position"];
    animation.duration = 3; // 动画持续时间
    animation.repeatCount = NSIntegerMax; // 不重复
    animation.autoreverses = NO; // 结束后执行逆动画
    animation.removedOnCompletion = NO;
    
    if([XTSystem isPhoneRetina35] || [XTSystem isPhoneRetina4]){
        animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2 - self.centerView.frame.size.width/2 - 30, 12)]; // 起始点
        animation.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2 - self.mj_HouseView.frame.size.width/2 - self.mj_HouseView.frame.size.width - 30, 12)];

    }
    
    if([XTSystem isPhoneRetina6]){
        animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2 - self.centerView.frame.size.width/2 - 45, 12)]; // 起始点
        animation.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2 - self.mj_HouseView.frame.size.width/2 - self.mj_HouseView.frame.size.width - 45, 12)];
    }
    
    if([XTSystem isPhoneRetina6Plus]){
        animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2 - self.centerView.frame.size.width/2 - 60, 12)]; // 起始点
        animation.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2 - self.mj_HouseView.frame.size.width/2 - self.mj_HouseView.frame.size.width - 60, 12)];
    }
    
    [self.mj_HouseView.layer addAnimation:animation forKey:@"rotate-layer"];
    
    CABasicAnimation *animation1 =
    [CABasicAnimation animationWithKeyPath:@"position"];
    animation1.duration = 3; // 动画持续时间
    animation1.repeatCount = NSIntegerMax; // 不重复
    animation1.autoreverses = NO; // 结束后执行逆动画
    animation1.removedOnCompletion = NO;
    if([XTSystem isPhoneRetina35] || [XTSystem isPhoneRetina4]){
        animation1.fromValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2 + self.mj_HouseView1.frame.size.width/2 - 30 , 12)]; // 起始点
        animation1.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2 - self.mj_HouseView1.frame.size.width/2 - 30, 12)];
    }
    
    if([XTSystem isPhoneRetina6]){
        animation1.fromValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2 + self.mj_HouseView1.frame.size.width/2 - 45 , 12)]; // 起始点
        animation1.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2 - self.mj_HouseView1.frame.size.width/2 - 45, 12)];
    }
    
    if([XTSystem isPhoneRetina6Plus]){
        animation1.fromValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2 + self.mj_HouseView1.frame.size.width/2 - 60 , 12)]; // 起始点
        animation1.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2 - self.mj_HouseView1.frame.size.width/2 - 60, 12)];
    }
    
    [self.mj_HouseView1.layer addAnimation:animation1 forKey:@"rotate-layer1"];
    
    
    CABasicAnimation *animationScale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    // 动画选项设定
    animationScale.duration = 2; // 动画持续时间
    animationScale.repeatCount = NSIntegerMax; // 重复次数
    animationScale.fromValue = [NSNumber numberWithFloat:0.4]; // 开始时的倍率
    animationScale.toValue = [NSNumber numberWithFloat:1]; // 结束时的倍率
    animationScale.removedOnCompletion = NO;
    
    // 添加动画
    [self.mj_AirView.layer addAnimation:animationScale forKey:@"scale-layer"];

    
    /* 移动 */
    CABasicAnimation *animation3 = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
    
    // 起止点的设定
    animation3.duration = 2; // 动画持续时间
    animation3.toValue = [NSNumber numberWithFloat: - 5];; // 終点
    animation3.repeatCount = NSIntegerMax; // 重复次数
    animation3.removedOnCompletion = NO;
    [self.mj_AirView.layer addAnimation:animation3 forKey:@"move-layer"];
    
    
    CABasicAnimation *animation4 = [CABasicAnimation animationWithKeyPath:@"opacity"];
    animation4.duration = 2; // 动画持续时间
    animation4.fromValue = [NSNumber numberWithFloat:1.0];
    animation4.toValue = [NSNumber numberWithFloat:0.0];
    animation4.repeatCount = NSIntegerMax; // 重复次数
    animation4.removedOnCompletion = NO;
    [self.mj_AirView.layer addAnimation:animation4 forKey:@"opacity-layer"];
    
    
    
}

@end