//
//  StatisticsViewController.m
//  IntelligentRepaymentButler
//
//  Created by ymtx on 2019/5/7.
//  Copyright © 2019年 lmj. All rights reserved.
//

#import "StatisticsViewController.h"
#import "AAChartKit.h"
#import "AAChartModel.h"
#import "WKClearPhotoManager.h"

@interface StatisticsViewController ()<AAChartViewEventDelegate>

@property (nonatomic, strong) WKClearPhotoManager *photoMgr;
@property (nonatomic, assign) NSInteger total;
@property (nonatomic, assign) AAChartModel *aaChartModel;
@property (nonatomic, strong) AAChartView *aaChartView;
@property (nonatomic, strong) UILabel * totalLab;

@end

@implementation StatisticsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.title = @"图片统计";
    
//    [MBProgressHUD showMessage:@"加载中..." delayTime:1.f toView:nil];
    [self setUpTheChartView:AAChartTypePie];
}

- (void)setUpTheChartView:(AAChartType)chartType {
    CGRect chartViewFrame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width);
    self.aaChartView = [[AAChartView alloc]initWithFrame:chartViewFrame];
    self.view.backgroundColor = [UIColor whiteColor];
    self.aaChartView.scrollEnabled = NO;
    [self.view addSubview:self.aaChartView];
    
    _totalLab = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.aaChartView.frame), KScreenWidth, 20)];
    _totalLab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_totalLab];
    
    self.photoMgr.pauseAction = false;

    if (self.photoMgr.fetchResult == nil) {
        [self loadPhotoData: true];
    }else{
        self.total = self.photoMgr.fetchResult.count;
        self.aaChartModel = [self configureTheChartModel:nil];
        [self.aaChartView aa_drawChartWithChartModel:self.aaChartModel];
    }
}

- (WKClearPhotoManager *)photoMgr {
    if (!_photoMgr) {
        _photoMgr = [WKClearPhotoManager shareManager];
    }
    return _photoMgr;
}

- (void)loadPhotoData: (BOOL)isFirstLoad {

    [MBProgressHUD showLoading:@"正在统计，请稍后..." toView:self.view];
    self.photoMgr.currentIndex = 0;
    self.photoMgr.pauseAction = false;
    [self.photoMgr loadPhotoWithProcess:^(NSInteger current, NSInteger total) {

    } completionHandler:^(BOOL success, NSError *error) {
    
        [MBProgressHUD hideHUDForView:self.view];
        [self loadLocationAssets];
    }];
}

- (void)loadLocationAssets {
    
    [MBProgressHUD showLoading:@"正在统计，请稍后..." toView:self.view];
    [self.photoMgr loadLocationPhotoWithProcess:^(NSInteger current, NSInteger total) {
        NSLog(@"loadLocationPhotoWithProcess %ld", (long)current);
        
        self.total = total;
    } completionHandler:^(BOOL success, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        
        self.aaChartModel = [self configureTheChartModel:nil];
        [self.aaChartView aa_drawChartWithChartModel:self.aaChartModel];
    }];
}

- (AAChartModel *)configureTheChartModel:(NSString *)chartType {
    
    NSInteger f = 0;
    for (NSArray * subArr in self.photoMgr.similarLiveArray){
        f += subArr.count;
    }
    NSInteger ff = 0;
    for (NSArray * subArr in self.photoMgr.similarArray){
        ff += subArr.count;
    }
    NSInteger fff = self.photoMgr.snapshotArray.count;
    NSInteger ffff = 0;
    for (NSArray * subArr in self.photoMgr.locationArray){
        ffff += subArr.count;
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.totalLab.text = [NSString stringWithFormat:@"合计：%ld", (long)self.total];
    });
    NSInteger fffff = self.total -f -ff-fff-ffff;

    AASeriesElement *element = AASeriesElement.new
    .nameSet(@"数量")
    .innerSizeSet(@"20%")//内部圆环半径大小占比
    .sizeSet(@200)//尺寸大小
    .borderWidthSet(@0)//描边的宽度
    .allowPointSelectSet(true)//是否允许在点击数据点标记(扇形图点击选中的块发生位移)
    .statesSet(AAStates.new
               .hoverSet(AAHover.new
                         .enabledSet(false)//禁用点击区块之后出现的半透明遮罩层
                         ))
    .dataSet(@[
        @[@"相似动态图片",          [NSNumber numberWithInteger:f]],
        @{@"sliced": @true,
          @"selected": @true,
          @"name": @"相似图片",
          @"y": [NSNumber numberWithInteger:ff]},
        @[@"屏幕截图",      [NSNumber numberWithInteger:fff]],
        @[@"同地点图片",       [NSNumber numberWithInteger:ffff]],
        @[@"其他",      [NSNumber numberWithInteger:fffff]],
    ]);
    
    return AAChartModel.new
    .chartTypeSet(AAChartTypePie)
    .colorsThemeSet(@[@"#0c9674",@"#7dffc0",@"#ff3333",@"#facd32",@"#ffffa0",@"#EA007B"])
    .dataLabelsEnabledSet(true)//是否直接显示扇形图数据
    .yAxisTitleSet(@"")
    .seriesSet(@[element])
    ;
}

@end
