//
//  socialOffLineGatherViewController.m
//  Open
//
//  Created by 看看智能 on 2017/11/9.
//  Copyright © 2017年 黄江涛 . All rights reserved.
//

#import "socialOffLineGatherViewController.h"

@interface socialOffLineGatherViewController ()

@end

@implementation socialOffLineGatherViewController
{
     ZZCircleProgress *circle1;
}

+ (socialOffLineGatherViewController *)sharedInstance{
    static socialOffLineGatherViewController *p = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        UIStoryboard *sb  =[UIStoryboard storyboardWithName:@"Main" bundle:nil];
        p=[sb instantiateViewControllerWithIdentifier:@"socialOffLineGatherViewController"];
    });
    return p;
}



-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    gatherUtil *gathers = [[gatherUtil alloc]initWithGG];

    
//    self.gatherLabelLeft.text = [NSString stringWithFormat:@"已经采集了%ld",(long)[[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"]];
//    self.gatherLabelRight.text =  [NSString stringWithFormat:@"还剩下%ld个人可以采集",GATHERMAX - (long)[[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"]];
//
    
//    [gathers addObserver:self forKeyPath:@"index" options:NSKeyValueObservingOptionNew context:nil];
    if ([[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"] == 0) {
        //还没有采集
        self.gatherLabelRight.text = [NSString stringWithFormat:@"%d",0];
        
        circle1.progress = 0.0;
    }else{
        //采集了
        //        [self.startGather setTitle: [NSString stringWithFormat:@"还剩下%ld人可以采集",GATHERMAX - (long)[[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"]] forState: UIControlStateNormal];
        //
        //        [self.publishButton setTitle:[NSString stringWithFormat:@"需要提交%ld人,马上提交",(long)[[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"]] forState:UIControlStateNormal];
        
        
        self.gatherLabelRight.text = [NSString stringWithFormat:@"%ld",(long)[[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"]];
        
        circle1.progress = [[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"]  /20.00;
        
    }
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:35/255.0 green:168/255.0 blue:222/255.0 alpha:1.0]];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
//这句代码要慎用
//    [self.navigationController.navigationBar.subviews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
//            if ([view isKindOfClass:NSClassFromString(@"_UINavigationBarBackground")]) {
//
//                [view.subviews firstObject].hidden = YES;
//            }
//        if ([view isKindOfClass:NSClassFromString(@"_UIBarBackground")]) {
//
//            [view.subviews firstObject].hidden = YES;
//        }
//    }];

    

    
    
    
    
}



-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"gatherABC" object:nil];

}




#pragma drawRect实现方式
- (void)initCircles {
    


    //默认状态
        circle1 = [[ZZCircleProgress alloc] initWithFrame:CGRectMake(0, 0, 150, 150) pathBackColor: [UIColor colorWithRed:47/255.0 green:174/255.0 blue:235/255.0 alpha:1.0] pathFillColor: kRGBColor(9, 255, 255) startAngle:-90 strokeWidth:5];
//    circle1.center = CGPointMake(0,0);
    circle1.progress = 0.6;
    circle1.showPoint = NO;
    circle1.animationModel = CircleIncreaseSameTime;
    circle1.progress = 0.0;
    circle1.showProgressText =NO;
    circle1.increaseFromLast =YES;
    [self.imageCGHeight addSubview:circle1];
    
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCircles];
    self.title =@"离线采集";
    self.gatherTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    self.gatherLabelLeft.text = [NSString stringWithFormat:@"已经采集了%ld",(long)[[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"]];
//    self.gatherLabelRight.text =  [NSString stringWithFormat:@"还剩下%ld个人可以采集",GATHERMAX - (long)[[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"]];
    if ([[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"] == 0) {
     //还没有采集
         self.gatherLabelRight.text = [NSString stringWithFormat:@"%d",0];
       
        circle1.progress = 0.0;
    }else{
        //采集了
//        [self.startGather setTitle: [NSString stringWithFormat:@"还剩下%ld人可以采集",GATHERMAX - (long)[[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"]] forState: UIControlStateNormal];
//
//        [self.publishButton setTitle:[NSString stringWithFormat:@"需要提交%ld人,马上提交",(long)[[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"]] forState:UIControlStateNormal];
        
        
        self.gatherLabelRight.text = [NSString stringWithFormat:@"%ld",(long)[[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"]];

        circle1.progress = [[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"]  /20.00;
      
    }
    
 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ageAchange:) name:@"gatherABC" object:nil];
    
}

-(void)ageAchange:(NSNotification *)notifi
{
//    self.myLabel.text =[not.object stringValue];
   // NSLog(@"sdsad%@",not.object);
//    self.gatherLabelLeft.text = [NSString stringWithFormat:@"已经采集了%ld",(long)[[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"]];
//    self.gatherLabelRight.text =  [NSString stringWithFormat:@"还剩下%ld个人可以采集",GATHERMAX - (long)[[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"]];


    self.gatherLabelRight.text = [NSString stringWithFormat:@"%ld",(long)[[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"]];

    circle1.progress = (long)[[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"] /20.00;


}






-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    gatherUtil *p =object;
    NSLog(@"监听到的值:%ld",(long)p.index);
  
}



- (IBAction)touchCaiJi:(UIButton *)sender {
    if (sender.tag == 9800) {
        
    }
    switch (sender.tag) {
        case 9800:
            //离线采集
            
            [self offLineGather];
            break;
        case 9801:
            //马上提交
            [self submit];
        
            
            
            break;
        default:
            break;
    }
}
-(void)offLineGather{
    if([[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"] >= GATHERMAX){
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:@"请全部提交后在使用离线采集功能"];
        });
        return;
    }
    //huo
    //第二个
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    socialOnceViewController *once  = [sb instantiateViewControllerWithIdentifier:@"socialOnceViewController"];
    //            socialSecurityViewController *once = [socialSecurityViewController sharedInstance];
    once.personPrivate = 1;
    once.offLineGather = @"离线采集";
    once.hidesBottomBarWhenPushed =YES;
     self.navigationController.navigationBar.translucent = YES;
            [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:27/255.0 green:132/255.0 blue:208/255.0 alpha:1.0]];
    [self.navigationController pushViewController:once animated:YES];
    
    
    
    
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
         self.navigationController.navigationBar.translucent = YES; 
            [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:27/255.0 green:132/255.0 blue:208/255.0 alpha:1.0]];
    
}
-(void)submit{
    gatherUtil *gat = [[gatherUtil alloc]initWithGG];
    [gat  IsNetWork:^{
        self.labelLeft =0;
        NSMutableArray *dataArray= [[NSMutableArray alloc]init];
        [dataArray removeAllObjects];
        NSMutableArray *dataArrayNAME= [[NSMutableArray alloc]init];
        [dataArrayNAME removeAllObjects];
        [dataArray  addObjectsFromArray:[gat checkDate]];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
            //      GATHERMAX -> dataArray.count
            for (int i = 0 ; i < dataArray.count  ; i++) {
                NSString*  pathRoad =[IPDIZHI stringByAppendingString:@"/socialsecurity/kankan/insertInfo"];
                [SVProgressHUD show];
                [gatherNetModel getGatherSenderSocialMessageInsertInfo:pathRoad abd:dataArray[i]  completionHandle:^(verifyAdressModel *model, NSError *error) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        gather  * gatModel=        (gather *) dataArray[i];
                        //执行一次就增加一个变量
                        self.labelLeft ++;
                        if(model.code == 0)
                        {
                            //上传成功在将数据保存qi'lai
                            //                        采集成功的逻辑全部换掉
                            //                        NSInteger indexpage =     [[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"];
                            //                        indexpage -=1;
                            //                        [[NSUserDefaults standardUserDefaults] setInteger:indexpage forKey:@"fucck_two"];
                            //                        [[NSUserDefaults standardUserDefaults] synchronize];
                            //                        self.gatherLabelLeft.text = [NSString stringWithFormat:@"已经采集了%ld",(long)[[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"]];
                            //                        self.gatherLabelRight.text =  [NSString stringWithFormat:@"还剩下%ld可以采集",GATHERMAX - (long)[[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"]];
                            NSLog(@"采集成功了");
                            
                            
                            
                        }else
                        {
                            //                    [SVProgressHUD dismiss];
                            //                    codeSelected *code = [codeSelected MustTakeReturn:model.code];
                            //                    [code returnCodeMethod];
                            
                            //  [dataArray addObject: gatModel.Name];
                            
                            [dataArrayNAME addObject:gatModel.Name];
                            
                        }
                        if (self.labelLeft == dataArray.count) {
                            //                    [SVProgressHUD showSuccessWithStatus:[NSString stringWithFormat:@"提交成功了%ld",(long)self.labelLeft]];
                            
                            //失败的名字
//                            [self.startGather setTitle:@"离 线 采 集" forState: UIControlStateNormal];
//
//                            [self.publishButton setTitle:@"确 定 提 交" forState:UIControlStateNormal];
                       
                            self.gatherLabelRight.text = [NSString stringWithFormat:@"%d",0];
 circle1.progress =0.0;
                            NSString *str1 =  [NSString stringWithFormat:@"离线采集上传成功%ld个,\n其中",(long)(dataArray.count - dataArrayNAME.count)] ;
                            NSString *str2 = @"";
                            for (NSString *sd in dataArrayNAME) {
                                
                                str2 =       [[str2 stringByAppendingString:sd] stringByAppendingString:@"、"];
                                
                                NSLog(@"%@",sd);
                                
                                
                                
                            }
                            str1 =       [[str1 stringByAppendingString:str2] stringByAppendingString:@"上传失败请重新采集上传"] ;
                            
                            UIAlertController *alertCon = nil;
                            if((long)(dataArray.count - dataArrayNAME.count) == dataArray.count ){
                                NSString *str1 =  [NSString stringWithFormat:@"离线采集上传成功%ld个",(long)(dataArray.count - dataArrayNAME.count)] ;
                                alertCon =[UIAlertController alertControllerWithTitle:str1 message:nil preferredStyle:UIAlertControllerStyleAlert];
                            }else{
                                alertCon =[UIAlertController alertControllerWithTitle:str1 message:nil preferredStyle:UIAlertControllerStyleAlert];
                            }
                            
                            [alertCon addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                
                            }]];
                            [SVProgressHUD dismiss];
                            [self presentViewController:alertCon animated:YES completion:nil];
                            
                        }
                        
                    });
                }];
            }
        });
        [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"fucck_two"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        [gat deleteDate:dataArray];
        
        
    }];
    
    
//    if ([gat checkDate].count  < GATHERMAX) {
//        [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"采集满%d个人才可以上传",GATHERMAX]];
//        return;
//    }
    
 


}

@end
