//
//  TextUtils.m
//  AntsContract
//
//  Created by Ray on 12/20/16.
//  Copyright © 2016 United Software Applications, Inc. All rights reserved.
//

#import "TextUtils.h"

@implementation TextUtils


+ (long)countOccurencesOfString:(NSString*)string find:(NSString*)searchString {
    long strCount = string.length - [[string stringByReplacingOccurrencesOfString:searchString withString:@""] length];
    return strCount / [searchString length];
}

+ (NSString *)formatNumber:(NSString *)mobileNumber
{
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
    
    NSLog(@"%@", mobileNumber);
    
    int length = (int)[mobileNumber length];
    if(length > 10)
    {
        mobileNumber = [mobileNumber substringFromIndex: length-10];
        NSLog(@"%@", mobileNumber);
        
    }
    
    return mobileNumber;
}

+ (int)getLength:(NSString *)mobileNumber
{
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
    
    int length = (int)[mobileNumber length];
    
    return length;
}

+(NSString*) encodePhoneNumber:(NSString*) phone
{
    
    
    
    
    
    NSLocale* locale = [NSLocale currentLocale];
    if( [[locale localeIdentifier] compare:@"en_US"]!=NSOrderedSame )
        return phone;
    
    if( [phone length]==0 )
        return phone;
    

    if( [phone rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet] ].location==NSNotFound )
    {
        const char* string = [phone UTF8String];
        int length = [phone lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
        if( [[NSString stringWithCString:string length:1] intValue]==1 )
        {
            if( length > 11 )
                return phone;
            NSMutableData* array = [[NSMutableData alloc] init] ;
            // 1 (234) 567-8901
            for( int i=0; i < length; i++ )
            {
                if( i==1 )
                {
                    [array appendBytes:" (" length:2];
                }
                if( i == 4 )
                {
                    [array appendBytes:") " length:2];
                }
                if( i==7 )
                {
                    [array appendBytes:" " length:1];
                }
                [array appendBytes:string++ length:1];
            }
            return [NSString stringWithUTF8String:(const char*)[array bytes]];
        }
        else
        {
            // (012) 345-6789
            if( length > 10 )
                return phone;
            NSMutableData* array = [[NSMutableData alloc] init] ;
            int i=0;
            if( length <=7 )
            {
                for( i=0; i < length; i++ )
                {
                    if( i==3 )
                    {
                        [array appendBytes:"-" length:1];
                    }
                    [array appendBytes:string++ length:1];
                }
            }
            else
            {
                for( i=0; i < length; i++ )
                {
                    if( i==0 )
                        [array appendBytes:"(" length:1];
                    if( i==3 )
                        [array appendBytes:") " length:2];
                    if( i==6 )
                        [array appendBytes:"-" length:1];
                    [array appendBytes:string++ length:1];
                }
            }
            [array appendBytes:"\0" length:1];
            return [NSString stringWithUTF8String:(const char*)[array bytes]];
        }
    }
    return phone;
}
+(NSString*) RegularExpReplace:(NSString*) content from:(NSString*) pattern to:(NSString*) to_pattern
{
    if(content==nil)
        return nil;
    
    
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:nil error:nil];
    NSString* sss = [regex stringByReplacingMatchesInString:content options:0 range:NSMakeRange(0, content.length) withTemplate:to_pattern];
    
    return sss;
}
+(NSArray*) expression_varable:(NSString*)content regex:(NSString*) pattern
{
    
    if(content==nil)
        return nil;
    
    
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:nil error:nil];
    
    NSArray *matches = [regex matchesInString:content options:nil range:NSMakeRange(0, content.length)];
    
    if (matches) {
        for (NSTextCheckingResult *match in matches) {
            for (int i = 0; i < match.numberOfRanges; ++i) {
                DebugLog(@"-> %@", [content substringWithRange:[match rangeAtIndex:i]]);
            }
        }
    }
    return matches;
}
+(NSTextCheckingResult*) expression_findfistMatch:(NSString*)content regex:(NSString*) pattern
{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:nil error:nil];
    
    NSTextCheckingResult *match = [regex firstMatchInString:content options:nil range:NSMakeRange(0, content.length)];
    return match;
    //    if (matches) {
    //        for (NSTextCheckingResult *match in matches) {
    //            for (int i = 0; i < match.numberOfRanges; ++i) {
    //                DebugLog(@"-> %@", [content substringWithRange:[match rangeAtIndex:i]]);
    //            }
    //        }
    //    }
    //    return matches;
}
+ (CGRect)rectVAlign:(CGRect )parent rect:(CGRect)rect vAlign:(NSString*)vAlign
{
    //    double cx=parent.origin.x+parent.size.width/2;
    //    double cy=parent.origin.y+parent.size.height/2;
    CGPoint centerpoint= CGPointMake(parent.origin.x+parent.size.width/2,parent.origin.y+parent.size.height/2);
    
    if([vAlign.lowercaseString isEqualToString:@"middle"])
    {
        rect=CGRectMake(rect.origin.x, centerpoint.y-rect.size.height/2, rect.size.width, rect.size.height);
    }
    
    return rect;
    
}
@end
