//
//  ERTCBeautyManager.m
//  ERTCDemo
//
//  Created by wangjinlong23 on 2022/8/16.
//

#import "ERTCBeautyManager.h"
#import "ERTCPlayerViewManager.h"
#import <ERTC/ERTCDef.h>

NSString * const kBeautyConfigurationBarModels = @"kBeautyConfigurationBarModels";
NSString * const kSelectedBeautyKey = @"kSelectedBeautyKey";
NSString * const kSelectedBlurKey = @"kSelectedBlurKey";

NSString * const kBeauty_noneKey = @"beauty_none";
NSString * const kERTCBlur_noneKey = @"origin";

@implementation ERTCBeautyManager

#pragma mark 滤镜改变
/// 滤镜改变
+ (void)filterNameChange:(ERTCFilterType)filterType filterValueChange:(double)filterLevel{
    [ERTCPlayerViewManager sharedInstance].filterType = filterType;
    [ERTCPlayerViewManager sharedInstance].filterLevel = filterLevel;
    [[ERTCPlayerViewManager sharedInstance].engine setFilter:filterType filterLevel:filterLevel];
}

#pragma mark 滤镜程度改变
/// 滤镜程度改变
//+ (void)filterValueChange:(double)filterLevel {
//    NSLog(@"Beauty-- blurFilterValueChange:%f", filterLevel);
//    [ERTCPlayerViewManager sharedInstance].filterLevel = filterLevel;
//    [[ERTCPlayerViewManager sharedInstance].engine setFilter:[ERTCPlayerViewManager sharedInstance].filterType filterLevel:filterLevel];
//}

#pragma mark 美颜参数改变
/// 美颜参数改变
+ (void)beautyParamValueChange:(double)level type:(ERTCBeautySkin)type {
    NSLog(@"Beauty-- beautyParamValueChange:%f type:%d", level, (int)type);
    switch (type) {
        case ERTCBeautySkinSmoothness:
            [[ERTCPlayerViewManager sharedInstance].engine setSmoothnessLevel:level];
            break;
        case ERTCBeautySkinWhiteness:
            [[ERTCPlayerViewManager sharedInstance].engine setWhitenessLevel:level];
            break;
        case ERTCBeautySkinRuddyness:
            [[ERTCPlayerViewManager sharedInstance].engine setRuddynessLevel:level];
            break;
       
        default:
            break;
    }
}

#pragma mark - 初始化滤镜
+(NSArray *)loadBeautyFilters {
    //美颜资源
    NSArray *prams = @[kBeauty_noneKey, @"blur_level", @"color_level", @"red_level"];
    NSDictionary *titelDic = @{kBeauty_noneKey:@"无", @"blur_level":@"磨皮", @"color_level":@"美白", @"red_level":@"红润"};
    NSDictionary *normalImageDic = @{kBeauty_noneKey:@"icn_no", @"blur_level":@"icn_portrait", @"color_level":@"icn_ruddy", @"red_level":@"icn_whitening"};
    NSDictionary *selectedImageDic = @{kBeauty_noneKey:@"icn_no",@"blur_level":@"icn_portrait", @"color_level":@"icn_ruddy", @"red_level":@"icn_whitening"};
    NSDictionary *valueDic = @{kBeauty_noneKey:@(0), @"blur_level":@(0.508), @"color_level":@(0.292), @"red_level":@(0.292)};
    NSDictionary *defaultValueDic = @{kBeauty_noneKey:@(0), @"blur_level":@(0.5), @"color_level":@(0.3), @"red_level":@(0.3)};
    
    NSMutableArray *skinParams = [NSMutableArray array];
    for (NSUInteger i = 0; i < prams.count; i ++) {
        NSString *str = prams[i];
        ERTCBeautyModel *model = [[ERTCBeautyModel alloc] init];
        model.param = i;
        model.titleKey = str;
        model.title = [titelDic valueForKey:str];
        model.value = [[valueDic valueForKey:str] floatValue];
        model.defaultValue = [[defaultValueDic valueForKey:str] floatValue];
        model.normalImageName = [normalImageDic valueForKey:str];
        model.selectedImageName = [selectedImageDic valueForKey:str];
        [skinParams addObject:model];
    }
    
    return skinParams;
}
    
+(NSArray *)loadBlurFilters {
    //滤镜资源
    NSArray<NSString *> *beautyFiltersDataSource = @[kERTCBlur_noneKey,
                                                     @"filter_baixi",@"filter_wennuan",
                                                     @"filter_qingchun",@"filter_huahai",
                                                     @"filter_qingxin",@"filter_tianmei",
                                                     @"filter_luokeke",@"filter_shengxia",
                                                     @"filter_shaonv",@"filter_mingkuai",
                                                     @"filter_fugu",@"filter_tuise"
    ];
    
    NSDictionary *filtersCHName = @{kERTCBlur_noneKey:@"无",
                                    @"filter_baixi":@"白皙",@"filter_wennuan":@"温暖",
                                    
                                    @"filter_qingchun":@"青春",@"filter_huahai":@"花海",
                                    
                                    @"filter_qingxin":@"清新",@"filter_tianmei":@"甜美",
                                    
                                    @"filter_luokeke":@"洛可可",@"filter_shengxia":@"盛夏",
                                    
                                    @"filter_shaonv":@"少女",@"filter_mingkuai":@"明快",
                                    
                                    @"filter_fugu":@"复古",@"filter_tuise":@"褪色"
    };
    NSDictionary *filterTypeDic = @{kERTCBlur_noneKey:@(ERTC_Basic_Filter_Type_None),
                                    @"filter_baixi":@(ERTC_Basic_Filter_Type_1),@"filter_wennuan":@(ERTC_Basic_Filter_Type_2),
                                    
                                    @"filter_qingchun":@(ERTC_Basic_Filter_Type_3),@"filter_huahai":@(ERTC_Basic_Filter_Type_4),
                                    
                                    @"filter_qingxin":@(ERTC_Basic_Filter_Type_5),@"filter_tianmei":@(ERTC_Basic_Filter_Type_6),
                                    
                                    @"filter_luokeke":@(ERTC_Basic_Filter_Type_7),@"filter_shengxia":@(ERTC_Basic_Filter_Type_8),
                                    
                                    @"filter_shaonv":@(ERTC_Basic_Filter_Type_9),@"filter_mingkuai":@(ERTC_Basic_Filter_Type_10),
                                    
                                    @"filter_fugu":@(ERTC_Basic_Filter_Type_11),@"filter_tuise":@(ERTC_Basic_Filter_Type_12)
    };
    NSMutableArray *filters = [NSMutableArray array];
    [beautyFiltersDataSource enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        ERTCBeautyModel *model = [[ERTCBeautyModel alloc] init];
        NSNumber *filterNumber = [filterTypeDic valueForKey:obj];
        /// 视频处理类型(滤镜)
        model.type = ERTCBeautyDefineFilter;
        model.titleKey = obj;
        model.title = [filtersCHName valueForKey:obj];
        model.filterType = [filterNumber intValue];
        model.value = 0.4;
        model.selected = NO;
        model.defaultValue = 0.4;
        if ([obj isEqualToString:kERTCBlur_noneKey]) {
            model.normalImageName = @"icn_no";
        }
        else
        {
            model.normalImageName = obj;
        }
        
        [filters addObject:model];
    }];
    
    return filters;
}

@end
