//
//  DialogueHttpPostCmd+Response.m
//  MobilePortal
//
//  Created by caiyang on 2020/4/10.
//  Copyright © 2020 yin chen. All rights reserved.
//

#import "DialogueHttpPostCmd+Response.h"
#import "DialogueHttpPostCmd.h"
#import "DialogueHttpPostResult.h"

@protocol DialogueHttpPostResultDataList;
@interface DialogueHttpPostResultDataList : JSONModel
@property NSString<Optional>*id;
@property NSString<Optional>*title;
@property NSString<Optional>*status;
@property NSString<Optional>*createon;
@property NSString<Optional>*createby;
@property NSString<Optional>*createbyname;
@property NSString<Optional>*image;
@property NSString<Optional>*comments;
@property NSString<Optional>*orderType;
@property NSString<Optional>*type;
@property NSString<Optional>*foldertype;
@property NSString<Optional>*typeid;
@property NSString<Optional>*typename;
@property NSString<Optional>*imagecategory;
@property NSString<Optional>*sort;
@property NSString<Optional>*homeshow;
@end
@implementation DialogueHttpPostResultDataList
@synthesize description;
@end


@protocol DialogueHttpPostResultData;
@interface DialogueHttpPostResultData : JSONModel
@property NSMutableArray<Optional,DialogueHttpPostResultDataList> *list;
@end
@implementation DialogueHttpPostResultData
@synthesize description;
@end


@interface DialogueHttpPostResult()
@property DialogueHttpPostResultData *data;
@end

@implementation DialogueHttpPostCmd (Response)

- (void)onRequestSuccess:(id)request code:(NSInteger)code{
    
    [self.result setResultState:kRequestResultSuccess];
    NSDictionary *dic = (NSDictionary *)request;
    DialogueHttpPostResult *result = [[DialogueHttpPostResult alloc]init];
    result = (DialogueHttpPostResult *)self.result;
    if (code>199&&code<299) {
        [self.result setResultState:kRequestResultSuccess];
        NSError *error;
        result.data = [[DialogueHttpPostResultData 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 DialogueHttpPostResult
- (NSMutableArray *)getDialogueHttpPostResult{
    NSMutableArray *httpArray = [NSMutableArray array];
    DialogueListModel *listModel = [[DialogueListModel alloc]init];
    for (int index = 0; index < self.data.list.count; index++) {
        DialogueListItemModel *model = [[DialogueListItemModel alloc]init];
        DialogueHttpPostResultDataList *httpModel = self.data.list[index];
        model.id = [self getNilString:httpModel.id];
        model.typeid = [self getNilString:httpModel.typeid];
        model.createby = [self getNilString:httpModel.createby];
        model.comments = [self getNilString:httpModel.comments];
        model.typename = [self getNilString:httpModel.typename];
        model.sort = [self getNilString:httpModel.sort];
        model.createbyname = [self getNilString:httpModel.createbyname];
        model.type = [self getNilString:httpModel.type];
        model.image = [self getNilString:httpModel.image];
        model.homeshow = [self getNilString:httpModel.homeshow];
        model.createon = [self getNilString:httpModel.createon];
        model.foldertype = [self getNilString:httpModel.foldertype];
        model.title = [self getNilString:httpModel.title];
        model.typeid = [self getNilString:httpModel.typeid];
        model.typeid = [self getNilString:httpModel.typeid];
        if (![listModel.titleArray containsObject:model.typename]) {
            [listModel.titleArray addObject:model.typename];
        }
//        NSLog(@"%@",model.typename);
        [listModel.itemModelArray addObject:model];
    }
    
    [httpArray addObject:listModel];
    return httpArray;
}
@end
