






#import "TabCarThemeLabel.h"
#import "DueIcyFetch.h"
#import "UIImage+IllImage.h"

@implementation TabCarThemeLabel

+ (TabCarThemeLabel *)forkFloatAwayLabel {
    return [self forkFloatAwayLabel:YES];
}

+ (TabCarThemeLabel *)forkFloatAwayLabel:(BOOL)isCheckBox {
    
    TabCarThemeLabel *label = [[TabCarThemeLabel alloc] init];
    label.numberOfLines = 0;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont systemFontOfSize:12];
    label.userInteractionEnabled = YES; 

    NSAttributedString *likePullFeedIconConsumes = nil;
    if (isCheckBox) {
        
        NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
        UIImage *templateImage = [[UIImage consoleMinKilowattsBaselinesEnablingName:DueIcyFetch.itsPosterIcy.startFaxUnderBirthdayInverseIndicated] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        attachment.image = templateImage; 
        
        attachment.bounds = CGRectMake(0, -5, 20, 20);
        likePullFeedIconConsumes = [NSAttributedString attributedStringWithAttachment:attachment];
    }

    
    NSString *text = DueIcyFetch.catBaseSinFill.capsExecutor;
    NSMutableAttributedString *eyeSameEggArts = [[NSMutableAttributedString alloc] initWithString:text];
    
    
    NSRange sheetCertTamil = [text rangeOfString:DueIcyFetch.catBaseSinFill.filtersInstant];
    if (sheetCertTamil.location != NSNotFound) {
        [eyeSameEggArts addAttribute:NSForegroundColorAttributeName value:[DueIcyFetch outPostalColor] range:sheetCertTamil];
        [eyeSameEggArts addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:sheetCertTamil];
    }

    
    NSMutableAttributedString *argumentVitalOwnSexLow = [[NSMutableAttributedString alloc] init];
    if (likePullFeedIconConsumes) {
        [argumentVitalOwnSexLow appendAttributedString:likePullFeedIconConsumes];
    }
    [argumentVitalOwnSexLow appendAttributedString:eyeSameEggArts];
    
    label.attributedText = argumentVitalOwnSexLow;
    
    
    UITapGestureRecognizer *tryTipDeep = [[UITapGestureRecognizer alloc] initWithTarget:label action:@selector(manyBedTwoConjugateSensor:)];
    [label addGestureRecognizer:tryTipDeep];
    
    return label;
}

- (void)setIndexesRefined:(BOOL)indexesRefined {
    _indexesRefined = !indexesRefined;
    [self treeShowingQuotationChangeIntersectCivilLabel:self];
}

- (void)manyBedTwoConjugateSensor:(UITapGestureRecognizer *)tryTipDeep {
    TabCarThemeLabel *label = (TabCarThemeLabel *)tryTipDeep.view;
    if (!label.attributedText) return;
    
    
    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:label.attributedText];
    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:label.bounds.size];
    
    textContainer.lineFragmentPadding = 0;
    textContainer.maximumNumberOfLines = label.numberOfLines;
    textContainer.lineBreakMode = label.lineBreakMode;
    
    [textStorage addLayoutManager:layoutManager];
    [layoutManager addTextContainer:textContainer];
    
    
    [layoutManager ensureLayoutForTextContainer:textContainer];
    
    
    CGPoint subPoolHost = [tryTipDeep locationInView:label];
    CGRect usedRect = [layoutManager usedRectForTextContainer:textContainer];
    CGPoint homeLettersGregorianReceivingUnplugged = CGPointMake(
        (label.bounds.size.width - usedRect.size.width) / 2,   
        (label.bounds.size.height - usedRect.size.height) / 2  
    );
    
    
    CGPoint labelSamplingModelRecycleNibblesToo = CGPointMake(
        subPoolHost.x - homeLettersGregorianReceivingUnplugged.x,
        subPoolHost.y - homeLettersGregorianReceivingUnplugged.y
    );
    
    
    __block BOOL insertingWord = NO;
    [label.attributedText enumerateAttribute:NSAttachmentAttributeName
                                    inRange:NSMakeRange(0, label.attributedText.length)
                                    options:0
                                 usingBlock:^(id value, NSRange range, BOOL *stop) {
        if ([value isKindOfClass:[NSTextAttachment class]]) {
            
            NSRange menMostDid;
            [layoutManager glyphRangeForCharacterRange:range actualCharacterRange:&menMostDid];
            
            
            CGRect priceRect = [layoutManager boundingRectForGlyphRange:menMostDid
                                                      inTextContainer:textContainer];
            
            
            CGRect declineFileRect = CGRectOffset(priceRect, homeLettersGregorianReceivingUnplugged.x, homeLettersGregorianReceivingUnplugged.y);
            
            
            if (CGRectContainsPoint(declineFileRect, subPoolHost)) {
                insertingWord = YES;
                *stop = YES;
            }
        }
    }];
    
    if (insertingWord) {
        
        
        [self treeShowingQuotationChangeIntersectCivilLabel:label];
        return;
    }
    
    
    NSUInteger characterIndex = [layoutManager characterIndexForPoint:labelSamplingModelRecycleNibblesToo
                                                    inTextContainer:textContainer
                           fractionOfDistanceBetweenInsertionPoints:NULL];
    
    NSString *zoomText = label.attributedText.string;
    NSRange sheetCertTamil = [zoomText rangeOfString:DueIcyFetch.catBaseSinFill.filtersInstant];
    
    if (characterIndex != NSNotFound && NSLocationInRange(characterIndex, sheetCertTamil)) {
        
        
        if (self.noticeBitsCalculateStaleClusterRear) {
            self.noticeBitsCalculateStaleClusterRear();
        }
    }
}


- (void)treeShowingQuotationChangeIntersectCivilLabel:(TabCarThemeLabel *)label {
    NSMutableAttributedString *attributedText = [label.attributedText mutableCopy];
    __block BOOL literalJob = NO;
    
    [attributedText enumerateAttribute:NSAttachmentAttributeName
                             inRange:NSMakeRange(0, attributedText.length)
                             options:0
                          usingBlock:^(NSTextAttachment *oldAttachment, NSRange range, BOOL *stop) {
        if (![oldAttachment isKindOfClass:[NSTextAttachment class]]) return;
        
        
        BOOL countCity = !_indexesRefined;
        
        
        NSTextAttachment *outTightClaim = [[NSTextAttachment alloc] init];
        
        
        UIColor *hourlyColor = countCity ? [DueIcyFetch outPostalColor]: UIColor.lightGrayColor;
        UIImage *sentenceImage = [UIImage consoleMinKilowattsBaselinesEnablingName:countCity ? DueIcyFetch.itsPosterIcy.justifiedContainerExecuteExclusiveSeeCelsius :DueIcyFetch.itsPosterIcy.startFaxUnderBirthdayInverseIndicated];
        
        
        outTightClaim.image = [[sentenceImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
                                imageWithTintColor:hourlyColor];
        outTightClaim.bounds = oldAttachment.bounds;
        
        
        [attributedText removeAttribute:NSAttachmentAttributeName range:range];
        [attributedText addAttribute:NSAttachmentAttributeName value:outTightClaim range:range];
        
        _indexesRefined = countCity;
        literalJob = YES;
        *stop = YES;
    }];
    
    if (literalJob) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIView transitionWithView:label
                              duration:0.3
                               options:UIViewAnimationOptionTransitionCrossDissolve
                            animations:^{
                                label.attributedText = attributedText;
                            } completion:nil];
            [label setNeedsDisplay];
        });
    }
}

@end
