//
//  UICheckBox.m
//  JasonDevelop
//
//  Created by Hslee on 10. 11. 22..
//  Copyright 2013 AnchorData. All rights reserved.
//
@import ObjectiveC.runtime;
#import "CustomCheckBox.h"

@interface CustomCheckBox () {
    UIImage *_imgNormal, *_imgHighlighted, *_imgDisbale;
    UIColor *_textColorNormal, *_textColorHighlight;
}

@end

@implementation CustomCheckBox
@synthesize checked = _checked;

- (id)initWithFrame:(CGRect)frame {//소스레벨에서 로드하는 경우, 초기화
    if (self = [super initWithFrame:frame]) {
        // Initialization code

        _imgNormal = [UIImage imageNamed:@"common_checkbox_default"];
        _imgHighlighted = [UIImage imageNamed:@"common_checkbox_checked"];
        _imgDisbale = [UIImage imageNamed:@"common_checkbox_disable"];

		self.contentHorizontalAlignment  = UIControlContentHorizontalAlignmentLeft;

		[self setImage:_imgNormal forState:UIControlStateNormal];
		[self addTarget:self action:@selector(checkBoxClicked) forControlEvents:UIControlEventTouchUpInside];
	}
    return self;
}



- (instancetype)initWithFrame:(CGRect)frame normalImage:(UIImage *)normalImage highlightImage:(UIImage *)highlightImage {//소스레벨에서 로드하는 경우, 초기화

    if (self = [super initWithFrame:frame]) {

        _imgNormal = normalImage;
        _imgHighlighted = highlightImage;

        self.contentHorizontalAlignment  = UIControlContentHorizontalAlignmentLeft;

        [self setImage:_imgNormal forState:UIControlStateNormal];
        [self addTarget:self action:@selector(checkBoxClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}


- (void)awakeFromNib {//NIB에서 로드하는 경우, 초기화

	self.contentHorizontalAlignment  = UIControlContentHorizontalAlignmentLeft;

    _imgNormal = [UIImage imageNamed:@"common_checkbox_default"];
    _imgHighlighted = [UIImage imageNamed:@"common_checkbox_checked"];
    _imgDisbale = [UIImage imageNamed:@"common_checkbox_disable"];

    _textColorNormal = [self titleColorForState:UIControlStateNormal];
    _textColorHighlight = [self titleColorForState:UIControlStateHighlighted];

	[self setImage:_imgNormal forState:UIControlStateNormal];
	[self addTarget:self action:@selector(checkBoxClicked) forControlEvents:UIControlEventTouchUpInside];
}

- (IBAction)checkBoxClicked {
	if (self.checked == NO) {
		self.checked = YES;
	} else {
		self.checked = NO;
	}

	if ([self.delegate respondsToSelector:@selector(didCheckBoxClicked:)]) {
		[self.delegate didCheckBoxClicked:self];
	}
}

- (BOOL)isChecked {
	return _checked;
}

- (BOOL)checked {
    return _disable ? NO : _checked;
}


//체크 On/Off
- (void)setChecked:(BOOL)checked {

	_checked = checked;

	if (_checked) {
		[self setImage:_imgHighlighted forState:UIControlStateNormal];
        [self setTitleColor:_textColorHighlight forState:UIControlStateNormal];
	} else {
		[self setImage:_imgNormal forState:UIControlStateNormal];
        [self setTitleColor:_textColorNormal forState:UIControlStateNormal];
	}
    if (_value) {
        objc_setAssociatedObject(_value, ksCustomCheckBoxStatus, [NSNumber numberWithBool:_checked], OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
}

- (BOOL)getCheckStatusFromValue {
    BOOL isChecked = NO;
    if (_value) {
         isChecked = [objc_getAssociatedObject(_value, ksCustomCheckBoxStatus) boolValue];
    }
    return isChecked;
}

//disable On/Off
- (void)setDisable:(BOOL)disable {
    _disable = _checked = disable;

	if (_disable && _imgDisbale) {
		[self setImage:_imgDisbale forState:UIControlStateNormal];
	} else {
        [self setCheckedDisable:_checkedDisable];
	}
}

//체크된 상태에서 On/Off
- (void)setCheckedDisable:(BOOL)checkedDisable {
    _checkedDisable = _checked = checkedDisable;

	if (_checkedDisable) {
		[self setImage:_imgDisbale forState:UIControlStateNormal];
	} else {
		[self setImage:_imgNormal forState:UIControlStateNormal];
	}
}



@end
