//
//  YMIMMessageCollectionView.m
//  PCDBank
//
//  Created by Gusont on 2021/8/25.
//  Copyright © 2021 SLAiELTS. All rights reserved.
//

#import "YMIMMessageCollectionView.h"
#import "YMIMBaseMessageCell.h"
#import "UICollectionView+YMHelper.h"

@interface YMIMMessageCollectionView()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate>


@end

@implementation YMIMMessageCollectionView

- (void)dealloc
{
    [self saveChatData];
}

- (void)saveChatData {
    NSString *key = [NSString stringWithFormat:@"%@--to--%@",[SLGlobalInfo SharedInstance].loginInfo.user.userId, self.friendModel.userId];
    NSArray *dictArr = [NSObject mj_keyValuesArrayWithObjectArray:self.chatDataManager];
    [[NSUserDefaults standardUserDefaults] setObject:dictArr forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupMessageCollectionView];
    }
    return self;
}

- (void)setupMessageCollectionView
{
    [self.messageCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    [self.messageCollectionView registerCellWithReuseIdentifierClass:YMIMBaseMessageCell.class];
}

- (void)applicationEnterBackground:(id)noti
{
//    if ([_iFlySpeechSynthesizer isSpeaking]) {
//        [_iFlySpeechSynthesizer pauseSpeaking];
//    }
}

- (CGFloat)getBotttomCellMaxY
{
    if (self.chatDataManager.count > 0) {
        NSIndexPath *idxPath = [NSIndexPath indexPathForItem:self.chatDataManager.count - 1 inSection:0];
        UICollectionViewCell *cell = [self.messageCollectionView cellForItemAtIndexPath:idxPath];
        CGRect rect = [self.messageCollectionView convertRect:cell.frame toView:self.messageCollectionView];
        return CGRectGetMaxY(rect);
    }
    return 0;
}

- (void)scrollToBottom:(BOOL)animated
{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self.chatDataManager.count > 0) {
            NSIndexPath *endIndex = [NSIndexPath indexPathForItem:self.chatDataManager.count - 1 inSection:0];
            [self.messageCollectionView scrollToItemAtIndexPath:endIndex atScrollPosition:UICollectionViewScrollPositionBottom animated:animated];
        }
    });
}
///Q
- (void)appendMessage:(MessageModel *)message
{
    MessageModel *lastModel = self.chatDataManager.lastObject;
    if ((lastModel && message.sendTime - lastModel.sendTime > 120) || !lastModel) {
        message.showTime = YES;
    }
    [self appendRequestMessage:message];
    [self resultsRequestWith:message];
}
///A
- (void)appendRequestMessage:(MessageModel *)message
{
    [self.chatDataManager addObject:message];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.messageCollectionView reloadData];
        [self scrollToBottom:YES];
    });
}

/// 结果请求数据
/// @param message <#message description#>
- (void)resultsRequestWith:(MessageModel *)message
{
    __block MessageModel *msgModel = message;
    NSString *msg = message.sendText;
    if (msg.length > 0) {
        dispatch_async(dispatch_get_main_queue(), ^{
//            MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self animated:YES];
//            hub.label.text = @"正在发送...";
            [[SLHttpCenter SharedInstance] getWithUrl:@"/api/Chat/SendMessage" parameter:@{@"chatType" : @"0", @"toId" : message.toId ?: @"", @"message" : message.sendText} success:^(id responseObject) {
//                [hub hideAnimated:YES];
                if (msgModel.sendFailure) {
                    msgModel.sendFailure = NO;
                }
                NSDictionary *dataDict = [responseObject objectForKey:@"data"];
                NSString *dataStr = [dataDict objectForKey:@"data"];
                NSString *str = [self replaceHasPrefix:@"\n" str:dataStr];
                MessageModel *model = [MessageModel initWithSendText:str isReceive:YES showMike:message.isMike showText:!message.isMike isMike:message.isMike];
                [self appendRequestMessage:model];
            } failure:^(SPRequestError *error) {
//                [hub hideAnimated:YES];
                NSInteger i = [self.chatDataManager indexOfObject:msgModel];
                if (i < self.chatDataManager.count && i > -1) {
                    msgModel.sendFailure = YES;
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [self updateMessage:msgModel idx:i];
                    });
                }
            }];
        });
    }
}

- (NSString *)replaceHasPrefix:(NSString *)fixStr str:(NSString *)str {
    if ([str hasPrefix:fixStr]) {
        NSString *newStr = [str substringFromIndex:fixStr.length];
        return [self replaceHasPrefix:fixStr str:newStr];
    }
    return str;
}

- (void)setVoiceStatus:(BOOL)voiceStatus
{
    _voiceStatus = voiceStatus;
}

#pragma mark - 语音合成
- (void)startSpeakingMessage:(NSString *)responseString
{

}

- (void)insertMessage:(NSArray *)message
{
    [self.chatDataManager addObjectsFromArray:message];
    [self.messageCollectionView reloadDataNoScroll];
}

- (void)updateMessage:(MessageModel *)message idx:(NSInteger)idx
{
    if (self.chatDataManager.count > 1) {
        [self.chatDataManager replaceObjectAtIndex:idx withObject:message];
        [self.messageCollectionView reloadData];
    }
}

- (void)removeMessage:(MessageModel *)message
{
    NSInteger idx = [self.chatDataManager indexOfObject:message];
    if (message.showTime && self.chatDataManager.count > idx + 1) {
        MessageModel *nextModel = [self.chatDataManager objectAtIndex:idx + 1];
        nextModel.showTime = YES;
    }
    [self.chatDataManager removeObject:message];
    [self.messageCollectionView reloadDataNoScroll];
}

- (void)removeAllMessages
{
    [self.chatDataManager removeAllObjects];
    [self.messageCollectionView reloadData];
}

#pragma mark - UICollectionViewDataSource
- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    YMIMBaseMessageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(YMIMBaseMessageCell.class) forIndexPath:indexPath];

    MessageModel *model = [self.chatDataManager objectAtIndex:indexPath.item];
    [cell confignBaseMessageContentWith:model receiveHeardImg:self.friendModel.userHead];
    [cell setCellLayerCornerRadiusWith:model];
    WS(weakSelf);
    cell.segmentBlock = ^(SLMikeSegmentModel * _Nullable sModel) {
//        MessageModel *models = [MessageModel initWithSendText:model.sendText isReceive:model.isReceive showMike:model.showMike showText:model.showText isMike:model.isMike showTime:model.showTime sendTime:model.sendTime voicePath:model.voicePath sendFailure:model.sendFailure sendTextTrans:model.sendTextTrans];
        MessageModel *models = [MessageModel mj_objectWithKeyValues:[model mj_JSONObject]];
        switch (sModel.mikeType) {
            case SLMikeInputDelete:
            {
                [weakSelf removeMessage:model];
            }
                break;
            case SLMikeInputLook:
            {
                models.showText = YES;
                [weakSelf updateMessage:models idx:indexPath.row];
            }
                break;
            case SLMikeInputBroadcast:
            {
                if (weakSelf.broadcastBlock) {
                    weakSelf.broadcastBlock(models);
                }
            }
                break;
            case SLMikeInputHidden:
            {
                models.showText = NO;
                [weakSelf updateMessage:models idx:indexPath.row];
            }
                break;
            case SLMikeInputTrans:
            {
                models.showText = YES;
                models.showTrans = !model.showTrans;
                if (!model.sendTextTrans) {
                    //百度智能云
                    [[SLHttpCenter SharedInstance] getWithUrl:@"https://aip.baidubce.com/oauth/2.0/token" parameter:@{@"grant_type":@"client_credentials", @"client_id":@"0VhDeLqdcbLamGENfw4c24aq", @"client_secret": @"92qphFKyc0uXGktqFLtfdmqlojt9MsgG"} success:^(id responseObject) {
                        NSString *accessToken = [responseObject objectForKey:@"access_token"];
                        if (accessToken) {
                            NSString *bdUrl = [NSString stringWithFormat:@"https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1?access_token=%@",accessToken];
                            NSDictionary *dict = @{@"q":model.sendText,
                                                   @"from":@"auto",
                                                   @"to":@"en"};
                            [[SLHttpCenter SharedInstance] postWithUrl:bdUrl parameter:dict success:^(id responseObject) {
                                NSDictionary *reslut = [responseObject objectForKey:@"result"];
                                NSArray *arr = [reslut objectForKey:@"trans_result"];
                                if (arr && arr.count) {
                                    NSDictionary *resultDict = arr.firstObject;
                                    NSString *dst = [resultDict objectForKey:@"dst"];
                                    models.sendTextTrans = dst;
                                    [weakSelf updateMessage:models idx:indexPath.row];
                                }else {
                                    [ZFToast ShowWithMessage:@"翻译失败"];
                                }
                            } failure:^(SPRequestError *error) {
                                [ZFToast ShowWithMessage:@"翻译失败"];
                            }];
                        }else {
                            [ZFToast ShowWithMessage:@"翻译失败"];
                        }
                    } failure:^(SPRequestError *error) {
                        [ZFToast ShowWithMessage:@"翻译失败"];
                    }];
                }else {
                    [weakSelf updateMessage:models idx:indexPath.row];
                }
            }
                break;
            case SLMsgRepeat:
            {
                [weakSelf resultsRequestWith:model];
            }
                break;
            case SLMsgCopy:
            {
                [[UIPasteboard generalPasteboard] setString:model.sendText];
                [ZFToast ShowWithMessage:@"复制成功"];
            }
                break;
            default:
                break;
        }
    };
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    [collectionView.collectionViewLayout invalidateLayout];
    return 1;
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.chatDataManager.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
/**

 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item >= self.chatDataManager.count) {
        return;
    }
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MessageModel *model = [self.chatDataManager objectAtIndex:indexPath.item];
    //实际send宽度16 + 52 + 10  + 16 英文和中文宽度不一致、ios string包含字符串iOS开发已经被淘汰了吧
   //
    CGFloat sw = 124;
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"];
    NSRange range = [model.sendText rangeOfCharacterFromSet:set];
    if (range.location != NSNotFound) {
        sw -= 8;
    }
    if (model.sendFailure) {
        sw += 8;
    }
    NSString *text = model.sendText;
    if (model.showMike && model.showTrans) {
        text = model.sendTextTrans;
    }
    CGSize contentSize = [text boundingRectWithSize:CGSizeMake(kSCREEN_WIDTH - sw, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil].size;
//    NSLog(@"%f---%f",contentSize.width, contentSize.height);
    CGFloat height = 0;
    if (model.showMike) {
        height += 65;
    }
    if (model.showText) {
        height += contentSize.height + (model.showMike ? 25 : 42);
    }
    if (model.showTime) {
        height += 23;
    }
    if (!model.showMike && model.showTrans) {
        CGSize transContentSize = [model.sendTextTrans boundingRectWithSize:CGSizeMake(kSCREEN_WIDTH - 124, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil].size;
        height += transContentSize.height + 40;
    }
    return CGSizeMake(kSCREEN_WIDTH, height);
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    
}

- (UICollectionView *)messageCollectionView
{
    if (!_messageCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 0.0;
        layout.minimumInteritemSpacing = 0.0;
        _messageCollectionView = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:layout];
        _messageCollectionView.delegate = self;
        _messageCollectionView.dataSource = self;
        _messageCollectionView.backgroundColor = [UIColor clearColor];
        _messageCollectionView.showsVerticalScrollIndicator = NO;
        _messageCollectionView.showsHorizontalScrollIndicator = NO;
        _messageCollectionView.bounces = NO;
        [self addSubview:_messageCollectionView];
    }
    return _messageCollectionView;
}

- (NSMutableArray<MessageModel *> *)chatDataManager
{
    if (!_chatDataManager) {
        _chatDataManager = [NSMutableArray array];
    }
    return _chatDataManager;
}

@end
