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

#import "YMChatBottomView.h"
#import "SLMessageViewController.h"

@interface YMChatBottomView()

@property (nonatomic, strong) UIButton *leftBtn;

@property (nonatomic, strong) UIButton *rightSendBtn;

@property (nonatomic, strong) UIView *textBgView;

@end

@implementation YMChatBottomView

static const NSInteger btnTag = 20000;

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

- (void)confignUI
{
    self.backgroundColor = [UIColor colorFormString:@"#F3F4F4"];
    [self.textBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.mas_equalTo(8);
        make.height.mas_greaterThanOrEqualTo(40);
        make.bottom.equalTo(self.mas_bottom).offset(-(8 + kSafeAreaBottom));
    }];
//    self.textBgView.backgroundColor = [UIColor redColor];
    
    [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12 * KScaleW);
        make.width.height.mas_equalTo(32);
//        make.centerY.equalTo(self.textBgView);
        make.bottom.equalTo(self.textBgView.mas_bottom).offset(-4);
    }];
    
    [self.rightSendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.leftBtn);
        make.right.equalTo(self.textBgView.mas_right).offset(-12 * KScaleW);
        make.width.mas_equalTo(50 * KScaleW);
        make.height.mas_equalTo(32);
    }];
    
    [self.imHintLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.equalTo(self.nextGrowingTextView);
        make.height.top.equalTo(self.textBgView);
    }];
    self.imHintLabel.frame = self.nextGrowingTextView.frame;
    
    [self.nextGrowingTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(64);
        make.centerY.equalTo(self.textBgView).priority(500);
        make.top.greaterThanOrEqualTo(self.textBgView).offset(0).priority(1000);
        make.right.equalTo(self.rightSendBtn.mas_left).offset(-8);
    }];
    
    [self btnClick:self.leftBtn];
}

- (void)btnClick:(UIButton *)btn
{
    switch (btn.tag - btnTag) {
        case 1:
            btn.selected = !btn.selected;
            [self endEditing:YES];
            [self changeChatType:btn];
            break;
        case 2:
            if (self.sendTextBlock) {
                self.sendTextBlock(self.nextGrowingTextView.textView.text);
            }
            break;
        case 3:
            if (self.hintBtnBlock) {
                self.hintBtnBlock(@"(|=|)");
            }
            break;
        default:
            break;
    }
}

- (void)changeChatType:(UIButton *)btn
{
    self.rightSendBtn.hidden = !btn.isSelected;
    self.imHintLabel.hidden = btn.isSelected;
    self.nextGrowingTextView.hidden = !btn.isSelected;
    SLMessageViewController *vc = (SLMessageViewController *)self.viewController;
    if (self.nextGrowingTextView.hidden == YES) {
        if ([vc isKindOfClass:SLMessageViewController.class]) {
            CGRect rect = [self convertRect:self.imHintLabel.frame toView:vc.view];
            vc.mikeInputView.oldRect = rect;
            vc.mikeInputView.frame = rect;
            vc.mikeInputView.hidden = NO;
        }
    }else {
        if ([vc isKindOfClass:SLMessageViewController.class]) {
            vc.mikeInputView.hidden = YES;
        }
    }
}

- (UIButton *)leftBtn
{
    if (!_leftBtn) {
        _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_leftBtn setImage:ImageName(@"icon_msg_keyboard") forState:UIControlStateNormal];
        [_leftBtn setImage:ImageName(@"icon_msg_mike") forState:UIControlStateSelected];
        [_leftBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        _leftBtn.tag = btnTag + 1;
        [self.textBgView addSubview:_leftBtn];
    }
    return _leftBtn;
}

- (UIButton *)rightSendBtn
{
    if (!_rightSendBtn) {
        _rightSendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightSendBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_rightSendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _rightSendBtn.backgroundColor = SLColor(@"#1677FF");
        [_rightSendBtn setTitle:@"发送" forState:UIControlStateNormal];
        [_rightSendBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        _rightSendBtn.hidden = YES;
        _rightSendBtn.layer.cornerRadius = 4.0;
        _rightSendBtn.tag = btnTag + 2;
        [self.textBgView addSubview:_rightSendBtn];
    }
    return _rightSendBtn;
}

- (YMNextGrowingTextView *)nextGrowingTextView
{
    if (!_nextGrowingTextView) {
        _nextGrowingTextView = [[YMNextGrowingTextView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH - 160, 40)];
        _nextGrowingTextView.backgroundColor = [UIColor whiteColor];
        _nextGrowingTextView.placeholderAttributedText = [[NSAttributedString alloc] initWithString:@"请输入关键词" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang SC" size: 16], NSForegroundColorAttributeName : SLColor(@"#999999")}];
        _nextGrowingTextView.layer.cornerRadius = 4.0;
        _nextGrowingTextView.maxNumberOfLines = 6;
        _nextGrowingTextView.hidden = YES;
        [self.textBgView addSubview:_nextGrowingTextView];
    }
    return _nextGrowingTextView;
}

- (UIView *)textBgView
{
    if (!_textBgView) {
        _textBgView = [[UIView alloc] initWithFrame:CGRectZero];
        [self addSubview:_textBgView];
    }
    return _textBgView;
}

- (UILabel *)imHintLabel
{
    if (!_imHintLabel) {
        _imHintLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _imHintLabel.text = @"按住 说话";
        _imHintLabel.font = [UIFont fontWithName:@"PingFang SC" size:12];
        _imHintLabel.textColor = [UIColor blackColor];
        _imHintLabel.backgroundColor = [UIColor whiteColor];
        _imHintLabel.textAlignment = NSTextAlignmentCenter;
        _imHintLabel.userInteractionEnabled = YES;
        [self addSubview:_imHintLabel];
    }
    return _imHintLabel;
}

@end
