//
//  IDMWeChatBindingHttpPostCmd+Response.m
//  MobilePortal
//
//  Created by caiyang on 2020/12/7.
//  Copyright © 2020 yin chen. All rights reserved.
//

#import "IDMWeChatBindingHttpPostCmd+Response.h"
#import "IDMWeChatBindingHttpPostCmd.h"
#import "IDMWeChatBindingHttpResult.h"

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

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


@protocol IDMWeChatBindingHttpResultDataData;
@interface IDMWeChatBindingHttpResultDataData : JSONModel
@property IDMWeChatBindingHttpResultInfo<Optional>*data;
@property IDMWeChatBindingHttpResultMessage<Optional>*message;
@property NSString<Optional>*loginType;
@property NSString<Optional>*userId;
@property NSString<Optional>*tokenId;

@end
@implementation IDMWeChatBindingHttpResultDataData
@synthesize description;
@end

@protocol IDMWeChatBindingHttpResultData;
@interface IDMWeChatBindingHttpResultData : JSONModel
@property NSString<Optional>*code;
@property NSString<Optional>*msg;

@property IDMWeChatBindingHttpResultDataData<Optional>*data;
@end
@implementation IDMWeChatBindingHttpResultData
@synthesize description;
@end

@interface IDMWeChatBindingHttpResult()
@property IDMWeChatBindingHttpResultData *data;
@end

//@interface IDMWeChatBindingHttpResult()
//@property IDMWeChatBindingHttpResultData *data;
//@end


@implementation IDMWeChatBindingHttpPostCmd (Response)

- (void)onRequestSuccess:(id)request code:(NSInteger)code{
    
    [self.result setResultState:kRequestResultSuccess];
    NSDictionary *dic = (NSDictionary *)request;
    IDMWeChatBindingHttpResult *result = [[IDMWeChatBindingHttpResult alloc]init];
    result = (IDMWeChatBindingHttpResult *)self.result;
    if (code>199&&code<299) {
        [self.result setResultState:kRequestResultSuccess];
        NSError *error;
        result.data = [[IDMWeChatBindingHttpResultData 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 IDMWeChatBindingHttpResult
- (NSMutableArray *)getIDMWeChatBindingHttpResult{
    NSMutableArray *httpArray = [NSMutableArray array];
    if ([self.data.code isEqualToString:@"200"]) {
        if (![self.data.data.loginType isEqualToString:@"3"]) {
            [AccountInfo sharedInstance].UserId = self.data.data.userId;
            [AccountInfo sharedInstance].loginType = @"2";
        } else {
            [AccountInfo sharedInstance].singlePoint_access_token = self.data.data.tokenId;
            [AccountInfo sharedInstance].UserId = self.data.data.userId;
            [AccountInfo sharedInstance].loginType = @"1";
        }
    } 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:@"(%@)%@，您还有：%@次重试机会",self.data.data.message.content,self.data.data.message.hint,self.data.data.message.count] title:@"IDM登录失败"];
        } else if ([self.data.data.message.content isEqualToString:@"101"]) {
            [[NSNotificationCenter defaultCenter] postNotificationName: @"gotoWeChatBinding" object: nil];
//            [SMAlertView showOnlyButtonAlert:[NSString stringWithFormat:@"(%@)%@",self.data.data.message.content,self.data.data.message.hint] title:@"IDM微信登陆失败"];
        } else {
            [SMAlertView showOnlyButtonAlert:[NSString stringWithFormat:@"(%@)%@",self.data.data.message.content,self.data.data.message.hint] title:@"IDM登录失败"];
        }
    }  else if ([self.data.code isEqualToString:@"202"]){
        [httpArray addObject:self.data.msg];
        [[NSNotificationCenter defaultCenter] postNotificationName: @"gotoWeChatBinding" object: nil];
    }
    else {
        [SMAlertView showAlert:self.data.msg];
        [httpArray addObject:self.data.msg];
    }
    return httpArray;
}
@end
