//
//  BRCommonPopup.m
//  DingDCommunity
//
//  Created by JianjiYuan on 14-4-13.
//  Copyright (c) 2014年 JianjiYuan. All rights reserved.
//

#import "BRCommonPopup.h"

@interface BRCommonPopup ()

@property (nonatomic, assign) CGRect rectViewContainer;

@end

@implementation BRCommonPopup

- (void)dealloc
{
    self.viewContainer = nil;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:[UIScreen mainScreen].applicationFrame];

    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        self.hidden = YES;
        self.clipsToBounds = YES;
        self.popupDirection = BRCommonPopupDirectionFromTop;
        self.popupArrow = BRCommonPopupArrowNone;
        
        
       // [self initBackgroudView];
        
    }
    return self;
}

- (void)initBackgroudView
{
    UIView *viewBg = [[UIView alloc] initWithFrame:self.bounds];
    viewBg.alpha = 0;
    viewBg.backgroundColor = [UIColor blackColor];
    [self addSubview:viewBg];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[touches anyObject] locationInView:self];
    
    if (!CGRectContainsPoint(self.viewContainer.frame, point)) {
        [self hideWithAnimated:YES];
    }
}

- (void)setViewContainer:(UIView *)viewContainer
{
    if (_viewContainer != viewContainer) {
        _viewContainer = viewContainer;
        //保存位置信息
        self.rectViewContainer = _viewContainer.frame;
        
        _viewContainer.layer.cornerRadius = 3;
        
        _viewContainer.layer.masksToBounds = YES;
        
        
        _viewContainer.layer.borderWidth = 2;
        
        _viewContainer.layer.borderColor = [[UIColor colorWithRed:0.70 green:0.84 blue:0.96 alpha:1] CGColor];
        
        
        
        [self addSubview:_viewContainer];
    }

}

- (void)showWithAnimated:(BOOL)animated
{
    UIImage *image = nil;
    if (self.popupArrow == BRCommonPopupArrowLeft) {
        image = [UIImage imageNamed:@"common_line_arrow_left.png"];
    }else if (self.popupArrow == BRCommonPopupArrowMiddle) {
        image = [UIImage imageNamed:@"common_line_arrow_middle.png"];
    }else if (self.popupArrow == BRCommonPopupArrowRight) {
        image = [UIImage imageNamed:@"common_line_arrow_right.png"];
    }
    
    if (self.popupArrow != BRCommonPopupArrowNone) {
        CGRect rectArrow = self.rectViewContainer;
        rectArrow.origin.y -= 6;
        rectArrow.size.height = 6;
        UIImageView *imageVArrow = [[UIImageView alloc] initWithFrame:rectArrow];
        imageVArrow.image = image;
        [self addSubview:imageVArrow];
    }
    
    if (animated) {
        
        if (self.isDropdown) {
            CGRect rectOrigin = self.viewContainer.frame;
            rectOrigin.size.height = 0;
            self.viewContainer.frame = rectOrigin;
        }else {
            CGRect rectOrigin = self.viewContainer.frame;
            if (self.popupDirection == BRCommonPopupDirectionFromTop) {
                rectOrigin.origin.y = -rectOrigin.size.height;
            }else if (self.popupDirection == BRCommonPopupDirectionFromButton) {
                rectOrigin.origin.y = CGRectGetHeight(self.bounds);
            }else if (self.popupDirection == BRCommonPopupDirectionFromLeft) {
                rectOrigin.origin.x = -CGRectGetWidth(self.bounds);
            }else if (self.popupDirection == BRCommonPopupDirectionFromRight) {
                rectOrigin.origin.x = CGRectGetWidth(self.bounds);
            }
            self.viewContainer.frame = rectOrigin;
        }
    
        self.hidden = NO;
        
        [UIView animateWithDuration:0.5 animations:^{
            self.viewContainer.frame = self.rectViewContainer;
        } completion:^(BOOL finished){
            
        }];
        
    }else {
        self.hidden = NO;
    }
}

- (void)hideWithAnimated:(BOOL)animated
{
    if (animated) {
        [UIView animateWithDuration:0.5 animations:^{
            
            if (self.isDropdown) {
                CGRect rectOrigin = self.viewContainer.frame;
                rectOrigin.size.height = 0;
                self.viewContainer.frame = rectOrigin;
            }else{
                CGRect rectOrigin = self.viewContainer.frame;
                if (self.popupDirection == BRCommonPopupDirectionFromTop) {
                    rectOrigin.origin.y = -rectOrigin.size.height;
                }else if (self.popupDirection == BRCommonPopupDirectionFromButton) {
                    rectOrigin.origin.y = CGRectGetHeight(self.bounds);
                }else if (self.popupDirection == BRCommonPopupDirectionFromLeft) {
                    rectOrigin.origin.x = -CGRectGetWidth(self.bounds);
                }else if (self.popupDirection == BRCommonPopupDirectionFromRight) {
                    rectOrigin.origin.x = CGRectGetWidth(self.bounds);
                }
                self.viewContainer.frame = rectOrigin;
            }
            
        } completion:^(BOOL finished){
            if (self.superview) {
                [self removeFromSuperview];
            }
        }];
        
    }else {
        if (self.superview) {
            [self removeFromSuperview];
        }
    }
    if (_delegate &&
        [_delegate respondsToSelector:@selector(commonPopupDidDismiss:)]) {
        [_delegate commonPopupDidDismiss:self];
    }
    
}

- (void)reloadDatas
{
    DLog(@"Reload Content Datas.");
}

@end
