//
//  SLMomentsCommentVc.m
//  SLAiELTS
//
//  Created by Gusont on 2023/4/13.
//

#import "SLMomentsCommentVc.h"
#import "PlaceholderTextView.h"

@interface SLMomentsCommentVc ()
@property (weak, nonatomic) IBOutlet PlaceholderTextView *textView;
@property (weak, nonatomic) IBOutlet UILabel *numbelLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textViewTopConst;

@end

@implementation SLMomentsCommentVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.textViewTopConst.constant = kNavBarHeight;
    self.title = @"评论";
    self.view.backgroundColor = [UIColor whiteColor];
    UIImage *backImage =[UIImage imageNamed:@"icon_nav_back"];
    YMBarButtonItem *backBarButtonItem =[[YMBarButtonItem alloc]initWithImage:backImage style:YMBarButtonItemPlain target:self action:@selector(backBarButtonPressed:)];
    backBarButtonItem.imageInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    backBarButtonItem.title = @"取消";
    self.navigationBar.leftBarButtonItem = backBarButtonItem;
    
    self.textView.placeholder = @"请输入...";
    self.textView.font = [UIFont systemFontOfSize:14];
    self.textView.textColor = SL333Color;
    
    YMBarButtonItem *rightBarItem = [[YMBarButtonItem alloc] initWithTitle:@"发布  " target:self action:@selector(preserveBtnClick)];
    rightBarItem.titleInsets = UIEdgeInsetsMake(0, 0, 0, 20);
    self.navigationBar.rightBarButtonItem = rightBarItem;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSString *toBeString = [textView.text stringByReplacingCharactersInRange:range withString:text];
    if (toBeString.length > 500) {
        return NO;
    }
    self.numbelLabel.text = [NSString stringWithFormat:@"%ld/500",toBeString.length];
    return YES;
}

- (void)preserveBtnClick {
    if (!self.textView.text.length) {
        [ZFToast ShowWithMessage:@"评论不能为空"];
        return;
    }
    if (self.momentsCommentBlock) {
        self.momentsCommentBlock(self.textView.text);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    }
}

@end
