//
//  CustomButton.m
//  JasonDevelop
//
//  Created by Jason Lee on 2013. 12. 16..
//  Copyright (c) jasondevelop. All rights reserved.
//
#import "UIView+AutoLayoutDebugging.h"
#import "UIImage+Addtions.h"
#import "CommonUtil.h"
#import "ImageUtil.h"
#import "CustomButton.h"

@interface CustomButton () {
    UIImage *_imgNormal, *_imgHighlighted;
    UIImage *_bgImageNormal, *_bgImageHighlited;
    UIEdgeInsets _insets, _bgInsets;
}

@end

@implementation CustomButton

#define kfRoundWidth 4.0f

- (void)setImageStratchForState:(UIControlState)state capInsets:(UIEdgeInsets)insets {

    UIImage *image = [self imageForState:state];
    image = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:image];
    [super setImage:image forState:state];
}

- (void)setBackgroundImageStretchForState:(UIControlState)state capInsets:(UIEdgeInsets)insets {

    UIImage *bimage = [self backgroundImageForState:state];
    bimage = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:bimage];
    [super setBackgroundImage:bimage forState:state];
}


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        if (!CGRectEqualToRect(_rectForCap, CGRectZero)) {//rectForCap이 설정된 경우, 이미지를 리사이즈함.

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

            _insets = UIEdgeInsetsMake(top, left, bottom, right);
            [self setImageStratchForState:UIControlStateNormal capInsets:_insets];
            [self setImageStratchForState:UIControlStateHighlighted capInsets:_insets];
        }

        if (!CGRectEqualToRect(_rectForCapBackground, CGRectZero)) {//rectForCap이 설정된 경우, 이미지를 리사이즈함.

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

            _bgInsets = UIEdgeInsetsMake(top, left, bottom, right);

            [self setBackgroundImageStretchForState:UIControlStateNormal capInsets:_bgInsets];
            [self setBackgroundImageStretchForState:UIControlStateHighlighted capInsets:_bgInsets];
            [self setBackgroundImageStretchForState:UIControlStateDisabled capInsets:_bgInsets];

        } else {//rectForCap이 설정되지 않은 경우, 픽스된 사이즈로 이미지를 리사이즈함.

            _bgInsets = UIEdgeInsetsMake(kfRoundWidth, kfRoundWidth, kfRoundWidth, kfRoundWidth);

            [self setBackgroundImageStretchForState:UIControlStateNormal capInsets:_bgInsets];
            [self setBackgroundImageStretchForState:UIControlStateHighlighted capInsets:_bgInsets];
            [self setBackgroundImageStretchForState:UIControlStateDisabled capInsets:_bgInsets];
        }
    }
    return self;
}

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

    if (!CGRectEqualToRect(_rectForCap, CGRectZero)) {//rectForCap이 설정된 경우, 이미지를 리사이즈함.

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

        _insets = UIEdgeInsetsMake(top, left, bottom, right);
        [self setImageStratchForState:UIControlStateNormal capInsets:_insets];
        [self setImageStratchForState:UIControlStateHighlighted capInsets:_insets];
    }

    if (!CGRectEqualToRect(_rectForCapBackground, CGRectZero)) {//rectForCap이 설정된 경우, 이미지를 리사이즈함.

        if (!CGRectEqualToRect(_rectForCapBackground, CGRectMake(-1, 0, 0, 0))) {
            CGFloat top, left, bottom, right;
            top = CGRectGetMinY(_rectForCapBackground);
            left = CGRectGetMinX(_rectForCapBackground);
            bottom = CGRectGetHeight(_rectForCapBackground);
            right = CGRectGetWidth(_rectForCapBackground);

            _bgInsets = UIEdgeInsetsMake(top, left, bottom, right);

            [self setBackgroundImageStretchForState:UIControlStateNormal capInsets:_bgInsets];
            [self setBackgroundImageStretchForState:UIControlStateHighlighted capInsets:_bgInsets];
            [self setBackgroundImageStretchForState:UIControlStateDisabled capInsets:_bgInsets];
        }

    } else {//rectForCap이 설정되지 않은 경우, 픽스된 사이즈로 이미지를 리사이즈함.

        _bgInsets = UIEdgeInsetsMake(kfRoundWidth, kfRoundWidth, kfRoundWidth, kfRoundWidth);

        [self setBackgroundImageStretchForState:UIControlStateNormal capInsets:_bgInsets];
        [self setBackgroundImageStretchForState:UIControlStateHighlighted capInsets:_bgInsets];
        [self setBackgroundImageStretchForState:UIControlStateDisabled capInsets:_bgInsets];
    }

    _textColorNormal = [self titleColorForState:UIControlStateNormal];

    if (_placeHolder) {
        [self setTitle:_placeHolder forState:UIControlStateNormal];
    }

    if (_placeHolderColor) {
        [self setTitleColor:_placeHolderColor forState:UIControlStateNormal];
    }

#ifdef DEBUG
    NSString *identifier = [NSString stringWithFormat:@"%@", [self titleForState:UIControlStateNormal]];
    if ([identifier isEqualToString:@"(null)"] || !identifier || [identifier isEqualToString:ksEmptyString]) {
        identifier = NSStringFromCGRect(self.frame);
    }
    [self setValue:identifier forKey:@"layoutDebuggingIdentifier"];
#endif
}

#pragma mark - Properties

- (void)setPlaceHolder:(NSString *)placeHolder {
    _placeHolder = placeHolder;
    [self setTitle:_placeHolder forState:UIControlStateNormal];
}

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state {//백그라운드 이미지를 버튼 크기에 맞게 조정함.
    image = [ImageUtil resizableImageWithCapInsets:_bgInsets resizingMode:UIImageResizingModeStretch img:image];
    [super setBackgroundImage:image forState:state];
}

- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];

    if ([self backgroundImageForState:UIControlStateNormal]) {//백그라운드 이미지를 버튼 크기에 맞게 조정함.
        [self setBackgroundImage:[ImageUtil stretchedImage:[self backgroundImageForState:UIControlStateNormal] expectSize:self.frame.size] forState:UIControlStateNormal];
    }

    if ([self backgroundImageForState:UIControlStateHighlighted]) {//백그라운드 이미지를 버튼 크기에 맞게 조정함.
        [self setBackgroundImage:[ImageUtil stretchedImage:[self backgroundImageForState:UIControlStateHighlighted] expectSize:self.frame.size] forState:UIControlStateHighlighted];
    }
}

//- (void)setUnderLine:(BOOL)setUnderLine {
//
//    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:[self titleForState:UIControlStateNormal]];
//    [attrString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [attrString length])];
//
//    UIColor* textColor = [self titleColorForState:UIControlStateNormal];
//
//    NSNumber *underLine = setUnderLine ? [NSNumber numberWithInteger:NSUnderlineStyleSingle] : [NSNumber numberWithInteger:NSUnderlineStyleNone];
//
//    [attrString setAttributes:@{NSForegroundColorAttributeName:textColor,NSUnderlineStyleAttributeName:underLine} range:NSMakeRange(0,[attrString length])];
//
//    [self setAttributedTitle:attrString forState:UIControlStateNormal];
//}

//- (void)setTitle:(NSString *)title forState:(UIControlState)state {
//    [super setTitle:title forState:UIControlStateNormal];
//
//    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:[self titleForState:state]];
//    UIColor* textColor = _textColorNormal ? _textColorNormal : [self titleColorForState:state];
//    [attrString setAttributes:@{NSForegroundColorAttributeName:textColor} range:NSMakeRange(0, [attrString length])];
//
//    [self setAttributedTitle:attrString forState:state];
//}
//
//- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state {
//    [super setTitleColor:color forState:state];
//
//    NSMutableAttributedString *attrString = nil;
//    if (![self attributedTitleForState:state]) {
//        attrString = [[NSMutableAttributedString alloc] initWithString:[self titleForState:state]];
//        [attrString addAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} range:NSMakeRange(0, [self titleForState:state].length)];
//    } else {
//        attrString = [[NSMutableAttributedString alloc] initWithAttributedString:[self attributedTitleForState:state]];
//        [attrString setAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} range:NSMakeRange(0, [self titleForState:state].length)];
//    }
//
//    [self setAttributedTitle:attrString forState:state];
//}


- (void)faceOffImage {
    if (!_imgNormal && !_imgHighlighted) {
        _imgNormal = [self imageForState:UIControlStateNormal];
        _imgHighlighted = [self imageForState:UIControlStateHighlighted];
    }

    if (_imgNormal && _imgHighlighted) {
        if ([_imgNormal isEqual:[self imageForState:UIControlStateNormal]]) {
            [self setImage:_imgHighlighted forState:UIControlStateNormal];
            [self setImage:_imgNormal forState:UIControlStateHighlighted];
        } else {
            [self setImage:_imgNormal forState:UIControlStateNormal];
            [self setImage:_imgHighlighted forState:UIControlStateHighlighted];
        }
    }

    //FIXME : weird
//    if (!_bgImageNormal && !_bgImageHighlited) {
        _bgImageNormal = [self backgroundImageForState:UIControlStateNormal];
        _bgImageHighlited = [self backgroundImageForState:UIControlStateHighlighted];
//    }

    if (_bgImageNormal && _bgImageHighlited) {
        if ([_bgImageNormal isEqual:[self backgroundImageForState:UIControlStateNormal]]) {

            [self setBackgroundImage:_bgImageHighlited forState:UIControlStateNormal];
            [self setBackgroundImage:_bgImageNormal forState:UIControlStateHighlighted];
        } else {
            [self setBackgroundImage:_bgImageNormal forState:UIControlStateNormal];
            [self setBackgroundImage:_bgImageHighlited forState:UIControlStateHighlighted];
        }

        [self setBackgroundImageStretchForState:UIControlStateNormal capInsets:_bgInsets];
        [self setBackgroundImageStretchForState:UIControlStateHighlighted capInsets:_bgInsets];
    }
}

@end


@implementation CustomLabelButton

@end
