#import "TGLocationPinView.h"

const CGSize TGLocationPinSize = { 13.5f, 36 };
const CGFloat TGLocationPinDamping = 2.0f;
const CGFloat TGLocationPinPinnedOrigin = 47;
const CGFloat TGLocationPinRaisedOrigin = 7;
const CGPoint TGLocationPinShadowPinnedOrigin = { 43, 47 };
const CGPoint TGLocationPinShadowRaisedOrigin = { 87, -33 };

@implementation TGLocationPinWrapperView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *view = [super hitTest:point withEvent:event];
    if (view == self)
        return nil;
    
    return view;
}

@end

@interface TGLocationPinView ()
{
    UIImageView *_pinView;
    UIImageView *_pinPointView;
    UIImageView *_shadowView;
}
@end

@implementation TGLocationPinView

- (instancetype)init
{
    self = [super initWithFrame:CGRectMake(0, 0, 100, 100)];
    if (self != nil)
    {
        self.userInteractionEnabled = false;
        
        _shadowView = [[UIImageView alloc] initWithFrame:CGRectMake(43, 47, 32, 39)];
        _shadowView.alpha = 0.9f;
        _shadowView.image = [UIImage imageNamed:@"LocationPinShadow.png"];
        [self addSubview:_shadowView];
        
        _pinPointView = [[UIImageView alloc] initWithFrame:CGRectMake(CGFloor(self.frame.size.width / 2 - 2), self.frame.size.height - 18.5f, 3.5f, 1.5f)];
        _pinPointView.image = [UIImage imageNamed:@"LocationPinPoint.png"];
        [self addSubview:_pinPointView];
        
        _pinView = [[UIImageView alloc] initWithFrame:CGRectMake(CGFloor(self.frame.size.width / 2 - 7), 47, 13.5f, 36)];
        _pinView.image = [UIImage imageNamed:@"LocationPin.png"];
        [self addSubview:_pinView];
    }
    return self;
}

- (void)setPinRaised:(bool)pinRaised
{
    [self setPinRaised:pinRaised animated:false completion:nil];
}

- (void)setPinRaised:(bool)raised animated:(bool)animated completion:(void (^)(void))completion
{
    _pinRaised = raised;
    
    [_pinView.layer removeAllAnimations];
    [_shadowView.layer removeAllAnimations];
    
    if (animated)
    {
        if (raised)
        {
            [UIView animateWithDuration:0.2f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState animations:^
            {
                _pinView.frame = CGRectMake(_pinView.frame.origin.x, TGLocationPinRaisedOrigin, TGLocationPinSize.width, TGLocationPinSize.height);
                _shadowView.frame = CGRectMake(TGLocationPinShadowRaisedOrigin.x, TGLocationPinShadowRaisedOrigin.y, _shadowView.frame.size.width, _shadowView.frame.size.height);
            } completion:^(BOOL finished)
            {
                if (finished && completion != nil)
                    completion();
            }];
        }
        else
        {
            [UIView animateWithDuration:0.2f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState animations:^
            {
                _pinView.frame = CGRectMake(_pinView.frame.origin.x, TGLocationPinPinnedOrigin, TGLocationPinSize.width, TGLocationPinSize.height);
                _shadowView.frame = CGRectMake(TGLocationPinShadowPinnedOrigin.x, TGLocationPinShadowPinnedOrigin.y, _shadowView.frame.size.width, _shadowView.frame.size.height);
            } completion:^(BOOL finished)
            {
                if (finished)
                {
                    [UIView animateWithDuration:0.1f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState animations:^
                    {
                        _pinView.frame = CGRectMake(_pinView.frame.origin.x, TGLocationPinPinnedOrigin + TGLocationPinDamping, TGLocationPinSize.width, TGLocationPinSize.height - TGLocationPinDamping);
                    } completion:^(BOOL finished)
                    {
                        if (finished)
                        {
                            [UIView animateWithDuration:0.1f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState animations:^
                            {
                                 _pinView.frame = CGRectMake(_pinView.frame.origin.x, TGLocationPinPinnedOrigin, TGLocationPinSize.width, TGLocationPinSize.height);
                            } completion:^(BOOL finished)
                            {
                                if (finished && completion != nil)
                                    completion();
                            }];
                        }
                    }];
                }
            }];
        }
    }
    else
    {
        _pinView.frame = CGRectMake(_pinView.frame.origin.x, raised ? TGLocationPinRaisedOrigin : TGLocationPinPinnedOrigin, TGLocationPinSize.width, TGLocationPinSize.height);
        
        CGPoint shadowOrigin = raised ? TGLocationPinShadowRaisedOrigin : TGLocationPinShadowPinnedOrigin;
        _shadowView.frame = CGRectMake(shadowOrigin.x, shadowOrigin.y, _shadowView.frame.size.width, _shadowView.frame.size.height);
        
        if (completion != nil)
            completion();
    }
}

@end
