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

#import "gatherUtil.h"

@implementation gatherUtil

-(instancetype)initWithGG
{
    self = [super init];
    if (self ) {
    }
    return self;
}


-(instancetype)init{
    
    self =[super init];
    if (self) {
        //1.使用userdefault
//    NSInteger  numberNUll =   [[NSUserDefaults standardUserDefaults] integerForKey:@"fucck_two"];
        NSString *plistPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"gather.plist"];
        //2. 判断userdefault 的值存不存在,如果存在就读取,不存在就创建
        
        
        
        if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
            //创建一个为0的
      //删除以前的表格
            NSFileManager *fileManager = [NSFileManager defaultManager];
            [fileManager removeItemAtPath:plistPath error:nil];
            NSDictionary *statusDict = @{@"indexNumber":@0};
            [statusDict writeToFile:plistPath atomically:YES];
        //可变数组是无法写入到plist文件的
           
        [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"fucck_two"];
             [[NSUserDefaults standardUserDefaults] synchronize];
             self.index = [[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"];
        }else{
            //变量已经保存成功的状态
//                    NSString *plistPaths=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"gather.plist"];
//              NSMutableDictionary *userDics=[NSMutableDictionary dictionaryWithContentsOfFile:plistPaths];
//
//            NSLog(@"%@",[userDics allKeys]);
//
//
//      gather   *models =       [gather mj_objectWithKeyValues:[userDics valueForKey:@"indexNumber"]];
            if([[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"] >( GATHERMAX+1)){
                //这里监听有没有上传,如果上传了就给它一个权限将他设置为0;
                
              
            }else{
        
      
      NSInteger indexpage =     [[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"];
            indexpage +=1;
                   [[NSUserDefaults standardUserDefaults] setInteger:indexpage forKey:@"fucck_two"];
            [[NSUserDefaults standardUserDefaults] synchronize];
                [self.delegate sendFaceImage:indexpage];
            NSLog(@"%ld",(long)indexpage);
            self.index =indexpage;
            
            }
            
            
            
            
        }
        
      
        
        
        
        
    }
    return self;
    
}
+ (gatherUtil *)sharedInstance{
    static gatherUtil *p = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        p=[[gatherUtil alloc]init];
    });
    return p;
}

//增加数据







-(void)addDate:(gather *)gatherModel{
    if([[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"] >=( GATHERMAX+1)){
        dispatch_async(dispatch_get_main_queue(), ^{
  
        });
        return;
    }

    //路径
    NSString *plistPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"gather.plist"];
    NSString *userindex = [NSString stringWithFormat:@"user_%ld",(long)self.index];

    
      //如果为21的话不要保存提醒必须要全部上传才能使用这个功能
    NSMutableDictionary *userDics=[NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
//    NSInteger index_ =  (NSInteger)[userDics objectForKey:@"indexNumber"];
    NSLog(@"%ld",(long)[[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"]);
    [userDics setObject:gatherModel.mj_keyValues forKey:userindex];
    [userDics writeToFile:plistPath atomically:YES];
////增加数据的代码
    
       [[NSNotificationCenter defaultCenter]postNotificationName:@"gatherABC" object:@((long)[[NSUserDefaults standardUserDefaults]  integerForKey:@"fucck_two"])];
        [self  returnAddressDate:gatherModel];
    
    
    
    /*上传的接口
     *需要等会取消注释
     *
     */
    

}




-(NSArray *)readAddress{
    
    
    
    NSString *paths=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"gather.plist"];
    //如果要更改，那么要使用可变字典或可变数组来读
    NSMutableDictionary *userDic=[NSMutableDictionary dictionaryWithContentsOfFile:paths];
    NSMutableArray *userArray = [NSMutableArray new];
    [userArray  removeAllObjects];
    if(!userDic[@"address"][@"provinceGather"]){
        return nil ;
    }
    [userArray addObject:userDic[@"address"][@"provinceGather"] ];
      [userArray addObject:userDic[@"address"][@"city_name"] ];
      [userArray addObject:userDic[@"address"][@"country_name"] ];
      [userArray addObject:userDic[@"address"][@"townName"] ];
      [userArray addObject:userDic[@"address"][@"village_name"] ];

    return [userArray copy];
}



-( void)returnAddressDate:(gather *)gatherModel
{
    
    NSString *plistPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"gather.plist"];
    NSString *userindex = [NSString stringWithFormat:@"address"];
    NSMutableDictionary *userDics=[NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
    [userDics setObject:gatherModel.mj_keyValues forKey:userindex];
    [userDics writeToFile:plistPath atomically:YES];

}




-(void)deleteDate:(NSArray*)gatherModel{
    
//    NSString *paths=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"gather.plist"];
//    //如果要更改，那么要使用可变字典或可变数组来读
//    NSMutableDictionary *userDic=[NSMutableDictionary dictionaryWithContentsOfFile:paths];
//    NSString *userindex = [NSString stringWithFormat:@"user_%ld",(long)gatherModel+1];
//    [userDic removeObjectForKey:userindex];
//  [userDic writeToFile:paths atomically:YES];
    
    
    NSString *paths=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"gather.plist"];
        NSMutableDictionary *userDics=[NSMutableDictionary dictionaryWithContentsOfFile:paths];
  
    for (int i = 0 ; i < gatherModel.count; i++) {
              [userDics removeObjectForKey:[NSString stringWithFormat:@"user_%d",i+1]];
    }
        [userDics writeToFile:paths atomically:YES];
    
//    NSFileManager *fileManager = [NSFileManager defaultManager];
//    [fileManager removeItemAtPath:paths error:nil];
    
}

//查询
//做项目的一些经验
//第四点:在 dispatch_async(dispatch_get_main_queue(), ^{执行
//
//
-(NSArray*)checkDate{
    
  
        NSString *paths=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"gather.plist"];
        //如果要更改，那么要使用可变字典或可变数组来读
        NSMutableDictionary *userDic=[NSMutableDictionary dictionaryWithContentsOfFile:paths];
   NSMutableArray *userArray = [NSMutableArray new];
        [userArray  removeAllObjects];
    NSFileManager *file_manager = [NSFileManager defaultManager];
if( [file_manager fileExistsAtPath:paths]){
//    if ( ![gather mj_objectWithKeyValues:[userDic valueForKey:[NSString stringWithFormat:@"user_%d",0]]]) {
//        [SVProgressHUD showErrorWithStatus:@"未知错误"];
//        return  nil;
//    }
    
    for (int i =0 ; i <userDic.allKeys.count-2 ; i ++) {
        NSString *nss =[NSString stringWithFormat:@"user_%ld",(long)i+1];
        gather   *model =     [gather mj_objectWithKeyValues:[userDic valueForKey:nss]];
        [userArray addObject:model];
        NSLog(@"%@",model);
        NSLog(@"%@",userDic[@"user_1"][@"Name"]);
    }
    
}
    

//排序
//        NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch|NSNumericSearch|
//        NSWidthInsensitiveSearch|NSForcedOrderingSearch;
//        NSComparator sort = ^(NSString *obj1,NSString *obj2){
//            NSRange range = NSMakeRange(0,obj1.length);
//            return [obj1 compare:obj2 options:comparisonOptions range:range];
//        };
//        NSArray *resultArray2 = [[[charArray sortedArrayUsingComparator:sort] reverseObjectEnumerator]allObjects];

    return userArray;
 
}
//修改
-(void)updateDate{}

//网络是否存在

-(void)IsNetWork:(NetWorkIsWorking)completionHandle
{
    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];

    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
     
        switch (status) {
            case AFNetworkReachabilityStatusReachableViaWWAN:
            case AFNetworkReachabilityStatusReachableViaWiFi:
                    completionHandle();
                break;
            case AFNetworkReachabilityStatusNotReachable:
            default:
                [SVProgressHUD showErrorWithStatus:@"网络状态差"];
                break;
        }
    }];
        [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
}







@end



