//
//  ERTCVideoChatMsgEditView.m
//  ERTCDemo
//
//  Created by wangjinlong23 on 2022/5/24.
//

#import "ERTCVideoChatMsgEditView.h"

@interface ERTCVideoChatMsgEditView ()<UITextViewDelegate>

@property (nonatomic, strong) UILabel *sendTipLabel;
@property (nonatomic, strong) UITextView *textView;
@end

@implementation ERTCVideoChatMsgEditView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1/1.0];
        [self addSubview:self.textView];
        
        UIView *topView = [UIView new];
        [self addSubview:topView];
        
        [topView addSubview:self.sendTipLabel];
        
        [self.textView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(12);
            make.right.equalTo(self).with.offset(-12);
            make.bottom.equalTo(self).with.offset(-8);
            make.height.ysmas_equalTo(32);
        }];
        
        [topView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.left.right.top.equalTo(self);
            make.bottom.equalTo(self.textView.ysmas_top);
        }];
        
        [self.sendTipLabel ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.left.equalTo(topView).with.offset(12);
            make.centerY.equalTo(topView);
        }];
    }
    
    return self;
}

#pragma mark -键盘弹出添加监听事件
-(void)beginEdit {

    // 键盘出现的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
    // 键盘消失的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHiden:) name:UIKeyboardWillHideNotification object:nil];
    
    [self.textView becomeFirstResponder];
}

-(void)endEdit {
    [self.textView resignFirstResponder];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 键盘监听方法
- (void)keyboardWasShown:(NSNotification *)notification {
    if ([self.delegate respondsToSelector:@selector(ERTCVideoChatMsgEditViewProtocol_keyboardWasShown:)]) {
        [self.delegate ERTCVideoChatMsgEditViewProtocol_keyboardWasShown:notification];
    }
}

- (void)keyboardWillBeHiden:(NSNotification *)notification {
    if ([self.delegate respondsToSelector:@selector(ERTCVideoChatMsgEditViewProtocol_keyboardWillBeHiden:)]) {
        [self.delegate ERTCVideoChatMsgEditViewProtocol_keyboardWillBeHiden:notification];
    }
}

#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"])
    { //判断输入的字是否是回车，即按下return
        if ([self.delegate respondsToSelector:@selector(ERTCVideoChatMsgEditViewProtocol_keyBoardSendBtnClick:)]) {
            [self.delegate ERTCVideoChatMsgEditViewProtocol_keyBoardSendBtnClick:textView.text];
            textView.text = @"";
        }
        
        return NO;
    }

    return YES;
}


#pragma mark - UI Getter Method
-(UITextView *)textView {
    if (!_textView) {
        _textView = [[UITextView alloc] init];
        _textView.font = [UIFont systemFontOfSize:13];
        _textView.keyboardType = UIKeyboardTypeDefault;
        _textView.returnKeyType = UIReturnKeySend;
        _textView.backgroundColor = [UIColor colorWithRed:31/255.0 green:31/255.0 blue:34/255.0 alpha:1/1.0];
        _textView.delegate = self;
        _textView.textColor = [UIColor whiteColor];
    }
    
    return _textView;
}

-(UILabel *)sendTipLabel {
    if (!_sendTipLabel) {
        _sendTipLabel = [[UILabel alloc] init];
        _sendTipLabel.font = [UIFont systemFontOfSize:13];
        _sendTipLabel.textColor = [UIColor whiteColor];
        _sendTipLabel.text = @"发送至所有人";
    }
    
    return _sendTipLabel;
}


@end
