//
//  OffcnIMSqliteTestVC.m
//  OffcnIMSDKiOS
//
//  Created by vockey on 2020/9/1.
//  Copyright © 2020 offcn. All rights reserved.
//

#import "OffcnIMSqliteTestVC.h"
#import "OffcnIMZYJFMDBHandler.h"
#import "OffcnIMModel.h"
#import "OffcnZYJTestDataStorageView.h"

@interface OffcnIMSqliteTestVC ()

@property (strong, nonatomic) NSString *fromId;
@property (strong, nonatomic) NSString *toId;
@property (assign, nonatomic) UIClearStyle clearStyle;
@end

@implementation OffcnIMSqliteTestVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"数据库";
    self.fromId = @"ce0819db8391f1e7258a71cc9e2c3235";
    self.toId = @"ceb7383519c5b6a1c8579553f01ad9c4";
    self.clearStyle = UIClearStylePart;
}

//删除记录
- (IBAction)deleteRecord:(UIButton *)sender {
   
    [[OffcnIMZYJFMDBHandler sharedInstance] deleteUserChatDataWithFromUser:self.fromId toUser:self.toId clearStyle:self.clearStyle callBack:^(BOOL isSuccess) {
        
    }];
}

//删除表格
- (IBAction)deleteTable:(UIButton *)sender {
    
    [[OffcnIMZYJFMDBHandler sharedInstance] deleteDataTable];
}

//查询聊天列表
- (IBAction)queryChatList:(UIButton *)sender {
    
    [[OffcnIMZYJFMDBHandler sharedInstance] getChatListArrayCallback:^(NSArray<OffcnIMBodyModel *> *messagesArray) {
        
        NSString *msg = @"";
        for (int i=0; i<messagesArray.count; i++) {
            OffcnIMBodyModel *model = messagesArray[i];
            msg = [msg stringByAppendingString:[NSString stringWithFormat:@"msg_type:%ld,lastSendtime:%@,lastmessage:%@",(long)model.msg_type,model.lastSendtime,model.lastmessage]];
            
            msg = [msg stringByAppendingString:@"\r\n"];
            msg = [msg stringByAppendingString:@"\r\n"];
        }
        
        OffcnZYJTestDataStorageView *storageView = [[OffcnZYJTestDataStorageView alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height-20)];
        storageView.msg = msg;
        [storageView showView];
    }];
}

//查询聊天记录
- (IBAction)queryChat:(UIButton *)sender {
    
    [[OffcnIMZYJFMDBHandler sharedInstance] getChatRecordArrayWithFromUser:self.fromId toUser:self.toId limit:@"20" startIndex:@"0" callback:^(NSArray<OffcnIMBodyModel *> *messagesArray) {
        
        NSString *msg = @"";
        for (int i=0; i<messagesArray.count; i++) {
            OffcnIMBodyModel *model = messagesArray[i];
            if (model.msg_type == 1) {
                msg = [msg stringByAppendingString:model.msg.msg?model.msg.msg:@"nil"];
            }else{
                msg = [msg stringByAppendingString:model.msg.url?model.msg.url:@"nil"];
            }
            
            msg = [msg stringByAppendingString:@"\r\n"];
            msg = [msg stringByAppendingString:@"\r\n"];
        }
        
        OffcnZYJTestDataStorageView *storageView = [[OffcnZYJTestDataStorageView alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height-20)];
        storageView.msg = msg;
        [storageView showView];
    }];
}

@end
