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

#import "PostMessageViewModel.h"
#import "postMessageMNet.h"
#import "postMessageModel.h"
@implementation PostMessageViewModel
-(NSMutableArray *)arrayForNews
{
    if (!_arrayForNews) {
        _arrayForNews =[NSMutableArray new];
        [_arrayForNews removeAllObjects];
    }
    return _arrayForNews;
}
-(NSMutableArray *)arrayForPostChild
{
    if (!_arrayForPostChild) {
        _arrayForPostChild=[NSMutableArray new];
    }
    return _arrayForPostChild;
}


//- (void)getDataFromNetCompleteHandle:(CompletionHandle)completionHandle{
//    __weak __typeof(self)weakSelf = self;
//    [postMessageMNet getSuperCatalogcompletionHandle:^(postMessageModel *model, NSError *error) {
//        [weakSelf.arrayForNews addObjectsFromArray:model.Result];
//        NSLog(@"%@",_arrayForNews);
////        for (postMessageResultModel *model in collection) {
////            statements
////        }
//
//    }];
//}




-(void)getDataFromNetCompleteHandlePostTwo:(NSInteger)row Handle:(CompletionHandle)completionHandle
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  
    
    
    
    __weak __typeof(self)weakSelf = self;
 __block   NSString *docPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES).firstObject;
 __block   NSString *plistPath=[docPath stringByAppendingPathComponent:@"55shhasdbb.plist"];
           if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]){
    for (__weak NSInteger g = 1; g < 6; g++ ) {
        NSLog(@"ggggggggggggggggggg:%ld",g);
        [postMessageMNet getChildCatalog:g completionHandle:^(postMessageModel *model, NSError *error) {
            if (model.Result.count <=0) {
                self.hasMore =YES;
            }
          //得到数据
            for (postMessageResultModel *models in model.Result ) {
                [weakSelf.arrayForPostChild addObject:models.categoryname];
                NSLog(@"%@",weakSelf.arrayForPostChild);
                }
        NSLog(@"ggggggggggggggggggg:%ld",g);
                
            NSString *str2 =@"teacher";
         NSString *str3=   [ str2 stringByAppendingFormat:@"%ld",g ];
 self.hasMore =NO;
                NSDictionary *userDic=@{str3:weakSelf.arrayForPostChild};
                //写入
                [userDic writeToFile:plistPath atomically:YES];
                NSLog(@"list Path %@", plistPath);
             NSLog(@"userDic: %@", userDic);
               
                _str1 =@"";
 
            
             [weakSelf.arrayForPostChild removeAllObjects];
                       completionHandle(error);
                   }];
               }
           }
  });
}
-(void)getChildPostMess:(NSInteger)row Handle:(CompletionHandle)completionHandle
{
    [self getDataFromNetCompleteHandlePostTwo:row Handle:completionHandle];

    

}

#pragma mark - 获取postmessageTotal
-(void)refreshDataCompletionHandle:(CompletionHandle)completionHandle
{
    _pageId=1;
    [self getDataFromNetCompleteHandle:completionHandle];

}
-(void)getMoreDataCompletionHandle:(CompletionHandle)completionHandle
{
        if (self.isHasMore) {
            _pageId +=1;
    [self getDataFromNetCompleteHandle:completionHandle];
        }else
        {
            NSError *err =[NSError errorWithDomain:@"'" code:999 userInfo:@{NSLocalizedDescriptionKey:@"没有更多数据了"}];
            completionHandle(err);
        }
}
-(void)getDataFromNetCompleteHandle:(NSInteger)info PageW:(NSInteger)pageW andState:(NSInteger)state handle:(CompletionHandle)completionHandle{
 
    self.dataTask =[postMessageMNet findMyPublishInfo:info andPage:pageW andState:state com:^(postMessageModel *model, NSError *error) {
//         [self.dataArr removeAllObjects];
        if (model.Result.count <=0) {
            self.hasMore =YES;
        }
        for (postMessageResultModel *models in model.Result) {
           
            [self.dataArr addObject:models];
            NSLog(@"dataArr:%@",self.dataArr);
             self.hasMore =NO;
        
        }
           completionHandle(error);
    }];
}
//获取分类的数据

-(void)getOneParent:(NSString*)str1 andTwoChild:(NSString*)two Page:(NSInteger)page Handle:(CompletionHandle)completionHandle
{
 
    if (!str1) {
        str1 =[NSNull null];
    }
    if (!two) {
        two =[NSNull null];
    }
    
  
    self.dataTask =[postMessageMNet findPublishInfo:page andPro:[NSNull null] andCity:[NSNull null] andCountry:[NSNull null]andSuper:str1 andCate:two andL_x:100 andL_y:100 cH:^(postMessageModel *model, NSError *error) {
        
     
        
        
        if (model.Result.count <=0) {
            self.hasMore =YES;
        }
        
        
        for (postMessageResultModel *models in model.Result) {
            [self.dataArr addObject:models];
            NSLog(@"dataArr:%@",self.dataArr);
             self.hasMore =NO;
        }
        completionHandle(error);
    }];





}
#pragma mark - 获取postmessageFirst
-(void)getPostmessageFirstfindMyPublishInfo:(NSString*)str1 andPage:(NSString*)two andState:(NSString*)state Handle:(CompletionHandle)completionHandle
{
    [self.dataArr removeAllObjects];
    self.dataTask =[postMessageMNet findMyPublishInfo:18 andPage:1 andState:0 com:^(postMessageModel *model, NSError *error) {
        if (model.Result.count <=0) {
            self.hasMore =YES;
        }
        for (postMessageResultModel *models in model.Result) {
            [self.dataArr addObject:models];
            NSLog(@"dataArr:%@",self.dataArr);
            self.hasMore =NO;
        }
         completionHandle(error);

    }];
    
    
    
    
}
#pragma mark -删除消息的接口
-(void)userId:(NSInteger)userID andIdString:(NSString*)idStriing Handle:(CompletionHandle)completionHandle
{
    [self.dataArr removeAllObjects];
    self.dataTask =[postMessageMNet deleteForeverUserID:userID andIdString:idStriing com:^(postMessageModel *model, NSError *error) {
        
//        [self.dataArr addObject:model];
//            NSLog(@"dataArr:%@",self.dataArr);
    
        completionHandle(error);
        
    }];
    
    
}

#pragma mark -推荐的接口
-(void)parent:(NSString *)parent andCity:(NSString*)city andCountry:(NSString *)country andPage:(NSUInteger)page Handle:(CompletionHandle)completionHandle
{__weak __typeof(self)weakSelf = self;
//[self.dataArr removeAllObjects];
self.dataTask =[postMessageMNet recommendedInfopublishProvince:parent andpublishCity:city andpublishCountry:country andpage:page H:^(postMessageModel *model, NSError *error) {
    if (model.Result.count <=0) {
        self.hasMore =YES;
    }else
    {
           self.hasMore =NO;
    }
    
    for (postMessageResultModel *models in model.Result) {
        [weakSelf.dataArr addObject:models];
        NSLog(@"dataArr:%@",weakSelf.dataArr);
    }
    completionHandle(error);
    
    
}];
}







@end
