






#import "VisualEjectLabel.h"
#import "DidTodayWax.h"
#import "UIImage+BinImage.h"

@implementation VisualEjectLabel

+ (VisualEjectLabel *)expansionSockLabel {
    return [self expansionSockLabel:YES];
}

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

    NSAttributedString *cathedralOverSocketJustAccording = nil;
    if (isCheckBox) {
        
        NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
        UIImage *templateImage = [[UIImage preserveSexItsMaskEffortName:DidTodayWax.invitePencil.softballLimitedChapterExpectedAuditAdverb] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        attachment.image = templateImage; 
        
        attachment.bounds = CGRectMake(0, -5, 20, 20);
        cathedralOverSocketJustAccording = [NSAttributedString attributedStringWithAttachment:attachment];
    }

    
    NSString *text = DidTodayWax.fixEraSafeTaps.semaphoreNap;
    NSMutableAttributedString *wrongAndOurEnd = [[NSMutableAttributedString alloc] initWithString:text];
    
    
    NSRange frontArbitrary = [text rangeOfString:DidTodayWax.fixEraSafeTaps.funkSexOverEar];
    if (frontArbitrary.location != NSNotFound) {
        [wrongAndOurEnd addAttribute:NSForegroundColorAttributeName value:[DidTodayWax mightLeftColor] range:frontArbitrary];
        [wrongAndOurEnd addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:frontArbitrary];
    }

    
    NSMutableAttributedString *fillVendorNorthExtensionPresence = [[NSMutableAttributedString alloc] init];
    if (cathedralOverSocketJustAccording) {
        [fillVendorNorthExtensionPresence appendAttributedString:cathedralOverSocketJustAccording];
    }
    [fillVendorNorthExtensionPresence appendAttributedString:wrongAndOurEnd];
    
    label.attributedText = fillVendorNorthExtensionPresence;
    
    
    UITapGestureRecognizer *magicSeven = [[UITapGestureRecognizer alloc] initWithTarget:label action:@selector(prepareLogoScalarWordShot:)];
    [label addGestureRecognizer:magicSeven];
    
    return label;
}

- (void)setNapEarlyKinArm:(BOOL)napEarlyKinArm {
    _napEarlyKinArm = !napEarlyKinArm;
    [self secureAxesSignerWriteEnvelopeBinLabel:self];
}

- (void)prepareLogoScalarWordShot:(UITapGestureRecognizer *)magicSeven {
    VisualEjectLabel *label = (VisualEjectLabel *)magicSeven.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 emailAcross = [magicSeven locationInView:label];
    CGRect usedRect = [layoutManager usedRectForTextContainer:textContainer];
    CGPoint flowRespectsPassPartlyGaelic = CGPointMake(
        (label.bounds.size.width - usedRect.size.width) / 2,   
        (label.bounds.size.height - usedRect.size.height) / 2  
    );
    
    
    CGPoint worldBalticJabberSurrogateNineteenFriend = CGPointMake(
        emailAcross.x - flowRespectsPassPartlyGaelic.x,
        emailAcross.y - flowRespectsPassPartlyGaelic.y
    );
    
    
    __block BOOL bagBitTeacher = 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 usesTenPub;
            [layoutManager glyphRangeForCharacterRange:range actualCharacterRange:&usesTenPub];
            
            
            CGRect milesRect = [layoutManager boundingRectForGlyphRange:usesTenPub
                                                      inTextContainer:textContainer];
            
            
            CGRect decodingMaxRect = CGRectOffset(milesRect, flowRespectsPassPartlyGaelic.x, flowRespectsPassPartlyGaelic.y);
            
            
            if (CGRectContainsPoint(decodingMaxRect, emailAcross)) {
                bagBitTeacher = YES;
                *stop = YES;
            }
        }
    }];
    
    if (bagBitTeacher) {
        
        
        [self secureAxesSignerWriteEnvelopeBinLabel:label];
        return;
    }
    
    
    NSUInteger characterIndex = [layoutManager characterIndexForPoint:worldBalticJabberSurrogateNineteenFriend
                                                    inTextContainer:textContainer
                           fractionOfDistanceBetweenInsertionPoints:NULL];
    
    NSString *nineText = label.attributedText.string;
    NSRange frontArbitrary = [nineText rangeOfString:DidTodayWax.fixEraSafeTaps.funkSexOverEar];
    
    if (characterIndex != NSNotFound && NSLocationInRange(characterIndex, frontArbitrary)) {
        
        
        if (self.bitsExtendingCenterAfterOriginArts) {
            self.bitsExtendingCenterAfterOriginArts();
        }
    }
}


- (void)secureAxesSignerWriteEnvelopeBinLabel:(VisualEjectLabel *)label {
    NSMutableAttributedString *attributedText = [label.attributedText mutableCopy];
    __block BOOL barClamped = 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 extentNow = !_napEarlyKinArm;
        
        
        NSTextAttachment *exclusionNode = [[NSTextAttachment alloc] init];
        
        
        UIColor *yetPutColor = extentNow ? [DidTodayWax mightLeftColor]: UIColor.lightGrayColor;
        UIImage *pressHitImage = [UIImage preserveSexItsMaskEffortName:extentNow ? DidTodayWax.invitePencil.winCallbacksDeletionAnchorTeacherSlide :DidTodayWax.invitePencil.softballLimitedChapterExpectedAuditAdverb];
        
        
        exclusionNode.image = [[pressHitImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
                                imageWithTintColor:yetPutColor];
        exclusionNode.bounds = oldAttachment.bounds;
        
        
        [attributedText removeAttribute:NSAttachmentAttributeName range:range];
        [attributedText addAttribute:NSAttachmentAttributeName value:exclusionNode range:range];
        
        _napEarlyKinArm = extentNow;
        barClamped = YES;
        *stop = YES;
    }];
    
    if (barClamped) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIView transitionWithView:label
                              duration:0.3
                               options:UIViewAnimationOptionTransitionCrossDissolve
                            animations:^{
                                label.attributedText = attributedText;
                            } completion:nil];
            [label setNeedsDisplay];
        });
    }
}

@end
