//
//  CustomImageView.m
//  JasonDevelop
//
//  Created by Jason Lee on 10/25/14.
//  Copyright (c) jasondevelop. All rights reserved.
//
#import "UIView+AutoLayoutDebugging.h"
#import "CommonUtil.h"
#import "CustomImageView.h"
#import "ImageUtil.h"

@implementation CustomImageView

#define kfRoundWidth 4.0f


- (id)initWithImage:(UIImage *)image rectForCap:(CGRect)rectForCap {
    if (self = [super initWithImage:image]) {

        _rectForCap = rectForCap;

        CGFloat top, left, bottom, right;
        top = CGRectGetMinY(_rectForCap);
        left = CGRectGetMinX(_rectForCap);
        bottom = CGRectGetHeight(_rectForCap);
        right = CGRectGetWidth(_rectForCap);

         self.image =  [ImageUtil resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right) resizingMode:UIImageResizingModeStretch img:self.image];
    }
    return self;
}

- (void)awakeFromNib {//NIB로 로드될 경우,

    if (CGRectEqualToRect(_rectForCap, CGRectZero)) {
        self.image =  self.image;
    } else {
            CGFloat top, left, bottom, right;
            top = CGRectGetMinY(_rectForCap);
            left = CGRectGetMinX(_rectForCap);
            bottom = CGRectGetHeight(_rectForCap);
            right = CGRectGetWidth(_rectForCap);
            
            self.image =  [ImageUtil resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right) resizingMode:UIImageResizingModeStretch img:self.image];
    }

#ifdef DEBUG
    NSString *identifier = [NSString stringWithFormat:@"CustomImage:(%@)", NSStringFromCGRect(self.frame)];
    [self setValue:identifier forKey:@"layoutDebuggingIdentifier"];
#endif
}

- (void)setImage:(UIImage *)image {

    UIImage *sImage = nil;
    if (!CGRectEqualToRect(_rectForCap, CGRectZero)) {
        sImage =  image;
    } else {
        CGFloat top, left, bottom, right;
        top = CGRectGetMinY(_rectForCap);
        left = CGRectGetMinX(_rectForCap);
        bottom = CGRectGetHeight(_rectForCap);
        right = CGRectGetWidth(_rectForCap);

        sImage =  [ImageUtil resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right) resizingMode:UIImageResizingModeStretch img:image];
    }

    [super setImage:sImage];
}

@end
