//
//  RichFileCell.m
//  smartRhino
//
//  Created by niuzhen on 2020/1/13.
//  Copyright © 2020 tederen. All rights reserved.
//

#import "RichFileCell.h"
#import "UtilMacro.h"
#import "MMRichImageModel.h"
#import "MMRichTextConfig.h"
#import "MMTextView.h"
#import "RichModel.h"

@interface RichFileCell () <MMTextViewDelegate, UITextViewDelegate>
@property (nonatomic, strong) MMTextView* textView;
@property (nonatomic, strong) TDButton *reloadButton;
@property (nonatomic, strong) UIView* fileView;
@property (nonatomic, strong) RichModel* fileModel;
@property (nonatomic, strong) UIImageView * iconV;
@property (nonatomic, strong) UILabel* nameL;
@property (nonatomic, strong) UILabel* subTitleL;

@end
@implementation RichFileCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    [self addSubview:self.textView];
    [self addSubview:self.fileView];
    [self addSubview:self.reloadButton];
    [self.fileView addSubview:self.iconV];
    [self.fileView addSubview:self.nameL];
    [self.fileView addSubview:self.subTitleL];

    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self);
        make.bottom.equalTo(self).priority(900);
    }];

    [self.fileView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(UIEdgeInsetsMake(24, 24, 24, 24));
    }];
    [self.iconV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_offset(@12);
        make.size.mas_offset(CGSizeMake(50, 50));
    }];
    [self.nameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconV.mas_right).offset(10);
        make.top.mas_offset(20);
    }];
    [self.subTitleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconV.mas_right).offset(10);
        make.bottom.mas_offset(-20);
    }];
    [self.reloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.fileView.mas_top);
        make.right.mas_equalTo(self.fileView.mas_right);
    }];
    [self.reloadButton setCurrentButtonHotSize:CGSizeZero];
}

- (void)updateWithData:(id)data {
    if([data isKindOfClass:[RichModel class]]){
        RichModel* model = (RichModel*)data;
        _fileModel = model;
        _iconV.image = IMG(@"站内信图");
        _nameL.text = model.Title;
        _subTitleL.attributedText = [ZYCTool checkOfString:[NSString stringWithFormat:@"来自-%@的收藏",model.Author] KeyString:model.Author];
        // 设置旧的数据delegate为nil
        NSAttributedString* imgAttrStr = [_fileModel attrStringWithContainer];
        
        self.textView.attributedText = imgAttrStr;
        // 重新设置TextView的约束
        [self.textView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self);
            make.bottom.equalTo(self).priority(900);
            make.height.mas_offset(@128);
        }];
        [self.fileView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(24, 24, 24, 24));
        }];
    }
}

- (void)mm_beginEditing {
    BOOL result = [self.textView becomeFirstResponder];
    // NSLog(@"result = %d", result);
}

- (void)mm_endEditing {
    BOOL result = [self.textView resignFirstResponder];
    // NSLog(@"result = %d", result);
}

- (void)getPreFlag:(BOOL*)isPre postFlag:(BOOL*)isPost richFlag:(BOOL*)isRich model:(RichModel *)model;
 {
    NSRange selRange = self.textView.selectedRange;
     if (isRich) {
         if (model && ![model isKindOfClass:[NSNull class]]) {
             *isRich = YES;
         }else{
             *isRich = NO;
         }
     }
    // 设置标记值
    if (isPre) {
        if (selRange.location == 0) {
            *isPre = YES;
        } else {
            *isPre = NO;
        }
    }
    
    if (isPost) {
        if (selRange.location+selRange.length == _textView.text.length) {
            *isPost = YES;
        } else {
            *isPost = NO;
        }
    }
}


#pragma mark - ......::::::: lazy load :::::::......

- (MMTextView *)textView {
    if (!_textView) {
        _textView = [MMTextView new];
        _textView.font = MMEditConfig.defaultEditContentFont;
        _textView.textContainerInset = UIEdgeInsetsMake(MMEditConfig.editAreaTopPadding, MMEditConfig.editAreaLeftPadding, MMEditConfig.editAreaBottomPadding, MMEditConfig.editAreaRightPadding);
        _textView.scrollEnabled = NO;
        _textView.allowsEditingTextAttributes = YES;
        _textView.delegate = self;
        _textView.mm_delegate = self;
    }
    return _textView;
}

- (UIView *)fileView
{
    if (!_fileView) {
        _fileView = [UIView new];
        _fileView.backgroundColor = UIColorHex(#F4F5F6);
    }
    return _fileView;
}
- (UIImageView *)iconV
{
    if (!_iconV) {
        _iconV = [UIImageView new];
        _iconV.layer.cornerRadius = 4.f;
        _iconV.layer.masksToBounds = YES;
    }
    return _iconV;
}
- (UILabel *)nameL
{
    if (!_nameL) {
        _nameL = [[UILabel alloc] init];
        _nameL.textColor = UIColorHex(#0A0A0A);
        _nameL.font = [UIFont systemFontOfSize:16];
    }
    return _nameL;
}
- (UILabel *)subTitleL
{
    if (!_subTitleL) {
        _subTitleL =  [[UILabel alloc] init];
        _subTitleL.textColor = UIColorHex(#999999);
        _subTitleL.font = [UIFont systemFontOfSize:11];
    }
    return _subTitleL;
}

- (TDButton *)reloadButton {
    if (!_reloadButton) {
        _reloadButton = [[TDButton alloc] init];
        [_reloadButton setImage:[UIImage imageNamed:@"频道关闭"] forState:UIControlStateNormal];
        [_reloadButton addTarget:self action:@selector(onReloadBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _reloadButton;
}

- (void)onReloadBtnClick:(UIButton*)sender {
    if ([self.delegate respondsToSelector:@selector(mm_reloadItemAtIndexPath:)]) {
        [self.delegate mm_reloadItemAtIndexPath:[self curIndexPath]];
    }
}


#pragma mark - ......::::::: UITextViewDelegate :::::::......

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    // 处理换行
    if ([text isEqualToString:@"\n"] || text.length > 0) {
        if (range.location == 0 && range.length == 0) {
            // 在前面添加换行
            if ([self.delegate respondsToSelector:@selector(mm_preInsertTextLineAtIndexPath:textContent:)]) {
                [self.delegate mm_preInsertTextLineAtIndexPath:[self curIndexPath]textContent:text];
            }
        } else if (range.location == 1 && range.length == 0) {
            // 在后面添加换行
            if ([self.delegate respondsToSelector:@selector(mm_postInsertTextLineAtIndexPath:textContent:)]) {
                [self.delegate mm_postInsertTextLineAtIndexPath:[self curIndexPath] textContent:text];
            }
        } else if (range.location == 0 && range.length == 2) {
            // 选中和换行
        }
    }
    
    if (![text isEqualToString:@"\n"] && text.length > 0) {
        if (range.location == 1 && range.length == 0) {
            // 在后面添加换行
            if ([self.delegate respondsToSelector:@selector(mm_postInsertTextLineAtIndexPath:textContent:)]) {
                [self.delegate mm_postInsertTextLineAtIndexPath:[self curIndexPath] textContent:text];
            }
        }
    }
    
    // 处理删除
    if ([text isEqualToString:@""]) {
        NSRange selRange = textView.selectedRange;
        if (selRange.location == 0 && selRange.length == 0) {
            // 处理删除
            if ([self.delegate respondsToSelector:@selector(mm_preDeleteItemAtIndexPath:)]) {
                [self.delegate mm_preDeleteItemAtIndexPath:[self curIndexPath]];
            }
        } else if (selRange.location == 1 && selRange.length == 0) {
            // 处理删除
            if ([self.delegate respondsToSelector:@selector(mm_PostDeleteItemAtIndexPath:)]) {
                [self.delegate mm_PostDeleteItemAtIndexPath:[self curIndexPath]];
            }
        } else if (selRange.location == 0 && selRange.length == 2) {
            // 处理删除
            if ([self.delegate respondsToSelector:@selector(mm_preDeleteItemAtIndexPath:)]) {
                [self.delegate mm_preDeleteItemAtIndexPath:[self curIndexPath]];
            }
        }
    }
    return NO;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    // textView.inputAccessoryView = [self.delegate mm_inputAccessoryView];
    if ([self.delegate respondsToSelector:@selector(mm_shouldShowAccessoryView:)]) {
        [self.delegate mm_shouldShowAccessoryView:YES];
    }
    if ([self.delegate respondsToSelector:@selector(mm_updateActiveIndexPath:)]) {
        [self.delegate mm_updateActiveIndexPath:[self curIndexPath]];
    }
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
    textView.inputAccessoryView = nil;
    return YES;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end