//
//  GMScanAnimationController.m
//  Gengmei
//
//  Created by 刘鹿杰 on 2019/6/19.
//  Copyright © 2019 更美互动信息科技有限公司. All rights reserved.
//

#import "GMScanAnimationController.h"
#import "ALFaceInfoViewModel.h"
#import "UIImage+GMExtension.h"
#import "GMFaceV3Util.h"

@interface GMScanAnimationController ()
@end

@implementation GMScanAnimationController

-(void)initController{
    [super initController];
    self.navigationBar.backgroundColor = UIColor.clearColor;
    self.navigationBar.titleLabel.textColor = UIColor.whiteColor;
    self.navigationBar.leftIcon = @"NewFaceBack";
    self.navigationBar.isShowShadow = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImage *newImage = [self.image normalizedImage]; // 防止iOS 13 图片自动旋转90度
    GMImageView *imageView = [[GMImageView alloc] init];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.frame = self.view.frame;
    imageView.image = newImage;
    [self.view addSubview:imageView];
    
    GMImageView *line = [[GMImageView alloc] initWithFrame:CGRectMake(0, -162, MAINSCREEN_WIDTH, 162)];
    line.backgroundColor = [UIColor blueColor];
    line.tag = 1111;
    line.image = [UIImage imageNamed:@"icon_face_scanning_down"];
    line.contentMode = UIViewContentModeScaleAspectFill;
    line.backgroundColor = [UIColor clearColor];
    [self.view addSubview:line];
    [self addAnimation];
    [GMFaceV3Util uploadOneImageToQiNiu:newImage vc:self successBlock:NULL];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self removeAnimation];
}

//  添加扫描动画
- (void)addAnimation{
    UIView *line = [self.view viewWithTag:1111];
    line.hidden = NO;
    CABasicAnimation *animation = [GMScanAnimationController moveYTime:2.5 fromY:[NSNumber numberWithFloat:0] toY:[NSNumber numberWithFloat:MAINSCREEN_HEIGHT] rep:OPEN_MAX];
    [line.layer addAnimation:animation forKey:@"LineAnimation"];
}

//   去除扫描动画
- (void)removeAnimation{
    UIView *line = [self.view viewWithTag:1111];
    [line.layer removeAnimationForKey:@"LineAnimation"];
    line.hidden = YES;
}

+ (CABasicAnimation *)moveYTime:(float)time fromY:(NSNumber *)fromY toY:(NSNumber *)toY rep:(int)rep{
    CABasicAnimation *animationMove = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    [animationMove setFromValue:fromY];
    [animationMove setToValue:toY];
    animationMove.duration = time;
    animationMove.repeatCount  = rep;
    animationMove.fillMode = kCAFillModeForwards;
    animationMove.removedOnCompletion = YES;
    animationMove.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    return animationMove;
}

@end
