//
//  UITextView+LengthLimit.m
//  Gengmei
//
//  Created by wangyang on 2017/3/9.
//  Copyright © 2017年 更美互动信息科技有限公司. All rights reserved.
//

#import "UITextView+LengthLimit.h"
#import <objc/runtime.h>


@implementation UITextView (LimitBase)
- (NSInteger)maxTextLength {
    NSNumber *length = objc_getAssociatedObject(self, @selector(maxTextLength));
    return length.integerValue;
}

- (void)setMaxTextLength:(NSInteger)maxTextLength {
    objc_setAssociatedObject(self, @selector(maxTextLength), @(maxTextLength), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSInteger)textLength {
    NSNumber *length = objc_getAssociatedObject(self, @selector(textLength));
    return length.integerValue;
}

- (void)setTextLength:(NSInteger)textLength {
    objc_setAssociatedObject(self, @selector(textLength), @(textLength), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end

@implementation UITextView (SimpleLimit)
- (void)s_textViewDidChange {
    NSString *toBeString = self.text;
    UITextRange *selectedRange = [self markedTextRange];
    UITextPosition *position = [self positionFromPosition:selectedRange.start offset:0];

    NSInteger length = self.textLength;
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position){
        if (toBeString.length > self.maxTextLength){
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:self.maxTextLength];
            if (rangeIndex.length == 1){
                self.text = [toBeString substringToIndex:self.maxTextLength];
            } else{
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, self.maxTextLength)];
                self.text = [toBeString substringWithRange:rangeRange];
            }
            length = self.text.length;
        } else {
            NSString *selectedText = [self textInRange:selectedRange];
            length = self.text.length - selectedText.length;
        }
    }

    if (length > self.maxTextLength) {
        self.textLength = self.maxTextLength;
    } else {
        self.textLength = length;
    }
}
@end

@implementation UITextView (ComplexLimit)
- (BOOL)c_shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (text.length==0) { //Delete any cases
        if(range.length > 1){
            //Delete whole word
        }
        else if(range.length == 1){
            //Delete single letter
        }
        else if(range.length == 0){
            //Tap delete key when textField empty
        }

        return YES;
    }

    UITextRange *selectedRange = [self markedTextRange];
    //获取高亮部分
    UITextPosition *pos = [self positionFromPosition:selectedRange.start offset:0];
    //获取高亮部分内容
    //    NSString * selectedtext = [self textInRange:selectedRange];

    //如果有高亮且当前字数开始位置小于最大限制时允许输入
    if (selectedRange && pos) {
        NSInteger startOffset = [self offsetFromPosition:self.beginningOfDocument toPosition:selectedRange.start];
        NSInteger endOffset = [self offsetFromPosition:self.beginningOfDocument toPosition:selectedRange.end];
        NSRange offsetRange = NSMakeRange(startOffset, endOffset - startOffset);

        if (offsetRange.location < self.maxTextLength) {
            return YES;
        }
        else
        {
            return NO;
        }
    }


    NSString *comcatstr = [self.text stringByReplacingCharactersInRange:range withString:text];

    NSInteger canInputLen = self.maxTextLength - comcatstr.length;

    if (canInputLen >= 0)
    {
        return YES;
    }
    else
    {

        NSInteger len = text.length + canInputLen;
        //防止当text.length + caninputlen < 0时，使得rg.length为一个非法最大正数出错
        NSRange rg = {0,MAX(len,0)};

        if (rg.length > 0)
        {
            NSString *s = @"";
            //判断是否只普通的字符或asc码(对于中文和表情返回NO)
            BOOL asc = [text canBeConvertedToEncoding:NSASCIIStringEncoding];
            if (asc) {
                s = [text substringWithRange:rg];//因为是ascii码直接取就可以了不会错
            }
            else
            {
                __block NSInteger idx = 0;
                __block NSString  *trimString = @"";//截取出的字串
                //使用字符串遍历，这个方法能准确知道每个emoji是占一个unicode还是两个
                [text enumerateSubstringsInRange:NSMakeRange(0, [text length])
                                         options:NSStringEnumerationByComposedCharacterSequences
                                      usingBlock: ^(NSString* substring, NSRange substringRange, NSRange enclosingRange, BOOL* stop) {

                                          NSInteger steplen = substring.length;
                                          if (idx >= rg.length) {
                                              *stop = YES; //取出所需要就break，提高效率
                                              return ;
                                          }

                                          trimString = [trimString stringByAppendingString:substring];

                                          idx = idx + steplen;//这里变化了，使用了字串占的长度来作为步长
                                      }];

                s = trimString;
            }
            //rang是指从当前光标处进行替换处理(注意如果执行此句后面返回的是YES会触发didchange事件)
            [self setText:[self.text stringByReplacingCharactersInRange:range withString:s]];
            //既然是超出部分截取了，那一定是最大限制了。
            self.textLength = self.maxTextLength;
        }
        return NO;
    }
}

- (void)c_textViewDidChange
{
    UITextRange *selectedRange = [self markedTextRange];
    //获取高亮部分
    UITextPosition *pos = [self positionFromPosition:selectedRange.start offset:0];

    //如果在变化中是高亮部分在变，就不要计算字符了
    if (selectedRange && pos) {
        return;
    }

    NSString  *nsTextContent = self.text;
    NSInteger existTextNum = nsTextContent.length;

    if (existTextNum > self.maxTextLength)
    {
        //截取到最大位置的字符(由于超出截部分在should时被处理了所在这里这了提高效率不再判断)
        NSString *s = [nsTextContent substringToIndex:self.maxTextLength];
        
        [self setText:s];
        self.textLength = self.maxTextLength;
    } else {
        self.textLength = existTextNum;
    }
}

@end

