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

#import "VerifyTokenHttpPostCmd+Response.h"
#import "VerifyTokenHttpPostCmd.h"
#import "VerifyTokenHttpResult.h"


@protocol VerifyTokenHttpResultMessage;
@interface VerifyTokenHttpResultMessage : JSONModel
@property NSString<Optional>*name;
@property NSString<Optional>*count;
@property NSString<Optional>*content;
@property NSString<Optional>*hint;
@end
@implementation VerifyTokenHttpResultMessage
@synthesize description;
@end

@protocol VerifyTokenHttpResultInfo;
@interface VerifyTokenHttpResultInfo : JSONModel
@property NSString<Optional>*token;
@property NSString<Optional>*userId;
@end
@implementation VerifyTokenHttpResultInfo
@synthesize description;
@end


@protocol VerifyTokenHttpResultDataData;
@interface VerifyTokenHttpResultDataData : JSONModel
@property VerifyTokenHttpResultInfo<Optional>*data;
@property VerifyTokenHttpResultMessage<Optional>*message;
@end
@implementation VerifyTokenHttpResultDataData
@synthesize description;
@end

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

@interface VerifyTokenHttpResult()
@property VerifyTokenHttpResultData *data;
@end


@implementation VerifyTokenHttpPostCmd (Response)

- (void)onRequestSuccess:(id)request code:(NSInteger)code{
    
    [self.result setResultState:kRequestResultSuccess];
    NSDictionary *dic = (NSDictionary *)request;
    VerifyTokenHttpResult *result = [[VerifyTokenHttpResult alloc]init];
    result = (VerifyTokenHttpResult *)self.result;
    if (code>199&&code<299) {
        [self.result setResultState:kRequestResultSuccess];
        NSError *error;
        result.data = [[VerifyTokenHttpResultData 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 VerifyTokenHttpResult
- (NSMutableArray *)getVerifyTokenHttpResult{
    NSMutableArray *httpArray = [NSMutableArray array];
    if ([self.data.code isEqualToString:@"200"]) {
        [AccountInfo sharedInstance].singlePoint_access_token = self.data.data.data.token;
        [AccountInfo sharedInstance].UserId = self.data.data.data.userId;
    } else if ([self.data.code isEqualToString:@"501"]) {
        if ([[self getNilString:self.data.data.message.content] isEqualToString:@""]) {
            [SMAlertView showOnlyButtonAlert:[NSString stringWithFormat:@"%@",self.data.msg] title:@"IDM登录失败"];
            [httpArray addObject:self.data.msg];
            return httpArray;
        }
        [httpArray addObject:self.data.data.message.content];
        if ([self.data.data.message.content isEqualToString:@"002"]) {
            [httpArray addObject:self.data.data.message.hint];
        } else if ([self.data.data.message.content isEqualToString:@"001"]) {
//            [SMAlertView showOnlyButtonAlert:[NSString stringWithFormat:@"错误名称：%@\n错误次数：%@\n错误代码：%@",self.data.data.message.hint,self.data.data.message.count,self.data.data.message.content] title:@"IDM登录失败"];
            [SMAlertView showOnlyButtonAlert:[NSString stringWithFormat:@"(%@)%@，您还有：%@次重试机会",self.data.data.message.content,self.data.data.message.hint,self.data.data.message.count] title:@"IDM登录失败"];
        } else {
            [SMAlertView showOnlyButtonAlert:[NSString stringWithFormat:@"(%@)%@",self.data.data.message.content,self.data.data.message.hint] title:@"IDM登录失败"];
        }
    } else {
//        [SMAlertView showOnlyButtonAlert:[NSString stringWithFormat:@"%@",self.data.msg] title:@"登录失败！"];
        [SMAlertView showAlert:self.data.msg];
        [httpArray addObject:self.data.msg];
    }
    return httpArray;
}
@end
