//
//  WeChatXinfoHttpPostCmd+Response.m
//  MobilePortal
//
//  Created by caiyang on 2020/5/15.
//  Copyright © 2020 yin chen. All rights reserved.
//

#import "WeChatXinfoHttpPostCmd+Response.h"
#import "WeChatXinfoHttpPostCmd.h"
#import "WeChatXinfoHttpResult.h"

@protocol WeChatXinfoHttpResultDataData;
@interface WeChatXinfoHttpResultDataData : JSONModel
@property NSString<Optional>*msg;
@property NSString<Optional>*userid;
@property NSString<Optional>*status;
@end
@implementation WeChatXinfoHttpResultDataData
@synthesize description;
@end

@protocol WeChatXinfoHttpResultData;
@interface WeChatXinfoHttpResultData : JSONModel
//@property WeChatXinfoHttpResultDataData *data;
@property NSMutableArray<Optional,WeChatXinfoHttpResultDataData> *data;
@property NSString<Optional>*msg;
@property NSString<Optional>*code;
@property NSString<Optional>*success;
@end
@implementation WeChatXinfoHttpResultData
@synthesize description;
@end

@interface WeChatXinfoHttpResult()
@property WeChatXinfoHttpResultData *data;
@end


@implementation WeChatXinfoHttpPostCmd (Response)
- (void)onRequestSuccess:(id)request code:(NSInteger)code{
    
    [self.result setResultState:kRequestResultSuccess];
    NSDictionary *dic = (NSDictionary *)request;
    WeChatXinfoHttpResult *result = [[WeChatXinfoHttpResult alloc]init];
    result = (WeChatXinfoHttpResult *)self.result;
    if (code>199&&code<299) {
        [self.result setResultState:kRequestResultSuccess];
        NSError *error;
        result.data = [[WeChatXinfoHttpResultData 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 WeChatXinfoHttpResult
- (NSMutableArray *)getWeChatXinfoHttpResult{
    NSMutableArray *httpArray = [NSMutableArray array];
    for (int index = 0; index < self.data.data.count; index ++) {
        WeChatXinfoHttpResultDataData *data = self.data.data[index];
        [httpArray addObject:data.msg];
        if ([data.msg isEqualToString:@"success"]) {
            httpArray = [NSMutableArray array];
            [AccountInfo sharedInstance].UserId = data.userid;
        }
    }
    return httpArray;
}
@end
