//
//  GMGeneMemoryAnimationViewController.m
//  Gengmei
//
//  Created by 朱璇 on 2019/12/6.
//  Copyright © 2019 更美互动信息科技有限公司. All rights reserved.
//

#import "GMGeneMemoryAnimationViewController.h"
#import "ALFaceInfoViewModel.h"
#import "UIImage+GMExtension.h"
#import "GMFaceV3Util.h"
#import "NSString+jsonData.h"
#import "GMFacePointViewController.h"
//#import "Lottie.h"
@import Lottie;

#define centerPoint CGPointMake(MAINSCREEN_WIDTH / 2, MAINSCREEN_HEIGHT / 2)
#define fromRadius sqrtf((centerPoint.x * centerPoint.x) + (centerPoint.y * centerPoint.y))
#define toRadius (92.0 / 375 * MAINSCREEN_WIDTH)
#define circleRadius (80.0 / 375 * MAINSCREEN_WIDTH)
#define showAnimaitionTime .35
#define hideAnimaitionTime .35
#define circleAnimaitionTime 2


@interface GMGeneMemoryAnimationViewController () {
    UIImageView *_bgImage;
    UIImageView *_photoImage;
    CAShapeLayer *_maskLayer;
}
@property (nonatomic, strong) LOTAnimationView *animationView;

@end

@implementation GMGeneMemoryAnimationViewController

-(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];
    self.extraParam = [NSString jsonStringWithObject:@{@"land_tab_name" : @"AI面孔"}];
    [self setUp];

}

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

- (void)setUp {
    UIImage *newImage = [self.image normalizedImage]; // 防止iOS 13 图片自动旋转90度
    
    // 上传图片 并请求测试结果
    __weak __typeof(self)weakSelf = self;
    [GMFaceV3Util uploadOneImageToQiNiu:newImage gender:self.gender vc:self successBlock:^(id responseObject) {
        [weakSelf startHidePhotoAnimation];
        [weakSelf performSelector:@selector(pushToNextVC:) withObject:responseObject afterDelay:.8];
    }];
    
    _bgImage = [[UIImageView alloc] init];
    _bgImage.image = [UIImage imageNamed:@"gene_loading_bg"];
    _bgImage.contentMode = UIViewContentModeScaleAspectFill;
    _bgImage.frame = self.view.frame;
    [self.view addSubview:_bgImage];
    
    
    _maskLayer = [CAShapeLayer layer];
    _maskLayer.bounds = self.view.bounds;
    _maskLayer.fillColor = [UIColor greenColor].CGColor; // Any color but clear will be OK
    _maskLayer.path = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:fromRadius startAngle:0 endAngle:2 * M_PI clockwise:YES].CGPath;
    _maskLayer.opacity = 1;
    _maskLayer.position = centerPoint;
    
    _photoImage = [[UIImageView alloc] init];
    _photoImage.contentMode = UIViewContentModeScaleAspectFill;
    _photoImage.frame = self.view.frame;
    _photoImage.image = newImage;
    _photoImage.layer.mask = _maskLayer;
    [self.view addSubview:_photoImage];
    
}

- (LOTAnimationView *)animationView {
    if (!_animationView) {
        _animationView = [LOTAnimationView animationNamed:@"gene_loading"];
        _animationView.contentMode = UIViewContentModeScaleAspectFit;
        _animationView.loopAnimation = YES;
        _animationView.frame = CGRectMake(0, 85, 177, 60);
        _animationView.centerX = centerPoint.x;
        [self.view addSubview:_animationView];
    }
    return _animationView;
}

- (void)removeAnimation {
    [_maskLayer removeAllAnimations];
    [_animationView stop];
}

- (void)startShowPhotoAnimation {
    CGPathRef fromPath = _maskLayer.path;
    CGPathRef toPath = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:toRadius startAngle:0 endAngle:2 * M_PI clockwise:YES].CGPath;
    
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    pathAnimation.fromValue = (__bridge id _Nullable)(fromPath);
    pathAnimation.toValue = (__bridge id _Nullable)(toPath);
    pathAnimation.duration = showAnimaitionTime;
    _maskLayer.path = toPath;
    [_maskLayer addAnimation:pathAnimation forKey:@"path"];
    [self performSelector:@selector(startCircleAnimation) withObject:nil afterDelay:showAnimaitionTime];
}

- (void)startCircleAnimation {
    // 开始头部动画
    [self.animationView playFromProgress:0 toProgress:1 withCompletion:^(BOOL animationFinished) {}];

    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
     pathAnimation.calculationMode = kCAAnimationCubicPaced;
     pathAnimation.fillMode = kCAFillModeForwards;
     pathAnimation.removedOnCompletion = NO;
     pathAnimation.duration = circleAnimaitionTime;
     pathAnimation.repeatCount = OPEN_MAX;
    CGMutablePathRef curvedPath = CGPathCreateMutable();
    //center为圆心,circleRadius为半径 （startAngle，endAngle）为起始角度和结束角度，0为顺时针，1 为逆时针
    CGPathAddArc(curvedPath, NULL, centerPoint.x + circleRadius / 2, centerPoint.y, circleRadius / 2, M_PI, 2 * M_PI, 0);
    CGPathAddArc(curvedPath, NULL, centerPoint.x, centerPoint.y, circleRadius, 0, M_PI, 0);
    CGPathAddArc(curvedPath, NULL, centerPoint.x - circleRadius / 2, centerPoint.y, circleRadius / 2, M_PI, 2 * M_PI, 0);

    pathAnimation.path = curvedPath;
    CGPathRelease(curvedPath);

    [_maskLayer addAnimation:pathAnimation forKey:@"position"];
}

- (void)startHidePhotoAnimation {
    // 删除动画
    [self.animationView stop];
    [self.animationView removeFromSuperview];
    
    CGPathRef fromPath = _maskLayer.path;
    CGPathRef toPath = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:fromRadius * 2 startAngle:0 endAngle:2 * M_PI clockwise:YES].CGPath;
    
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    pathAnimation.fromValue = (__bridge id _Nullable)(fromPath);
    pathAnimation.toValue = (__bridge id _Nullable)(toPath);
    pathAnimation.duration = hideAnimaitionTime;
    _maskLayer.path = toPath;
    [_maskLayer addAnimation:pathAnimation forKey:@"path"];
}

- (void)pushToNextVC:(id)objcet {
    GMFaceV3Model *faceModel = (GMFaceV3Model *)objcet;
    GMFacePointViewController *pointVc = [[GMFacePointViewController alloc] initWithModel:faceModel];
    pointVc.isGene = YES;
    [self.navigationController pushViewController:pointVc animated:YES];
}

@end
