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

#import "YMChatServiceView.h"
#import "YMIMHintView.h"
#import "MessageModel.h"
#import "SLMikeInputView.h"

@interface YMChatServiceView()<Delegates>

@property (nonatomic, strong) NSMutableString *recResultString;

@end

@implementation YMChatServiceView

- (void)dealloc
{

}

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

- (void)confignUI
{
    CGFloat bottomHeight = 56 + kSafeAreaBottom;
    self.messageView.messageCollectionView.contentInset = UIEdgeInsetsMake(0, 0, 32, 0);
    
    [self.messageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.bottom.equalTo(self.chatBottomView.mas_top).offset(0);
    }];
    
    [self.chatBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.height.mas_equalTo( bottomHeight);
        make.bottom.equalTo(self.mas_bottom).offset(-0);
    }];

    WS(weakSelf);
    self.chatBottomView.sendTextBlock = ^(NSString * _Nonnull text) {
        if (text.length) {
            MessageModel *model = [MessageModel initWithSendText:text isReceive:NO showMike:NO showText:YES];
            model.toId = weakSelf.friendModel.userId;
            [weakSelf.messageView appendMessage:model];
            weakSelf.chatBottomView.nextGrowingTextView.textView.text = nil;
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf.chatBottomView endEditing:YES];
            });
        }
    };
    
    self.chatBottomView.nextGrowingTextView.delegates = self;

}

- (void)onVolumeChangedEx:(int)volume
{
    if (0 == volume) {
//        self.chatBottomView.imBtnAnimateBg.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0];
    }else {
        CGFloat f = (volume + (arc4random() % 5)) / 100.0;
         f = f * 0.5;
        CGFloat s = f + 0.1;
//        self.chatBottomView.imBtnAnimateBg.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:MAX((0.6 - s) * 0.7, 0.2)];
//        self.chatBottomView.imBtnAnimateBg.transform = CGAffineTransformMakeScale(1 + s, 1 + s);
    }
}

#pragma mark - changeHeight
- (void)willChangeHeight:(CGFloat)height
{
    [self bottomChangeHeight:height];
}

- (void)didChangeHeight:(CGFloat)height
{
    [self bottomChangeHeight:height];
}

- (void)bottomChangeHeight:(CGFloat)height
{
    
    [self.chatBottomView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(kSafeAreaBottom + MAX(height + 16, 56));
    }];
    [self.chatBottomView setNeedsDisplay];
    [self.messageView scrollToBottom:NO];
}

- (YMChatBottomView *)chatBottomView
{
    if (!_chatBottomView) {
        _chatBottomView = [[YMChatBottomView alloc] initWithFrame:CGRectZero];
        [self addSubview:_chatBottomView];
    }
    return _chatBottomView;
}

- (YMIMMessageCollectionView *)messageView
{
    if (!_messageView) {
        _messageView = [[YMIMMessageCollectionView alloc] initWithFrame:CGRectZero];
        [self addSubview:_messageView];
    }
    return _messageView;
}

@end
