//
//  MessageJsonHttpPostCmd+Response.m
//  MobilePortal
//
//  Created by caiyang on 2021/11/9.
//  Copyright © 2021 yin chen. All rights reserved.
//

#import "MessageJsonHttpPostCmd+Response.h"
#import "MessageJsonHttpPostCmd.h"
#import "MessageJsonHttpResult.h"


@protocol MessageJsonHttpResultData;
@interface MessageJsonHttpResultData : JSONModel
@property NSString<Optional>*code;
@property NSString<Optional>*msg;
@end
@implementation MessageJsonHttpResultData
@synthesize description;
@end

@interface MessageJsonHttpResult()
@property MessageJsonHttpResultData *data;
@end


@implementation MessageJsonHttpPostCmd (Response)

- (void)onRequestSuccess:(id)request code:(NSInteger)code{
    
    [self.result setResultState:kRequestResultSuccess];
    NSDictionary *dic = (NSDictionary *)request;
    MessageJsonHttpResult *result = [[MessageJsonHttpResult alloc]init];
    result = (MessageJsonHttpResult *)self.result;
    if (code>199&&code<299) {
        [self.result setResultState:kRequestResultSuccess];
        NSError *error;
        result.data = [[MessageJsonHttpResultData 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

@implementation MessageJsonHttpResult
- (NSMutableArray *)getMessageJsonHttpResult{
    NSMutableArray *httpArray = [NSMutableArray array];
    if ([self.data.code isEqualToString:@"200"]) {
        httpArray = [NSMutableArray array];
    } else {
        [httpArray addObject:self.data.msg];
    }
    return httpArray;
}
@end
