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

#import "GMFaceBaseVC.h"
#import "GMFaceV3Model.h"
#import "GMTitleView.h"
#import "GMDrawDottedView.h"
#import "GMImageView+GMFace.h"

#define AnimationDuration 0.35  // 动画执行时间
#define TitleToTopHight [OCNavigationBar barHeight] + 101
#define Blue_line [UIColor colorWithHexString:@"0x4EC4CD" alpha:0.6]

@interface GMFaceBaseVC ()

@end

@implementation GMFaceBaseVC

// 面部分析黄金三角文字展示
-(void)creatFourWord:(NSArray *)triangleArray addModel:(GMFaceCommonContentModel *)model {
    GMFaceCommonPositionModel *triangleL = triangleArray[0];                   // 获取黄金三角最后一个点
    GMFaceCommonPositionModel *triangleR = triangleArray[1];                   // 获取黄金三角最后一个点
    GMFaceCommonPositionModel *triangleP = triangleArray[2];                   // 获取黄金三角最后一个点
    // 添加黄金三角文字
    NSString *title = [NSString stringWithFormat:@"%@\n%@",model.title,model.value];
    CGRect titleSize = CGRectMake(triangleP.x - 40, triangleP.y - 60, 80, 35);
    GMLabel *dectLabel0 = [self creatLabelWithText:title Font:9 Color:UIColor.whiteColor Size:titleSize];
    dectLabel0.numberOfLines = 2;
    dectLabel0.alpha = 0.0;
    [self.allViewLine addObject:dectLabel0];
    // 添加 L 文字
    CGRect LSize = CGRectMake(triangleL.x - 15, triangleL.y - 6, 12, 12);
    GMLabel *dectLabelL = [self creatLabelWithText:@"L" Font:12 Color:UIColor.whiteColor Size:LSize];
    dectLabelL.alpha = 0.0;
    [self.allViewLine addObject:dectLabelL];
    // 添加 R 文字
    CGRect RSize = CGRectMake(triangleR.x + 3, triangleR.y - 6, 12, 12);
    GMLabel *dectLabelR = [self creatLabelWithText:@"R" Font:12 Color:UIColor.whiteColor Size:RSize];
    dectLabelR.alpha = 0.0;
    [self.allViewLine addObject:dectLabelR];
    // 添加 P 文字
    CGRect PSize = CGRectMake(triangleP.x - 6, triangleP.y + 3, 12, 12);
    GMLabel *dectLabelP = [self creatLabelWithText:@"P" Font:12 Color:UIColor.whiteColor Size:PSize];
    dectLabelP.alpha = 0.0;
    [self.allViewLine addObject:dectLabelP];
    [UIView animateWithDuration:AnimationDuration animations:^{
        dectLabel0.alpha = 1.0;
        dectLabelL.alpha = 1.0;
        dectLabelR.alpha = 1.0;
        dectLabelP.alpha = 1.0;
    }];
}

// 面部分析 画下巴线条
-(void)addChinLine:(NSArray *)chinArray descModel:(GMFaceCommonContentModel *)descModel{
    GMFaceCommonPositionModel *point0 = chinArray[1];           // 中心点
    GMFaceCommonPositionModel *point1 = chinArray.firstObject;  // 左侧点
    GMFaceCommonPositionModel *point2 = chinArray.lastObject;   // 右侧点
    // 延长线条
    CGPoint leftEndPoint = CGPointMake(point0.x - 2*(point0.x - point1.x), point0.y - 2*( point0.y - point1.y));
    CGPoint rightEndPoint = CGPointMake(point0.x + 2*(point2.x-point0.x), point0.y - 2*( point0.y - point2.y));
    GMFaceCommonPositionModel *leftModel = [GMFaceCommonPositionModel creatFaceCommonModelWithCGPoint:leftEndPoint];
    GMFaceCommonPositionModel *rightModel = [GMFaceCommonPositionModel creatFaceCommonModelWithCGPoint:rightEndPoint];
    // 添加下巴两天线
    CALayer *layer1 = [self drawLineMethod:@[point0,leftModel] Color:Blue_line];
    CALayer *layer2 = [self drawLineMethod:@[point0,rightModel] Color:Blue_line];
    [self.allViewLine addObject:layer1];
    [self.allViewLine addObject:layer2];
    // 添加下巴角度
    //    GMFaceCommonContentModel *lastContentModel = contentArray.lastObject;
    GMLabel *dectLabel1 = [self creatLabelWithText:descModel.value Font:9 Color:UIColor.whiteColor Size:CGRectMake(point0.x - 30, point0.y - 20, 60, 15)];
    [self.allViewLine addObject:dectLabel1];
}


// 面部分析 画竖线加描述文字
- (void)creatVorticalDescLine:(NSArray *)pModelArray content:(GMFaceCommonContentModel *)descModel offset:(CGFloat)offsetX{
    GMFaceCommonPositionModel* linePointModel0 = pModelArray[0];
    GMFaceCommonPositionModel* linePointModel1 = pModelArray[1];
    NSString *descStr = [NSString stringWithFormat:@"%@%@",descModel.title,descModel.value];
    CGSize descSize = [descStr sizeWithAttributes:@{NSFontAttributeName:[UIFont gmFont:9]}];
    GMDrawDottedView *lineView = [[GMDrawDottedView alloc] initWithLineType:drawViewTypeVerticalDotted addlabel:wordPlaceRight addDect:descStr];
    lineView.backgroundColor = UIColor.clearColor;
    lineView.alpha = 0;
    lineView.frame = CGRectMake(linePointModel0.x + offsetX, linePointModel0.y, descSize.width + 10, linePointModel1.y - linePointModel0.y);
    [self.view addSubview:lineView];
    [self.allViewLine addObject:lineView];
    [UIView animateWithDuration:AnimationDuration animations:^{
        lineView.alpha = 1.0;
    }];
}

// 面部分析 画四条横线
- (void)creatHorizontalLine:(NSMutableArray *)pointArray {
    __weak typeof (self) weakSelf = self;
    NSArray *lineWidth = @[@(82),@(82),@(175),@(175),@(284),@(284)];
    [pointArray enumerateObjectsUsingBlock:^(GMFaceCommonPositionModel * _Nonnull point, NSUInteger idx, BOOL * _Nonnull stop) {
        GMImageView *lineImage = [GMImageView creatHorizontalPathWithY:point.y];
        lineImage.centerX = point.x;
        [weakSelf.view addSubview:lineImage];
        [UIView animateWithDuration:AnimationDuration animations:^{
            lineImage.width = [lineWidth[idx] floatValue];
            lineImage.centerX = point.x;
        }];
        [weakSelf.allViewLine addObject:lineImage];
    }];
}

#pragma mark - 第五个页面动画 - 画线通用方法
// 眉眼分析 - 四条虚线
-(void)fiveViewdrawDottodlineWithEye:(NSArray<GMFaceCommonPositionModel *> *)eyeArray eyebrow:(NSArray<GMFaceCommonPositionModel *> *)eyebroArray modelAry:(NSArray *)modelAry {
    ALFaceRectModel *eye = [ALFaceRectModel creatRectModelWithTop:eyeArray[1].y right:eyeArray[2].x left:eyeArray[0].x bottom:eyeArray[3].y];
    ALFaceRectModel *eyebrow = [ALFaceRectModel creatRectModelWithTop:eyebroArray[1].y right:eyebroArray[2].x left:eyebroArray[0].x bottom:eyebroArray[3].y];
    // 画虚线框
    NSArray *type = @[@(0),@(1),@(0),@(1)];
    NSArray *pleac = @[@(wordPlaceLeft),@(wordPlaceTop),@(wordPlaceRight),@(wordPlaceBottom)];
    for (int i = 0; i < modelAry.count; i++ ) {
        GMFaceCommonContentModel *model = modelAry[i];
        NSString *dect = [NSString stringWithFormat:@"%@\n%@",model.title,model.value];
        CGSize dectSize = [dect sizeWithAttributes:@{NSFontAttributeName:[UIFont gmFont:9]}];
        // 文字在中间
        GMDrawDottedView *drawHView = [[GMDrawDottedView alloc] initWithLineType:[type[i] intValue]  addlabel:[pleac[i] intValue] addDect:dect];
        drawHView.backgroundColor = [UIColor clearColor];
        CGRect frame;
        if (i == 0) {
            frame = CGRectMake(eyebrow.left - dectSize.width - 15, eyebrow.top, dectSize.width + 10, eyebrow.bottom - eyebrow.top);
        }else if (i == 1) {
            frame = CGRectMake(eyebrow.left, eyebrow.top - dectSize.height - 15, eyebrow.right - eyebrow.left, dectSize.height + 10);
        }else if (i == 2){
            frame = CGRectMake(eye.right + 5, eye.top, dectSize.width + 10, eye.bottom -eye.top);
        }else{
            frame = CGRectMake(eye.left, eye.bottom + 5, eye.right - eye.left, dectSize.height + 10);
        }
        drawHView.frame = frame;
        drawHView.alpha = 0;
        [self.view addSubview:drawHView];
        [UIView animateWithDuration:AnimationDuration animations:^{
            drawHView.alpha = 1;
        }];
        [self.allViewLine addObject:drawHView];
    }
}
// 眉眼分析 - 横线和竖线
- (void)fiveViewdrawlineWithEye:(NSArray<GMFaceCommonPositionModel *> *)eyeNewPoint eyebrow:(NSArray<GMFaceCommonPositionModel *> *)eyebrowNewPoint {
    NSArray *HLinePointArray = @[eyebrowNewPoint[1],eyebrowNewPoint[3],eyeNewPoint[1],eyeNewPoint[3]];
    NSArray *VLinePointArray = @[eyebrowNewPoint[0],eyebrowNewPoint[2],eyeNewPoint[0],eyeNewPoint[2]];
    // 获取矩形框的宽和高
    CGSize eyebrowSize = [self getWidthAddHightWithRect:eyebrowNewPoint];
    CGSize eyeSize = [self getWidthAddHightWithRect:eyeNewPoint];
    // 画四条横线
    NSArray *widthArray = @[@(eyebrowSize.width),@(eyebrowSize.width),@(eyeSize.width),@(eyeSize.width)];
    [HLinePointArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        GMFaceCommonPositionModel *model = (GMFaceCommonPositionModel *)obj;
        GMImageView *himageView = [GMImageView creatHorizontalPathWithY:model.y];
        himageView.centerX = model.x;
        himageView.alpha = 0;
        [UIView animateWithDuration:AnimationDuration animations:^{
            himageView.width = [widthArray[idx] floatValue] + 30;
            himageView.alpha = 1;
            himageView.centerX = model.x;
        }];
        [self.view addSubview:himageView];
        [self.allViewLine addObject:himageView];
    }];
    // 画四条竖线
    NSArray *hightArray = @[@(eyebrowSize.height),@(eyebrowSize.height),@(eyeSize.height),@(eyeSize.height)];
    [VLinePointArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        GMFaceCommonPositionModel *model = (GMFaceCommonPositionModel *)obj;
        GMImageView *vimageView = [GMImageView creatVerticalPathWithX:model.x];
        vimageView.centerY = model.y;
        [self.view addSubview:vimageView];
        [UIView animateWithDuration:AnimationDuration animations:^{
            vimageView.height = [hightArray[idx] floatValue] + 30;;
            vimageView.centerY =  model.y;
        }];
        [self.allViewLine addObject:vimageView];
    }];
}


#pragma mark - 第六个页面动画 - 画线通用方法

- (void)drawDottodLineWithPointArray:(NSArray<GMFaceCommonPositionModel *> *)pointArray content:(GMFaceCommonContentModel *)contentModel type:(GMLineType)lineType place:(GMWordPlace)place {
    NSString *dect = [NSString stringWithFormat:@"%@\n%@",contentModel.title,contentModel.value];
    CGSize dectSize = [dect sizeWithAttributes:@{NSFontAttributeName:[UIFont gmFont:9]}];
    GMDrawDottedView *drawHView = [[GMDrawDottedView alloc] initWithLineType:lineType addlabel:place addDect:dect];
    drawHView.backgroundColor = [UIColor clearColor];
    CGRect frame;
    if (lineType == drawViewTypeHorizontalDotted) {
        if (place == wordPlaceTop) {
            frame = CGRectMake(pointArray[0].x, (pointArray[0].y - dectSize.height - 10),pointArray[1].x - pointArray[0].x, dectSize.height + 10);
        }else{
            frame = CGRectMake(pointArray[0].x, (pointArray[0].y - 10),pointArray[1].x - pointArray[0].x, dectSize.height + 10);
        }
    }else{
        frame = CGRectMake(pointArray[0].x + 5, pointArray[0].y, dectSize.width + 10, pointArray[1].y - pointArray[0].y);
    }
    drawHView.frame = frame;
    drawHView.alpha = 0;
    [self.view addSubview:drawHView];
    [UIView animateWithDuration:AnimationDuration animations:^{
        drawHView.alpha = 1;
    }];
    [self.allViewLine addObject:drawHView];
}

- (void)drawSixhLine:(NSArray<GMFaceCommonPositionModel *> *)pointXArray widthArray:(NSArray<NSNumber *> *)widthArray {
    for (int i = 0; i < pointXArray.count; i++) {
        GMFaceCommonPositionModel *model = pointXArray[i];
        GMImageView *vimageView = [GMImageView creatHorizontalPathWithY:model.y];
        vimageView.centerX = model.x;
        [self.view addSubview:vimageView];
        [UIView animateWithDuration:AnimationDuration animations:^{
            vimageView.width = [widthArray[i] floatValue];
            vimageView.centerX = model.x;
        }];
        [self.allViewLine addObject:vimageView];
    }
}

- (void)drawSixVerticalLine:(NSArray<GMFaceCommonPositionModel *> *)pointXArray hightArray:(NSArray<NSNumber *> *)hightArray {
    for (int i = 0; i < pointXArray.count; i++) {
        GMFaceCommonPositionModel *model = pointXArray[i];
        GMImageView *vimageView = [GMImageView creatVerticalPathWithX:model.x];
        vimageView.centerY = model.y;
        [self.view addSubview:vimageView];
        [UIView animateWithDuration:AnimationDuration animations:^{
            vimageView.height = [hightArray[i] floatValue];
            vimageView.centerY =  model.y;
        }];
        [self.allViewLine addObject:vimageView];
    }
}


#pragma mark - 公用方法

/*
创建带描述文字的竖线 文字在右侧
@param contentModel 描述文字
@param pointArray 画线的点位传两个点过来
 */
-(void)creatVerticalDottedLineWith:(GMFaceCommonContentModel *)contentModel addArray:(NSArray *)pointArray offset:(CGFloat)offset{
    GMFaceCommonPositionModel *model0 = pointArray[0];
    GMFaceCommonPositionModel *model1 = pointArray[1];
    NSString *nbqkString = [NSString stringWithFormat:@"%@%@",contentModel.title,contentModel.value];
    CGSize nbqkStringSize = [nbqkString sizeWithAttributes:@{NSFontAttributeName:[UIFont gmFont:9]}];
    GMDrawDottedView *nbqkDottedLine = [[GMDrawDottedView alloc] initWithLineType:drawViewTypeVerticalDotted addlabel:wordPlaceRight addDect:nbqkString];
    nbqkDottedLine.backgroundColor = UIColor.clearColor;
    nbqkDottedLine.alpha = 0;
    nbqkDottedLine.frame = CGRectMake(model0.x + offset, model0.y, nbqkStringSize.width + 10, model1.y - model0.y);
    [self.view addSubview:nbqkDottedLine];
    [self.allViewLine addObject:nbqkDottedLine];
    [UIView animateWithDuration:AnimationDuration animations:^{
        nbqkDottedLine.alpha = 1;
    }];
}
// 计算两点之间的水平宽度
-(CGFloat)calculateWidthWithArray:(NSArray*)array{
    GMFaceCommonPositionModel *model0 = array[0];
    GMFaceCommonPositionModel *model1 = array[1];
    return model1.x  -  model0.x;
}

// 返回中心点model
-(GMFaceCommonPositionModel *)calculateCenterModelWithArray:(NSArray*)array{
    GMFaceCommonPositionModel *centerModel = [GMFaceCommonPositionModel new];
    GMFaceCommonPositionModel *model0 = array[0];
    GMFaceCommonPositionModel *model1 = array[1];
    centerModel.x = (model0.x + model1.x) / 2;
    centerModel.y = (model0.y + model1.y) / 2;
    return centerModel;
}

// 更新背景图片的尺寸
- (void)updatePicImageViewframeP {
    [UIView animateWithDuration:AnimationDuration animations:^{
        self.picImageView.frame = self.view.frame;
    }];
}
// 给一个矩形框，返回一个宽和高
- (CGSize)getWidthAddHightWithRect:(NSArray<GMFaceCommonPositionModel *> *)rectArray{
    NSAssert(rectArray.count == 4, @"这个数组必须传过来四个点");
    CGFloat rectWidth = rectArray[2].x - rectArray[0].x;
    CGFloat recthight = rectArray[3].y - rectArray[1].y;
    return CGSizeMake(rectWidth, recthight);
}

// 计算中心点坐标
-(NSArray *)midlePointWithModel:(ALFaceRectModel *)model {
    GMFaceCommonPositionModel *midleLeft = [GMFaceCommonPositionModel creatFaceCommonModelWithX:model.left addY:(model.bottom - model.top) / 2 + model.top];
    GMFaceCommonPositionModel *midleTop = [GMFaceCommonPositionModel creatFaceCommonModelWithX:(model.right - model.left) / 2 + model.left addY:model.top];
    GMFaceCommonPositionModel *midleRight = [GMFaceCommonPositionModel creatFaceCommonModelWithX:model.right addY:(model.bottom - model.top) / 2 + model.top];
    GMFaceCommonPositionModel *midleBottom = [GMFaceCommonPositionModel creatFaceCommonModelWithX:(model.right - model.left) / 2 + model.left addY:model.bottom];
    return [NSArray arrayWithObjects:midleLeft,midleTop,midleRight,midleBottom, nil];
}

// 计算放大后的点位置
-(NSMutableArray *)bigImagePointWithScale:(CGFloat)scale offsetX:(CGFloat)offsetX offsetY:(CGFloat)offsetY poinyArray:(NSArray *)pointArray {
    NSMutableArray *newPointArray = [NSMutableArray array];
    [pointArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        GMFaceCommonPositionModel *point = (GMFaceCommonPositionModel *)obj;
        GMFaceCommonPositionModel *newPoint = [GMFaceCommonPositionModel new];
        newPoint.x = point.x * scale + offsetX;
        newPoint.y = point.y * scale + offsetY;
        [newPointArray addObject:newPoint];
    }];
    return newPointArray;
}

// 左侧描述弹出框方法封装
- (void)leftPopTitleWith:(NSMutableArray *)viewLineArray AddSubtitle:(NSArray *)subTitle Animation:(CGFloat)duration {
    __weak typeof (self) weakSelf = self;
    for (int i = 0; i < subTitle.count; i++) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((duration + 0.15 * i) *  NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (weakSelf.stopGCD) { return ;}
            GMFaceCommonSubtitleModel *subTitleModel = subTitle[i];
            CGFloat sumHight = TitleToTopHight;
            for (int j = 0; j < i; j ++) {
                GMFaceCommonSubtitleModel *titleModel = subTitle[j];
                sumHight += titleModel.hight + 20;
            }
            GMTitleView *tView = [weakSelf addDectTitleNameWithModel:subTitleModel viewFrame:CGRectMake(-120, sumHight, 120, subTitleModel.hight)];
            [viewLineArray addObject:tView];
        });
    }
}

// 创建黄金三角类似的描述文字
- (GMLabel *)creatLabelWithText:(NSString *)text Font:(CGFloat)font Color:(UIColor *)color Size:(CGRect)frame {
    GMLabel *label = [GMLabel labelWithTextAlignment:NSTextAlignmentCenter backgroundColor:UIColor.clearColor textColor:color fontSize:font];
    label.shadowColor = [UIColor colorWithHexString:@"000000" alpha:0.3];
    label.shadowOffset = CGSizeMake(0, 1);
    label.layer.shadowRadius = 0.5;
    label.frame = frame;
    label.text = text;
    [self.view addSubview:label];
    return label;
}

// 添加描述文字
- (GMTitleView *)addDectTitleNameWithModel:(GMFaceCommonSubtitleModel *)tempModel viewFrame:(CGRect)frame {
    GMTitleView *tView = [[GMTitleView alloc] initWithModel:tempModel];
    tView.alpha = 0;
    tView.backgroundColor = [UIColor clearColor];
    tView.frame = frame;
    [self.view addSubview:tView];
    [UIView animateWithDuration:AnimationDuration animations:^{
        tView.transform = CGAffineTransformMakeTranslation(135, 0);
        tView.alpha = 1;
    }];
    return tView;
}

// 画线方法
- (CAShapeLayer *)drawLineMethod:(NSArray<GMFaceCommonPositionModel *>*)positionModelArray Color:(UIColor *)color{
    UIBezierPath *path = [UIBezierPath bezierPath];
    [positionModelArray enumerateObjectsUsingBlock:^(GMFaceCommonPositionModel *value, NSUInteger idx, BOOL * _Nonnull stop) {
        GMFaceCommonPositionModel *pointModel = value;
        if (idx == 0) {
            [path moveToPoint:CGPointMake(pointModel.x, pointModel.y)];
        }else {
            [path addLineToPoint:CGPointMake(pointModel.x, pointModel.y)];
        }
    }];
    // 创建CAShapeLayer
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.lineWidth = 1.0f;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = color.CGColor;
    [self.view.layer addSublayer:layer];
    layer.path = path.CGPath;
    // 创建Animation
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.fromValue = @(0.0);
    animation.toValue = @(1.0);
    layer.autoreverses = NO;
    animation.duration = 0.75f;
    //     设置layer的animation
    [layer addAnimation:animation forKey:nil];
    return layer;
}

// 清除页面上的线条
- (void)clearFristViewLineView {
    for (id viewOrLayer in self.allViewLine) {
        if ([viewOrLayer isKindOfClass:[UIView class]]) {
            UIView *view = (UIView *)viewOrLayer;
            [UIView animateWithDuration:AnimationDuration animations:^{
                view.alpha = 0;
            } completion:^(BOOL finished) {
                [view removeAllSubviews];
            }];
        }else if([viewOrLayer isKindOfClass:[CALayer class]]){
            CALayer *layer = (CALayer *)viewOrLayer;
            [CATransaction begin];
            [CATransaction setAnimationDuration:AnimationDuration];
            layer.opacity = 0.0;
            [CATransaction commit];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(AnimationDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [layer removeFromSuperlayer];
            });
        }
    }
}

-(NSMutableArray *)allViewLine{
    if (!_allViewLine) {
        _allViewLine = [NSMutableArray array];
    }
    return _allViewLine;
}

@end
