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

#import "ImageUtils.h"
#import "AppDelegate.h"

@implementation ImageUtils
+ (CGRect)scaleToSize:(CGRect )from to:(CGSize)to
{
    if(from.size.width/from.size.height>to.width/to.height)
    {
        return CGRectMake(from.origin.x, from.origin.y, to.width, to.width*from.size.height/from.size.width);
    }
    else
    {
        return CGRectMake(from.origin.x, from.origin.y, to.height*from.size.width/from.size.height, to.height);
    }
    //    // 创建一个bitmap的context
    //    // 并把它设置成为当前正在使用的context
    //    UIGraphicsBeginImageContext(size);
    //    // 绘制改变大小的图片
    //    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
    //    // 从当前context中创建一个改变大小后的图片
    //    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    //    // 使当前的context出堆栈
    //    UIGraphicsEndImageContext();
    //    // 返回新的改变大小后的图片
    //
    //    //   NSData  *imageData=UIImageJPEGRepresentation(scaledImage, 1.f);
    //    return scaledImage;
}

+ (CGRect)rectAlign:(CGRect )parent rect:(CGRect)rect hAlign:(NSString*)hAlign 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([hAlign.lowercaseString isEqualToString:@"center"])
    {
        rect=CGRectMake(centerpoint.x-rect.size.width/2, rect.origin.y, rect.size.width, rect.size.height);
    }
    else
        if([hAlign.lowercaseString isEqualToString:@"left"])
        {
            rect=CGRectMake(parent.origin.x, rect.origin.y, rect.size.width, rect.size.height);
        }
        else
            if([hAlign.lowercaseString isEqualToString:@"right"])
            {
                rect=CGRectMake(parent.origin.x+parent.size.width-rect.size.width, rect.origin.y, rect.size.width, rect.size.height);
            }
    if([vAlign.lowercaseString isEqualToString:@"middle"])
    {
        rect=CGRectMake(rect.origin.x, centerpoint.y-rect.size.height/2, rect.size.width, rect.size.height);
    }
    
    return rect;
    
}
+ (NSData*) load_cached_img:(NSString*) filename loadFrom:(NSString*) path
{
    if(path.length==0)
        return nil;
    
    path=[path stringByReplacingOccurrencesOfString:@"https://" withString:@""];
    path=[path stringByReplacingOccurrencesOfString:@"http://" withString:@""];
    //    path=[path stringByReplacingOccurrencesOfString:filename withString:@""];
    
    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    
    if(appDelegate.bEnable_Cache==false)
        return nil;
    
    
    NSData* data = nil;
    
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachefolder = [paths objectAtIndex:0];
    NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
    NSString *filePath = [img_cache stringByAppendingPathComponent:path];
    
    NSFileManager* fileManager = [NSFileManager defaultManager];
    if(  [fileManager fileExistsAtPath:filePath ])
    {
        data = [NSData dataWithContentsOfFile: filePath];
    }
    

    return data;
    
}
+ (NSData*) load_img:(NSString*) filename
{
    if(filename.length==0)
        return nil;
    
    NSData* data = nil;
//    
//    
//    NSString* tempDir = NSTemporaryDirectory();
//    NSString *filePath = [tempDir stringByAppendingPathComponent:filename];
//    
    NSFileManager* fileManager = [NSFileManager defaultManager];
    if(  [fileManager fileExistsAtPath:filename ])
    {
        data = [NSData dataWithContentsOfFile: filename];
    }
    
    
    return data;
    
}
@end
