//
//  StatisticsViewController.m
//  Open
//
//  Created by 雷俊博 on 2020/7/17.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "StatisticsViewController.h"
#import "KKChartView.h"

@interface StatisticsViewController ()

@property (nonatomic, strong)UIScrollView* scrollView;

@property (nonatomic, strong)UILabel* lb_dateDistance;

@property (nonatomic, strong)KKChartView*chartView;

@property (nonatomic, strong)UIView* dayView;

@property (nonatomic, strong)UILabel* lb_kiolmeter;

@property (nonatomic, strong)UILabel* lb_time;

@property (nonatomic, strong)UILabel* lb_calories;


@end

@implementation StatisticsViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [GTCountSDK trackCountEvent:@"life_jiankangtongji" withArgs:@{}];//个推统计
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self navConfig];
    [self.view addSubview:self.scrollView];
    [self configData];
    [self loadData];
    
}

- (void)navConfig
{
    [self.navigationController setNavigationBarHidden:YES];
    
    UIView*navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, cl_kNavigationBarHeight)];
    navView.backgroundColor = K_MainColor;
    [self.view addSubview:navView];
    
    
    UIButton * backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake( 10 * WIDTHRADIU, cl_kStatusBarHeight , 50, 44);
    backBtn.titleLabel.textAlignment =NSTextAlignmentLeft;
    [backBtn setTitle:@"返回" forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    //    [button_2 setBackgroundColor:[UIColor redColor]];
    //    [button_2 setTitleColor:[UIColor colorWithRed:12.0 green:12.0 blue:12.0 alpha:1] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:backBtn];
    [backBtn layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleLeft imageTitleSpace:3];
    
    
    
    
    //title
    UILabel* lb_title = [[UILabel alloc]init];
    lb_title.textColor = [UIColor whiteColor];
    lb_title.font = [UIFont boldSystemFontOfSize:17 * WIDTHRADIU];
    lb_title.textAlignment = NSTextAlignmentCenter;
    [navView addSubview:lb_title];
    
    lb_title.text = self.title;
    
    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(navView);
        make.height.offset(25);
        make.top.offset(cl_kStatusBarHeight + 9.5);
    }];
}

- (void)configData
{
    
    //公里
    NSString* str = [NSString stringWithFormat:@"%.2lf公里\n步行历程",self.distance.floatValue/1000];
    NSMutableAttributedString*attributedStr = [[NSMutableAttributedString alloc]initWithString:str];
    [attributedStr addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:20],NSForegroundColorAttributeName:[UIColor colorWithHex:0x4d4d4d]} range:NSMakeRange(0, str.length - 4)];
    [attributedStr addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName:[UIColor colorWithHex:0x808080]} range:NSMakeRange(str.length - 4, 4)];
    
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:8];
    paragraphStyle.alignment = NSTextAlignmentCenter;
    
    [attributedStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [str length])];
    
    self.lb_kiolmeter.attributedText = attributedStr;
    //时间
    NSString* str1 = [self getTimeStrWithDistance:self.distance];
   NSMutableAttributedString*attributedStr1 = [[NSMutableAttributedString alloc]initWithString:str1];
   [attributedStr1 addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:20],NSForegroundColorAttributeName:[UIColor colorWithHex:0x4d4d4d]} range:NSMakeRange(0, str1.length - 4)];
   [attributedStr1 addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName:[UIColor colorWithHex:0x808080]} range:NSMakeRange(str1.length - 4, 4)];
   NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
  [paragraphStyle1 setLineSpacing:8];
   paragraphStyle1.alignment = NSTextAlignmentCenter;
  
  [attributedStr1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [str1 length])];
   
   self.lb_time.attributedText = attributedStr1;
    
    //卡路里
    self.lb_calories.attributedText = [self getKaLuLiStrWithStep:self.stepCount];
    
}

- (void)loadData
{
    NSString* url = [NSString stringWithFormat:@"%@walkservice/getWeekStep",CoinsBaseUrl];
    NSDictionary*params = @{
        @"userid":@([getPeopleID PeopleID])
    };
    WeakSelf
    [KKLoadingAnimationView show];
    [NetworkRequest Post:url Params:params Success:^(id _Nonnull objc) {
        [KKLoadingAnimationView dismiss];
        __strong typeof(self) strongSelf = weakSelf;
        NSLog(@"objc:%@",objc);
        NSString*code = [NSString stringWithFormat:@"%@",objc[@"code"]];
        NSString*des = [NSString stringWithFormat:@"%@",objc[@"des"]];
        if ([code isEqualToString:@"0"]) {
            NSMutableArray*dateArray = [NSMutableArray array];
            NSMutableArray*stepArray = [NSMutableArray array];
            for (NSDictionary* dic in objc[@"result"]) {
                NSString*dateStr = [NSString stringWithFormat:@"%@\n%@",dic[@"dayStr"],dic[@"weekStr"]];
                [dateArray addObject:dateStr];
                [stepArray addObject:dic[@"stepCount"]];
            }
            NSArray*array = objc[@"result"];
            strongSelf.lb_dateDistance.text = [strongSelf getDateDistanceWithStartDate:array[0][@"dayStr"] endDate:array.lastObject[@"dayStr"]];
            [strongSelf.chartView drawBarChartViewWithX_Value_Names:dateArray TargetValues:stepArray];
        }else{
            [SVProgressHUD showErrorWithStatus:des];
        }
    } Failure:^(NSError* error) {
        [KKLoadingAnimationView dismiss];
        [SVProgressHUD showErrorWithStatus:@"网络异常"];
        NSLog(@"error:%@",error);
    }];
}

#pragma mark - event response
- (void)backClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - getters and setters
- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, cl_kNavigationBarHeight, ScreenWidth, ScreenHeight)];
        _scrollView.backgroundColor = [UIColor whiteColor];
        
        
        
        
        self.lb_dateDistance = [[UILabel alloc]initWithFrame:CGRectMake(20, 30, ScreenWidth - 40, 20)];
        self.lb_dateDistance.textColor = [UIColor colorWithHex:0x4d4d4d];
        self.lb_dateDistance.font = [UIFont systemFontOfSize:15];
        self.lb_dateDistance.text = @"2020年5月25日至5月31日";
        self.lb_dateDistance.textAlignment = NSTextAlignmentCenter;
        [_scrollView addSubview:self.lb_dateDistance];
        
        
        
        self.chartView.frame = CGRectMake(0, CGRectGetMaxY(self.lb_dateDistance.frame) + 10, ScreenWidth, 330);
        [_scrollView addSubview:self.chartView];
        
        UIView*grayView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.chartView.frame) + 20, ScreenWidth, 10)];
        grayView.backgroundColor = [UIColor colorWithHex:0xf4f4f4];
        [_scrollView addSubview:grayView];
        
        self.dayView.frame = CGRectMake(0, CGRectGetMaxY(grayView.frame), ScreenWidth, 120);
        
        [_scrollView addSubview:self.dayView];
        
        _scrollView.contentSize = CGSizeMake(ScreenWidth, CGRectGetMaxY(self.dayView.frame));
        
    }
    return _scrollView;
}

- (KKChartView *)chartView
{
    if (!_chartView) {
        _chartView = [[KKChartView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 330)];
        _chartView.backgroundColor = [UIColor whiteColor];
        _chartView.leftMargin = 20.0;
        _chartView.bottomMargin = 30.0;
        _chartView.horizontalMargin = 10.0;
        _chartView.verticalMargin = 40.0;
        _chartView.priceLabelWidth = 40;
        _chartView.priceLabelHeight = 280;
        _chartView.columnWidth = 20;
        _chartView.verticalArray = @[@"0",@"860",@"1720",@"2580",@"3440",@"4330"];
        _chartView.dateArray = @[@"5/25\n周一",@"5/26\n周二",@"5/27\n周三",@"5/28\n周四",@"5/29\n周五", @"5/30\n周六",@"5/31\n周日"];
        _chartView.priceArray = @[@"0",@"0",@"0",@"0",@"0",@"0",@"0"];
        _chartView.weightArray = @[@"20",@"30",@"40",@"50",@"60",@"70"];
        [_chartView drawBarChartViewWithX_Value_Names:_chartView.dateArray TargetValues:_chartView.priceArray];
    }
    return _chartView;
}

- (UIView *)dayView
{
    if (!_dayView) {
        _dayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 150)];
        _dayView.backgroundColor = [UIColor whiteColor];
        
        UIImageView*iconImg = [[UIImageView alloc]initWithFrame:CGRectMake(20, 10, 27, 27)];
        iconImg.image = [UIImage imageNamed:@"fullView_list_ playBtn_play_2"];
        [_dayView addSubview:iconImg];
        
        UILabel*lb_title = [[UILabel alloc]initWithFrame:CGRectMake(44, 10, 80, 27)];
        lb_title.textColor = [UIColor colorWithHex:0x4d4d4d];
        lb_title.font = [UIFont systemFontOfSize:16];
        lb_title.text = @"每日数据";
        [_dayView addSubview:lb_title];
        CGFloat width = (ScreenWidth - 40)/3;
        CGFloat height = 80;
        for (int i = 0; i < 3; i++) {
            CGFloat x = 20 + i*width;
            UILabel*lb = [UILabel new];
            lb.numberOfLines = 2;
            [_dayView addSubview:lb];
            if (i == 0) {
                lb.frame = CGRectMake(x, CGRectGetMaxY(iconImg.frame), 100, height);
                lb.textAlignment = NSTextAlignmentCenter;
                NSString* str = @"2.45公里\n步行里程";
                NSMutableAttributedString*attributedStr = [[NSMutableAttributedString alloc]initWithString:str];
                [attributedStr addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:20],NSForegroundColorAttributeName:[UIColor colorWithHex:0x4d4d4d]} range:NSMakeRange(0, str.length - 4)];
                [attributedStr addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName:[UIColor colorWithHex:0x808080]} range:NSMakeRange(str.length - 4, 4)];
                
                NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
                [paragraphStyle setLineSpacing:8];
                paragraphStyle.alignment = NSTextAlignmentCenter;
                
                [attributedStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [str length])];
                
                lb.attributedText = attributedStr;
                
                self.lb_kiolmeter = lb;
            }else if (i == 1){
                lb.frame = CGRectMake(x, CGRectGetMaxY(iconImg.frame), width, height);
                lb.textAlignment = NSTextAlignmentCenter;
                NSString* str = @"1时58分\n步行时间";
                NSMutableAttributedString*attributedStr = [[NSMutableAttributedString alloc]initWithString:str];
                [attributedStr addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:20],NSForegroundColorAttributeName:[UIColor colorWithHex:0x4d4d4d]} range:NSMakeRange(0, str.length - 4)];
                [attributedStr addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName:[UIColor colorWithHex:0x808080]} range:NSMakeRange(str.length - 4, 4)];
                NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
               [paragraphStyle setLineSpacing:8];
                paragraphStyle.alignment = NSTextAlignmentCenter;
               
               [attributedStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [str length])];
                
                lb.attributedText = attributedStr;
                self.lb_time = lb;
            }else{
                lb.frame = CGRectMake(ScreenWidth - 120, CGRectGetMaxY(iconImg.frame), 100, height);
                lb.textAlignment = NSTextAlignmentCenter;
                NSString* str = @"1.30千卡\n卡路里";
                NSMutableAttributedString*attributedStr = [[NSMutableAttributedString alloc]initWithString:str];
                [attributedStr addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:20],NSForegroundColorAttributeName:[UIColor colorWithHex:0x4d4d4d]} range:NSMakeRange(0, str.length - 3)];
                [attributedStr addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName:[UIColor colorWithHex:0x808080]} range:NSMakeRange(str.length - 3, 3)];
                NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
               [paragraphStyle setLineSpacing:8];
                paragraphStyle.alignment = NSTextAlignmentCenter;
               
               [attributedStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [str length])];
                
                lb.attributedText = attributedStr;
                self.lb_calories = lb;
            }
        }
        
        
    }
    return _dayView;
}
#pragma mark - getDateDistance
- (NSString *)getDateDistanceWithStartDate:(NSString *)startStr endDate:(NSString *)endStr
{
    NSString* str = [NSString stringWithFormat:@"%@%@至%@",[self getCurrentYear],[self getDateStr:startStr],[self getDateStr:endStr]];
    return str;
}

- (NSString *)getDateStr:(NSString *)str
{
    NSArray*dateArray = [str componentsSeparatedByString:@"/"];
    NSString*month = dateArray[0];
    NSString*day = dateArray[1];
    
      
    NSString* currentDateStr = [NSString stringWithFormat:@"%d月%d日",month.intValue,day.intValue];
    return currentDateStr;
}

- (NSString *)getCurrentYear
{
    NSDate *  currentDate=[NSDate date];

    

    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];

    

    [dateformatter setDateFormat:@"yyyy"];

    //    今年2016

    NSString *thisYearString=[dateformatter stringFromDate:currentDate];
    
    thisYearString = [thisYearString stringByAppendingFormat:@"年"];
    return thisYearString;
}
- (NSString *)getTimeStrWithDistance:(NSNumber *)distance
{
    CGFloat number = distance.floatValue/1000;
    number = 12 * number;
    
    int hours = (int)number/60;
    int minutes = (int)number % 60;
    if (hours == 0) {
        return [NSString stringWithFormat:@"%d分\n步行时间",minutes];
    }
    return [NSString stringWithFormat:@"%d时%d分\n步行时间",hours,minutes];
}


- (NSMutableAttributedString *)getKaLuLiStrWithStep:(NSNumber *)stepCount
{
    CGFloat step = stepCount.integerValue * 34.7 / 1000;
    
    NSString* str = nil;
    if (step >= 1000) {
        str = [NSString stringWithFormat:@"%dK千卡\n卡路里",(int)step/1000];
    }else{
        str = [NSString stringWithFormat:@"%d千卡\n卡路里",(int)step];
    }
     NSMutableAttributedString*attributedStr = [[NSMutableAttributedString alloc]initWithString:str];
     [attributedStr addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:20],NSForegroundColorAttributeName:[UIColor colorWithHex:0x4d4d4d]} range:NSMakeRange(0, str.length - 3)];
     [attributedStr addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName:[UIColor colorWithHex:0x808080]} range:NSMakeRange(str.length - 3, 3)];
     NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:8];
     paragraphStyle.alignment = NSTextAlignmentCenter;
    
    [attributedStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [str length])];
    return attributedStr;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
