//
//  newsViewModel.m
//  Open
//
//  Created by gao on 2017/1/23.
//  Copyright © 2017年 黄江涛 . All rights reserved.
//

#import "newsViewModel.h"

@implementation newsViewModel
-(void)refreshDataCompletionHandle:(CompletionHandle)completionHandle
{
    _pageId=1;
    [self getDataFromNetCompleteHandle:completionHandle];
}
-(void)getMoreDataCompletionHandle:(CompletionHandle)completionHandle
{
//    if (self.isHasMore) {
//        _pageId +=1;
    _pageId +=1;
        [self getDataFromNetCompleteHandle:completionHandle];
//    }else
//    {
//        NSError *err =[NSError errorWithDomain:@"'" code:999 userInfo:@{NSLocalizedDescriptionKey:@"没有更多数据了"}];
//        completionHandle(err);
//    }
}
-(NSMutableArray *)arrayForNews
{
    if (!_arrayForNews) {
        _arrayForNews =[NSMutableArray new];
        [_arrayForNews removeAllObjects];
    }
    return _arrayForNews;
}
-(void)getDataFromNetCompleteHandle:(CompletionHandle)completionHandle{
    switch (_pageId) {
        case 1:
            if (_pageId ==1) {
                [self.dataArr removeAllObjects];
                self.dataTask =[NewsNetModel getMoreNewsForThisPath:nil  nssing:@"top" completionHandle:^(newsModel *model, NSError *error) {
                    if (!error) {
                        //        if(){
                        //            [self.dataArr removeAllObjects];
                        //        }else{
                        
                        [self.dataArr addObjectsFromArray:model.result.TRdata];
                      
                        
                        NSLog(@"%@",self.dataArr);
                      
                    }
                    completionHandle(error);
                }];
            }
            break;
        case 2:
            if (_pageId ==2) {
//                [self.dataArr removeAllObjects];
                self.dataTask =[NewsNetModel getMoreNewsForThisPath:nil  nssing:@"shehui" completionHandle:^(newsModel *model, NSError *error) {
                    if (!error) {
                        //        if(){
                        //            [self.dataArr removeAllObjects];
                        //        }else{
                        
                        [self.dataArr addObjectsFromArray:model.result.TRdata];
                        NSLog(@"%@",self.dataArr);
                        
                    }
                    completionHandle(error);
                }];
                
            }
            break;
        case 3:
            if (_pageId ==3) {
//                [self.dataArr removeAllObjects];
                self.dataTask =[NewsNetModel getMoreNewsForThisPath:nil  nssing:@"yule" completionHandle:^(newsModel *model, NSError *error) {
                    if (!error) {
                        //        if(){
                        //            [self.dataArr removeAllObjects];
                        //        }else{
                        
                        [self.dataArr addObjectsFromArray:model.result.TRdata];
                        NSLog(@"%@",self.dataArr);
                        
                    }
                    completionHandle(error);
                }];
            }
            break;
        case 4:
            if (_pageId ==4) {
//                [self.dataArr removeAllObjects];
                self.dataTask =[NewsNetModel getMoreNewsForThisPath:nil  nssing:@"tiyu" completionHandle:^(newsModel *model, NSError *error) {
                    if (!error) {
                        //        if(){
                        //            [self.dataArr removeAllObjects];
                        //        }else{
                        
                        [self.dataArr addObjectsFromArray:model.result.TRdata];
                        NSLog(@"%@",self.dataArr);
                        
                    }
                    completionHandle(error);
                }];
            }
            break;
        case 5:
            if (_pageId ==5) {
//                [self.dataArr removeAllObjects];
                self.dataTask =[NewsNetModel getMoreNewsForThisPath:nil  nssing:@"junshi" completionHandle:^(newsModel *model, NSError *error) {
                    if (!error) {
                        //        if(){
                        //            [self.dataArr removeAllObjects];
                        //        }else{
                        
                        [self.dataArr addObjectsFromArray:model.result.TRdata];
                        NSLog(@"%@",self.dataArr);
                        
                    }
                    completionHandle(error);
                }];
            }
            break;
        case 6:
            if (_pageId ==6) {
//                [self.dataArr removeAllObjects];
                self.dataTask =[NewsNetModel getMoreNewsForThisPath:nil  nssing:@"keji" completionHandle:^(newsModel *model, NSError *error) {
                    if (!error) {
                        //        if(){
                        //            [self.dataArr removeAllObjects];
                        //        }else{
                        
                        [self.dataArr addObjectsFromArray:model.result.TRdata];
                        NSLog(@"%@",self.dataArr);
                        
                    }
                    completionHandle(error);
                }];
            }
            break;
          
        case 7:
            if (_pageId ==7) {
//                [self.dataArr removeAllObjects];
                self.dataTask =[NewsNetModel getMoreNewsForThisPath:nil  nssing:@"caijing" completionHandle:^(newsModel *model, NSError *error) {
                    if (!error) {
                        //        if(){
                        //            [self.dataArr removeAllObjects];
                        //        }else{
                        
                        [self.dataArr addObjectsFromArray:model.result.TRdata];
                        NSLog(@"%@",self.dataArr);
                        
                    }
                    completionHandle(error);
                }];
            }
            break;
            
        case 8:
            if (_pageId ==8) {
//                [self.dataArr removeAllObjects];
                self.dataTask =[NewsNetModel getMoreNewsForThisPath:nil  nssing:@"shishang" completionHandle:^(newsModel *model, NSError *error) {
                    if (!error) {
                        //        if(){
                        //            [self.dataArr removeAllObjects];
                        //        }else{
                        
                        [self.dataArr addObjectsFromArray:model.result.TRdata];
                        NSLog(@"%@",self.dataArr);
                        
                    }
                    completionHandle(error);
                }];
            }
            break;
            
        default:
            break;
    }
    
    
    

}





-(newSDataModel *)modelForRows:(NSInteger)row{
    if (self.dataArr.count  >0) {
        return self.dataArr[row];
    }else{
        [SVProgressHUD showErrorWithStatus:@"网络信号差"];
        return nil;
    }
    
    
}
-(NSString *)titleForRow:(NSInteger)row
{
    return [self modelForRows:row].title;
    
    
}

-(NSString *)iconUrlForRow:(NSInteger)row
{
// return [NSURL URLWithString:[self modelForRows:row].URL];
     return [self modelForRows:row].URL;
}
- (BOOL)isHasMore{
    return _maxPageId > _pageId;
}
-(NSURL *)imageForRow:(NSInteger)row
{

    return [NSURL URLWithString:[self modelForRows:row].thumbnail_pic_s];
}
-(NSInteger)rowNumber{
    NSLog(@"%lu",(unsigned long)self.dataArr.count);
    return self.dataArr.count;
    
    
}




-(NSString*)ObjectToJsonString:(id)object{
    NSString *jsonString = [[NSString
                             alloc]init];
    NSError *error;
    NSData *jsonData = nil;
    if (object) {
        jsonData = [NSJSONSerialization
                    
                    
                    
                    dataWithJSONObject:object
                    
                    
                    
                    options:NSJSONWritingPrettyPrinted
                    
                    
                    
                    error:&error];
    }
    
    
    
    if (! jsonData) {
        
        NSLog(@"error: %@", error);
        
        
        
        
    } else {
        
        
        
        jsonString = [[NSString
                       
                       
                       
                       alloc] initWithData:jsonData
                      
                      
                      
                      encoding:NSUTF8StringEncoding];
        
        
        
    }
    
    
    
    NSMutableString *mutStr = [NSMutableString
                               
                               
                               
                               stringWithString:jsonString];
    
    
    
    NSRange range = {0,jsonString.length};
    
    
    
    [mutStr replaceOccurrencesOfString:@" "
     
     
     
                            withString:@""
     
     
     
                               options:NSLiteralSearch range:range];
    
    
    
    NSRange range2 = {0,mutStr.length};
    
    
    
    [mutStr replaceOccurrencesOfString:@"\n"
     
     
     
                            withString:@""
     
     
     
                               options:NSLiteralSearch range:range2];
    
    NSRange range3 = {0, mutStr.length};
    
    NSString * str = @"\\";
    
    [mutStr replaceOccurrencesOfString:str withString:@"" options:NSLiteralSearch range:range3];
    
    
    
    return jsonString;
    
}




@end
