//
//  NewVersionUpdateHttpCmd+Response.m
//  MobilePortal
//
//  Created by xiejiangbo on 2019/3/21.
//  Copyright © 2019 yin chen. All rights reserved.
//

#import "NewVersionUpdateHttpCmd+Response.h"
#import "NewVersionUpdateHttpResult.h"

@protocol NewVersionUpdateHttpResultDataList;
@interface NewVersionUpdateHttpResultDataList : JSONModel

@property NSString<Optional>*title;
@property NSString<Optional>*comments;
@property NSString<Optional>*msg;

@end
@implementation NewVersionUpdateHttpResultDataList
@synthesize description;
@end


@protocol NewVersionUpdateHttpResultData;
@interface NewVersionUpdateHttpResultData : JSONModel
@property NSMutableArray<Optional,NewVersionUpdateHttpResultDataList> *list;
@property NSString<Optional>*status;
@property NSString<Optional>*responseTime;
@property NSString<Optional>*msg;

@end
@implementation NewVersionUpdateHttpResultData
@synthesize description;
@end


@interface NewVersionUpdateHttpResult()
@property NewVersionUpdateHttpResultData *data;
@end

@implementation NewVersionUpdateHttpCmd (Response)
- (void)onRequestSuccess:(id)request code:(NSInteger)code{
    
    [self.result setResultState:kRequestResultSuccess];
    NSDictionary *dic = (NSDictionary *)request;
    NewVersionUpdateHttpResult *result = [[NewVersionUpdateHttpResult alloc]init];
    result = (NewVersionUpdateHttpResult *)self.result;
    if (code>199&&code<299) {
        [self.result setResultState:kRequestResultSuccess];
        NSError *error;
        result.data = [[NewVersionUpdateHttpResultData alloc]initWithDictionary:dic error:(NSError **)&error];
        if(error){
            [self onRequestFailed:error];
            return;
        }
    }else{
        NSString *memo = [dic objectForKey:@"error_description"];
        [self.result setResultState:kRequestResultFail];
        [self.result setErrMsg:memo];
        NSLog(@"code :%ld errormsg:%@",(long)code,memo);
    }
}
- (void)onRequestFailed:(NSError *)error{
    NSLog(@"Error===%@",error);
    [self.result setErrMsg:[error localizedDescription]];
    [self.result setResultState:kRequestResultFail];
}
@end

@interface NewVersionUpdateHttpResult ()<UIAlertViewDelegate>
@property(strong,nonatomic)NSString*updownurl;
@end

@implementation NewVersionUpdateHttpResult
- (NSMutableArray *)getNewVersionUpdateHttpResult{
    NSMutableArray *httpArray = [NSMutableArray array];
    for (int index = 0; index<self.data.list.count; index++) {
        NewVersionUpdateModel *model = [[NewVersionUpdateModel alloc]init];
        NewVersionUpdateHttpResultDataList *listModel = self.data.list[index];
        model.downloadLink = listModel.comments;
        model.versionNumber = listModel.title;
        NSString*systemversion=[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
//        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
//        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
//        NSNumber *floatVersion  = [numberFormatter numberFromString:listModel.title];
//        NSNumber *floatsyestemVersion = [numberFormatter numberFromString:systemversion];
//        NSComparisonResult r = [floatVersion  compare:floatsyestemVersion];
//        if (r == NSOrderedDescending) {
//            [httpArray addObject:model];
//        }
        if (![model.versionNumber isEqualToString:systemversion]) {
            [httpArray addObject:model];
        }
    }
    return httpArray;
}
@end

