
#import "SLPageControlSlideRound.h"


#define Define_RoundSize 12.0f

#define Define_RoundSpace 7.0f

@interface SLPageControlSlideRound()

@property (nonatomic,strong) NSArray *imageArray;

@end

@implementation SLPageControlSlideRound

@synthesize imageArray;

@synthesize sizeOfRound =_sizeOfRound;

@synthesize spaceWithRound =_spaceWithRound;

@synthesize count =_count;

@synthesize imageOfSelected;
@synthesize imageOfUnSelected;
@synthesize alignment =_alignment;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
        
        self.backgroundColor =[UIColor clearColor];
        
        _slideFrame =frame;
        
		_slideSize =frame.size;
        
        _sizeOfRound =CGSizeMake(Define_RoundSize, Define_RoundSize);
        
        _spaceWithRound =Define_RoundSpace;
        
        _selectIndex =0;
        
        _dotAlpha = 0.8;
    }
    return self;
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    _slideFrame = frame;
    _slideSize = frame.size;
    [self layoutIfNeeded];
}

-(id)initWithFrame:(CGRect)frame andRoundCount:(int)count{

	if (self =[self initWithFrame:frame]) {
		
        [self setCount:count];
	}
	return self;
}

-(void)layoutSubviews{
	
    CGFloat width =_slideFrame.size.width;

    CGFloat firstRoundOrigin_X =0;
    
    switch (_alignment) {
        case SlideRoundAlignmentLeft:
        {
            firstRoundOrigin_X = 0;
        }
            break;
            
        case SlideRoundAlignmentRight:
        {
            firstRoundOrigin_X =width -self.count *(_sizeOfRound.width+_spaceWithRound);
        }
            break;
            
        case SlideRoundAlignmentCenter:
        {
            if ((self.count %2) ==0) {
                
                firstRoundOrigin_X =width/2 -self.count *(_sizeOfRound.width+_spaceWithRound)/2 +_spaceWithRound/2;
                
            }
            else{
                firstRoundOrigin_X =width/2 -(self.count-1) *(_sizeOfRound.width+_spaceWithRound)/2 -_sizeOfRound.width/2;
            }
        }
            break;
            
        default:
        {
            firstRoundOrigin_X =0;
        }
            break;
    }
    
	for (int i=0; i<self.count; i++) {
		
		UIImageView *imgView =[self.imageArray objectAtIndex:i];
		
		if (i == 0) {
			imgView.image =self.imageOfSelected;
		}
        else{
            imgView.image =self.imageOfUnSelected;
        }

        CGRect tempRect =CGRectMake(firstRoundOrigin_X +i*(_sizeOfRound.width +_spaceWithRound), (CGRectGetHeight(self.frame) - _sizeOfRound.height) / 2.0, _sizeOfRound.width, _sizeOfRound.height);
        
		imgView.frame =tempRect;
        
        if (_selctSizeOfRound.width > 0) {
            if (i == _currentSel) {
                imgView.image =self.imageOfSelected;
            }
            else{
                imgView.image =self.imageOfUnSelected;
            }
            CGRect imgFrame = imgView.frame;
            imgFrame.origin = CGPointMake(imgFrame.origin.x - (_selctSizeOfRound.width - _sizeOfRound.width), imgFrame.origin.y);
            imgFrame = imgFrame;
            if (i < _currentSel) {
                imgFrame.size = _sizeOfRound;
                imgView.frame = imgFrame;
            }else if (i == _currentSel) {
                imgFrame.size = _selctSizeOfRound;
                imgView.frame = imgFrame;
            }else {
                imgFrame.origin = CGPointMake(imgFrame.origin.x + _selctSizeOfRound.width - _sizeOfRound.width, imgFrame.origin.y);
                imgView.frame = imgFrame;
            }
        }
        imgView.contentMode = UIViewContentModeScaleAspectFit;
		imgView.alpha =_dotAlpha;
        
		[self addSubview:imgView];
	}
}

-(void)slideRoundSelectIndex:(NSInteger)index{
    
	for (int i=0; i<self.count; i++) {
		
		UIImageView *imgView =[self.imageArray objectAtIndex:i];
		
		if (i <= index) {
			imgView.image = self.imageOfSelected;
            
            imgView.backgroundColor = [UIColor colorFormString:@"#87B9FF"];
		}
		else {
			imgView.image =self.imageOfUnSelected;
            imgView.backgroundColor = UIColor.whiteColor;
		}
	}
}

-(void)slideRoundAutoSelectIndex:(int)index{
    _currentSel = index;
    [self setNeedsLayout];
}

-(void)setSizeOfRound:(CGSize)sizeOfRound{
    _sizeOfRound =sizeOfRound;
    [self layoutSubviews];
}

- (void)setSelctSizeOfRound:(CGSize)selctSizeOfRound {
    _selctSizeOfRound = selctSizeOfRound;
    [self layoutSubviews];
}

-(void)setSpaceWithRound:(float)spaceWithRound {

    _spaceWithRound =spaceWithRound;
    
    [self setNeedsLayout];
}

-(void)setCount:(NSUInteger)count {
    for (id obj in self.imageArray) {
        if ([obj isKindOfClass:[UIView class]]) {
            UIView *v =(UIView *)obj;
            [v removeFromSuperview];
        }
    }
    _count =count;
    NSMutableArray *array =[[NSMutableArray alloc] initWithCapacity:_count];
    
    for (int i=0; i<_count; i++) {
        
        UIImageView *imgView =[[UIImageView alloc] init];
        imgView.image =self.imageOfUnSelected;
        imgView.backgroundColor = UIColor.whiteColor;
        imgView.layer.cornerRadius = self.sizeOfRound.height / 2.0;
        imgView.clipsToBounds = YES;
        
        [array addObject:imgView];
    }
    
    self.imageArray =[NSArray arrayWithArray:array];
    
    [self setNeedsLayout];
}


-(void)setAlignment:(SlideRoundAlignment)alignment{
    _alignment =alignment;
    [self setNeedsLayout];
}

@end
