//
//  ALTransFormFaceUtil.m
//  Gengmei
//
//  Created by Q14 on 2019/6/27.
//  Copyright © 2019 更美互动信息科技有限公司. All rights reserved.
//

#import "ALTransFormFaceUtil.h"
#import "ALFaceInfoViewModel.h"

//static CGAffineTransform _transform;

@implementation ALTransFormFaceUtil
+ (GMFaceV3Model *)zoomFaceImagelScreenWidth:(GMFaceV3Model *)faceModel {
    UIImage *image = faceModel.image;
    //原图片的高度
    CGFloat imageW = image.size.width;
    CGFloat imageH = image.size.height;
    //宽高比
    CGFloat scale = imageW / imageH;
    
    //显示在当前屏幕的高度 imageH
    CGFloat currentImageH = MAINSCREEN_WIDTH / scale;
    faceModel.imageX = 0.f;
    faceModel.imageY = 0.f;
    faceModel.imageW = MAINSCREEN_WIDTH;
    faceModel.imageH = currentImageH;
    //x转换需要乘的比例
    CGFloat scaleX = MAINSCREEN_WIDTH / imageW;
    //y转换需要乘的比例
    CGFloat scaleY = currentImageH / imageH;
    
    GMFaceAnalysisV3Model *analysisModel = faceModel.analysis;
    //面部特征点的转换
    GMFaceFeatureInfoModel *feature = analysisModel.featureInfo;
    feature.pointsLeft = [self scalePointsArray:feature.pointsLeft scale:scaleX scaleY:scaleY];
    feature.pointsRight = [self scalePointsArray:feature.pointsRight scale:scaleX scaleY:scaleY];
    
    // 眼部点位的转换
    GMFaceEyeEyebrowInfo *eyebrow = analysisModel.eyeEyebrowInfo;
    eyebrow.pointsEye = [self scalePointsArray:eyebrow.pointsEye scale:scaleX scaleY:scaleY];
    eyebrow.pointsEyeBrow = [self scalePointsArray:eyebrow.pointsEyeBrow scale:scaleX scaleY:scaleY];
    eyebrow.eyeRect = [self scaleRect:eyebrow.eyeRect scale:scaleX scaleY:scaleY];
    eyebrow.eyebrowRect = [self scaleRect:eyebrow.eyebrowRect scale:scaleX scaleY:scaleY];
    
    //五眼
    GMFaceSantingInfo *wuyan = analysisModel.wuyanInfo;
    wuyan.realLinePoints = [self scalePointsArray:wuyan.realLinePoints scale:scaleX scaleY:scaleY];
    wuyan.dottedPoints = [self scalePointsArray:wuyan.dottedPoints scale:scaleX scaleY:scaleY];
    //三庭
    GMFaceSantingInfo *santing = analysisModel.santingInfo;
    santing.realLinePoints = [self scalePointsArray:santing.realLinePoints scale:scaleX scaleY:scaleY];
    santing.dottedPoints = [self scalePointsArray:santing.dottedPoints scale:scaleX scaleY:scaleY];
    // 颞部颧宽
    GMFaceNiebuquankuanInfo *niebuquankuan = analysisModel.niebuquankuanInfo;
    niebuquankuan.niebuPoints = [self scalePointsArray:niebuquankuan.niebuPoints scale:scaleX scaleY:scaleY];
    niebuquankuan.quankuanPoints = [self scalePointsArray:niebuquankuan.quankuanPoints scale:scaleX scaleY:scaleY];
    
    // 人中嘴唇
    GMFaceRZZCInfoModel *rzzcModel = analysisModel.renzhongzuichunkediInfo;
    rzzcModel.renzhongPoints = [self scalePointsArray:rzzcModel.renzhongPoints scale:scaleX scaleY:scaleY];
    rzzcModel.zuichunPoints = [self scalePointsArray:rzzcModel.zuichunPoints scale:scaleX scaleY:scaleY];
    rzzcModel.kediPoints = [self scalePointsArray:rzzcModel.kediPoints scale:scaleX scaleY:scaleY];
    
    // 瞳距鼻翼宽比
    GMFaceTongJuBiModel *tongjubiyiModel = analysisModel.tongjubiyiInfo;
    tongjubiyiModel.tongjuPoints = [self scalePointsArray:tongjubiyiModel.tongjuPoints scale:scaleX scaleY:scaleY];
    tongjubiyiModel.biyiPoints = [self scalePointsArray:tongjubiyiModel.biyiPoints scale:scaleX scaleY:scaleY];
    
    // 双Cxian
    GMFaceCLineModel *shuangCxian = analysisModel.shuangCxianInfo;
    shuangCxian.pointList = [self scalePointsArray:shuangCxian.pointList scale:scaleX scaleY:scaleY];
    
    //鼻子嘴巴
    GMFaceMouthNoseInfo *mouthNose = analysisModel.mouthNoseInfo;
    mouthNose.pointsNose =  [self scalePointsArray:mouthNose.pointsNose scale:scaleX scaleY:scaleY];
    for (GMFacePointsMouthInfoModel *model in mouthNose.pointsMouth) {
        model.points = [self scalePointsArray:model.points scale:scaleX scaleY:scaleY];
    }
    mouthNose.mouthRect = [self scaleRect:mouthNose.mouthRect scale:scaleX scaleY:scaleY];
    
    // 黄金三角
    GMFaceGoldTriangleInfo *goldTriangle = analysisModel.goldTriangleInfo;
    goldTriangle.pointsChin =  [self scalePointsArray:goldTriangle.pointsChin scale:scaleX scaleY:scaleY];
    goldTriangle.pointsMouth =  [self scalePointsArray:goldTriangle.pointsMouth scale:scaleX scaleY:scaleY];
    goldTriangle.pointsTriangle =  [self scalePointsArray:goldTriangle.pointsTriangle scale:scaleX scaleY:scaleY];
    
    // 太阳穴
    GMFaceTempleInfoModel *temple = faceModel.templeInfo;
    temple.leftX *= scaleX;
    temple.rightX *= scaleX;
    temple.leftY *= scaleY;
    temple.rightY *= scaleY;
    //人脸框的转换
    faceModel.rect = [self scaleRect:faceModel.rect scale:scaleX scaleY:scaleY];
    return faceModel;
}

+ (CGAffineTransform)rollTransformImageV:(GMFaceV3Model *)faceModel {
    CGAffineTransform transform = CGAffineTransformIdentity;
    transform = CGAffineTransformRotate(transform, -(faceModel.headpose.rollAngle/180* M_PI));
    return transform;
}

+ (GMFaceV3Model *)mapFaceModelToScreencCoordinates:(GMFaceV3Model *)faceModel aroundPoint:(CGPoint)standardPoint roate:(CGAffineTransform)roateTransform scale:(CGFloat)imageScale translate:(CGPoint)translate {
    GMFaceAnalysisV3Model *analysisModel = faceModel.analysis;
    
    //面部特征点的转换
    GMFaceFeatureInfoModel *feature = analysisModel.featureInfo;
    feature.pointsLeft = [self transformCommonPositionArray:feature.pointsLeft aroundPoint: standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    feature.pointsRight = [self transformCommonPositionArray:feature.pointsRight aroundPoint: standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    
    // 眼部点位的转换
    GMFaceEyeEyebrowInfo *eyebrow = analysisModel.eyeEyebrowInfo;
    eyebrow.pointsEye = [self transformCommonPositionArray:eyebrow.pointsEye aroundPoint: standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    
    
    eyebrow.pointsEyeBrow = [self transformCommonPositionArray:eyebrow.pointsEyeBrow aroundPoint: standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    
    eyebrow.eyeRect = [self transformScaleRect:eyebrow.eyeRect aroundPoint:standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    eyebrow.eyebrowRect = [self transformScaleRect:eyebrow.eyebrowRect aroundPoint:standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    
    //五眼
    GMFaceSantingInfo *wuyan = analysisModel.wuyanInfo;
    wuyan.realLinePoints = [self transformCommonPositionArray:wuyan.realLinePoints aroundPoint: standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    wuyan.dottedPoints = [self transformCommonPositionArray:wuyan.dottedPoints aroundPoint: standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    //三庭
    GMFaceSantingInfo *santing = analysisModel.santingInfo;
    santing.realLinePoints = [self transformCommonPositionArray:santing.realLinePoints aroundPoint: standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    santing.dottedPoints = [self transformCommonPositionArray:santing.dottedPoints aroundPoint: standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    // 颞部颧宽
    GMFaceNiebuquankuanInfo *niebuquankuan = analysisModel.niebuquankuanInfo;
    niebuquankuan.niebuPoints = [self transformCommonPositionArray:niebuquankuan.niebuPoints aroundPoint: standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    niebuquankuan.quankuanPoints = [self transformCommonPositionArray:niebuquankuan.quankuanPoints aroundPoint: standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    
    // 人中嘴唇
    GMFaceRZZCInfoModel *rzzcModel = analysisModel.renzhongzuichunkediInfo;
    rzzcModel.renzhongPoints  = [self transformCommonPositionArray:rzzcModel.renzhongPoints  aroundPoint: standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    rzzcModel.zuichunPoints = [self transformCommonPositionArray:rzzcModel.zuichunPoints aroundPoint: standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    rzzcModel.kediPoints = [self transformCommonPositionArray:rzzcModel.kediPoints aroundPoint: standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    
    // 瞳距鼻翼宽比
    GMFaceTongJuBiModel *tongjubiyiModel = analysisModel.tongjubiyiInfo;
    tongjubiyiModel.tongjuPoints  = [self transformCommonPositionArray:tongjubiyiModel.tongjuPoints  aroundPoint: standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    tongjubiyiModel.biyiPoints = [self transformCommonPositionArray:tongjubiyiModel.biyiPoints aroundPoint: standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    
    // 双Cxian
    GMFaceCLineModel *shuangCxian = analysisModel.shuangCxianInfo;
    shuangCxian.pointList = [self transformCommonPositionArray:shuangCxian.pointList aroundPoint: standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    
    //鼻子嘴巴
    GMFaceMouthNoseInfo *mouthNose = analysisModel.mouthNoseInfo;
    mouthNose.pointsNose = [self transformCommonPositionArray:mouthNose.pointsNose aroundPoint: standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    for (GMFacePointsMouthInfoModel *model in mouthNose.pointsMouth) {
        model.points = [self transformCommonPositionArray:model.points aroundPoint: standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    }
    mouthNose.mouthRect = [self transformScaleRect:mouthNose.mouthRect aroundPoint:standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    
    // 黄金三角
    GMFaceGoldTriangleInfo *goldTriangle = analysisModel.goldTriangleInfo;
    goldTriangle.pointsChin =  [self transformCommonPositionArray:goldTriangle.pointsChin  aroundPoint: standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    goldTriangle.pointsMouth =  [self transformCommonPositionArray:goldTriangle.pointsMouth  aroundPoint: standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    goldTriangle.pointsTriangle =  [self transformCommonPositionArray:goldTriangle.pointsTriangle  aroundPoint: standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    
    // 太阳穴
    GMFaceTempleInfoModel *temple = faceModel.templeInfo;
    CGPoint leftP = CGPointMake(temple.leftX, temple.leftY);
    CGPoint rightP = CGPointMake(temple.rightX, temple.rightY);
    CGPoint leftNewP = [self transformPoint:leftP aroundPoint:standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    CGPoint rightNewP = [self transformPoint:rightP aroundPoint:standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    temple.leftX = leftNewP.x;
    temple.rightX = rightNewP.x;
    temple.leftY = leftNewP.y;
    temple.rightY = rightNewP.y;
    
    //人脸框的转换
    faceModel.rect = [self transformScaleRect:faceModel.rect aroundPoint:standardPoint roate:[self rollTransformImageV:faceModel] scale:imageScale translate:translate];
    return faceModel;
}


+ (CGPoint)transformPoint:(CGPoint)point aroundPoint:(CGPoint)standardPoint roate:(CGAffineTransform)roateTransform scale:(CGFloat)imageScale translate:(CGPoint)translate {
    
    // 平移。因为整体图片有平移，所以point也需要平移，得到lastPoint
    CGPoint lastPoint = CGPointMake(point.x + translate.x, point.y + translate.y);
    // 锚点 就是标准点
    // lastPoint绕标准点旋转。
    /* 参考
     https://www.gamefromscratch.com/post/2012/11/24/GameDev-math-recipes-Rotating-one-point-around-another-point.aspx
     
     */
    lastPoint = CGPointApplyAffineTransform(
                                            CGPointMake(lastPoint.x - standardPoint.x,
                                                        lastPoint.y - standardPoint.y),
                                            roateTransform);
    lastPoint = CGPointMake(lastPoint.x + standardPoint.x, lastPoint.y + standardPoint.y);
    
    // lastPoint以标准点为圆点放大。参考网址找不到了
    CGFloat lastPointX = (lastPoint.x - standardPoint.x) * imageScale + standardPoint.x;
    CGFloat lastPointY = (lastPoint.y - standardPoint.y) * imageScale + standardPoint.y;
    lastPoint = CGPointMake(lastPointX, lastPointY);
    return lastPoint;
}

// 按照一定比例放大
+ (NSArray<GMFaceCommonPositionModel> *)scalePointsArray:(NSArray<GMFaceCommonPositionModel> *)array scale:(CGFloat)scaleX scaleY:(CGFloat)scaleY {
    NSMutableArray *muArray = [NSMutableArray array];
    for (GMFaceCommonPositionModel *position in array) {
        position.x *= scaleX;
        position.y *= scaleY;
        [muArray addObject:position];
    }
    NSArray<GMFaceCommonPositionModel> *arr = [muArray copy];
    return arr;
}

// 先平移frame 在旋转 最后放大的计算公式
+ (NSArray<GMFaceCommonPositionModel> *)transformCommonPositionArray:(NSArray<GMFaceCommonPositionModel> *)array aroundPoint:(CGPoint)standardPoint roate:(CGAffineTransform)roateTransform scale:(CGFloat)imageScale translate:(CGPoint)translate {
        NSMutableArray *muArray = [NSMutableArray array];
        for (GMFaceCommonPositionModel *position in array) {
            CGPoint point = CGPointMake(position.x, position.y);
            CGPoint transPoint = [self transformPoint:point aroundPoint:standardPoint  roate:roateTransform scale:imageScale translate:translate];
            position.x = transPoint.x;
            position.y = transPoint.y;
            [muArray addObject:position];
        }
        NSArray<GMFaceCommonPositionModel> *arr = [muArray copy];
        return arr;
}

+ (ALFaceRectModel *)transformScaleRect:(ALFaceRectModel *)rect  aroundPoint:(CGPoint)standardPoin roate:(CGAffineTransform)roateTransform scale:(CGFloat)imageScale translate:(CGPoint)translate {
    CGPoint leftPoint = CGPointMake(rect.left, rect.top);
    CGPoint rightPoint = CGPointMake(rect.right, rect.bottom);

    CGPoint leftTransPoint = [self transformPoint:leftPoint aroundPoint:standardPoin roate:roateTransform scale:imageScale translate:translate];
    CGPoint rightTransPoint = [self transformPoint:rightPoint aroundPoint:standardPoin roate:roateTransform scale:imageScale translate:translate];
    rect.left = leftTransPoint.x;
    rect.top = leftTransPoint.y;
    rect.right = rightTransPoint.x;
    rect.bottom = rightTransPoint.y;
    return rect;
}

 + (ALFaceRectModel *)scaleRect:(ALFaceRectModel *)rect scale:(CGFloat)scaleX scaleY:(CGFloat)scaleY {
     rect.left = rect.left * scaleX;
     rect.top = rect.top * scaleY;
     rect.right = rect.right * scaleX;
     rect.bottom = rect.bottom * scaleY;
     return rect;
}

+ (NSDictionary *)readLocalFileWithName:(NSString *)name {
    // 获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"json"];
    // 将文件数据化
    NSData *data = [[NSData alloc] initWithContentsOfFile:path];
    // 对数据进行JSON格式化并返回字典形式
    return [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
}

@end
