






#import "BaseFlippedLabel.h"
#import "PinMovement.h"
#import "UIImage+WhoImage.h"

@implementation BaseFlippedLabel

+ (BaseFlippedLabel *)upperKeyExistLabel {
    return [self upperKeyExistLabel:YES];
}

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

    NSAttributedString *sinBodyAdditionFootballUse = nil;
    if (isCheckBox) {
        
        NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
        UIImage *templateImage = [[UIImage characterMasterArmpitMinGatheringName:PinMovement.cupFormatSun.wateryButterflyFarPolicyFixtureDeveloper] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        attachment.image = templateImage; 
        
        attachment.bounds = CGRectMake(0, -5, 20, 20);
        sinBodyAdditionFootballUse = [NSAttributedString attributedStringWithAttachment:attachment];
    }

    
    NSString *text = PinMovement.otherPhasePlug.partlyDanish;
    NSMutableAttributedString *notifyEasyMood = [[NSMutableAttributedString alloc] initWithString:text];
    
    
    NSRange growSoundMight = [text rangeOfString:PinMovement.otherPhasePlug.armVeryIcyNext];
    if (growSoundMight.location != NSNotFound) {
        [notifyEasyMood addAttribute:NSForegroundColorAttributeName value:[PinMovement unorderedColor] range:growSoundMight];
        [notifyEasyMood addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:growSoundMight];
    }

    
    NSMutableAttributedString *laotianUnwindingQuitBlockFont = [[NSMutableAttributedString alloc] init];
    if (sinBodyAdditionFootballUse) {
        [laotianUnwindingQuitBlockFont appendAttributedString:sinBodyAdditionFootballUse];
    }
    [laotianUnwindingQuitBlockFont appendAttributedString:notifyEasyMood];
    
    label.attributedText = laotianUnwindingQuitBlockFont;
    
    
    UITapGestureRecognizer *silenceMin = [[UITapGestureRecognizer alloc] initWithTarget:label action:@selector(hoursStormDigestMetadataOur:)];
    [label addGestureRecognizer:silenceMin];
    
    return label;
}

- (void)setPasteImportant:(BOOL)pasteImportant {
    _pasteImportant = !pasteImportant;
    [self centerSpanishMarginPutHeavyRemoteLabel:self];
}

- (void)hoursStormDigestMetadataOur:(UITapGestureRecognizer *)silenceMin {
    BaseFlippedLabel *label = (BaseFlippedLabel *)silenceMin.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 phaseMinFix = [silenceMin locationInView:label];
    CGRect usedRect = [layoutManager usedRectForTextContainer:textContainer];
    CGPoint failKeySayNumeratorRearrange = CGPointMake(
        (label.bounds.size.width - usedRect.size.width) / 2,   
        (label.bounds.size.height - usedRect.size.height) / 2  
    );
    
    
    CGPoint goalDarkIndexBlockImproperEra = CGPointMake(
        phaseMinFix.x - failKeySayNumeratorRearrange.x,
        phaseMinFix.y - failKeySayNumeratorRearrange.y
    );
    
    
    __block BOOL pagerGreatOne = 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 eraSlabWho;
            [layoutManager glyphRangeForCharacterRange:range actualCharacterRange:&eraSlabWho];
            
            
            CGRect greekRect = [layoutManager boundingRectForGlyphRange:eraSlabWho
                                                      inTextContainer:textContainer];
            
            
            CGRect adaptiveLawRect = CGRectOffset(greekRect, failKeySayNumeratorRearrange.x, failKeySayNumeratorRearrange.y);
            
            
            if (CGRectContainsPoint(adaptiveLawRect, phaseMinFix)) {
                pagerGreatOne = YES;
                *stop = YES;
            }
        }
    }];
    
    if (pagerGreatOne) {
        
        
        [self centerSpanishMarginPutHeavyRemoteLabel:label];
        return;
    }
    
    
    NSUInteger characterIndex = [layoutManager characterIndexForPoint:goalDarkIndexBlockImproperEra
                                                    inTextContainer:textContainer
                           fractionOfDistanceBetweenInsertionPoints:NULL];
    
    NSString *caseText = label.attributedText.string;
    NSRange growSoundMight = [caseText rangeOfString:PinMovement.otherPhasePlug.armVeryIcyNext];
    
    if (characterIndex != NSNotFound && NSLocationInRange(characterIndex, growSoundMight)) {
        
        
        if (self.encryptBrownArtsTatarInsertionBalance) {
            self.encryptBrownArtsTatarInsertionBalance();
        }
    }
}


- (void)centerSpanishMarginPutHeavyRemoteLabel:(BaseFlippedLabel *)label {
    NSMutableAttributedString *attributedText = [label.attributedText mutableCopy];
    __block BOOL cellOurSex = 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 offActual = !_pasteImportant;
        
        
        NSTextAttachment *sentAllSheFit = [[NSTextAttachment alloc] init];
        
        
        UIColor *waxTwoColor = offActual ? [PinMovement unorderedColor]: UIColor.lightGrayColor;
        UIImage *daySpeedImage = [UIImage characterMasterArmpitMinGatheringName:offActual ? PinMovement.cupFormatSun.naturalSubscribeBackSubjectHandTheme :PinMovement.cupFormatSun.wateryButterflyFarPolicyFixtureDeveloper];
        
        
        sentAllSheFit.image = [[daySpeedImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
                                imageWithTintColor:waxTwoColor];
        sentAllSheFit.bounds = oldAttachment.bounds;
        
        
        [attributedText removeAttribute:NSAttachmentAttributeName range:range];
        [attributedText addAttribute:NSAttachmentAttributeName value:sentAllSheFit range:range];
        
        _pasteImportant = offActual;
        cellOurSex = YES;
        *stop = YES;
    }];
    
    if (cellOurSex) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIView transitionWithView:label
                              duration:0.3
                               options:UIViewAnimationOptionTransitionCrossDissolve
                            animations:^{
                                label.attributedText = attributedText;
                            } completion:nil];
            [label setNeedsDisplay];
        });
    }
}

@end
