//
//  ImageBrowserCell.m
//  TaiZhouTobacco_7s
//
//  Created by JianjiYuan on 14-9-11.
//  Copyright (c) 2014年 JianjiYuan. All rights reserved.
//

#import "ImageBrowserCell.h"

@interface ImageBrowserCell ()
{
    UILongPressGestureRecognizer *longGestureRec;
}

@end

@implementation ImageBrowserCell

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

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self.imageVPhoto autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
        
        [self.btnDelete autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
        [self.btnDelete autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:0];
        [self.btnDelete autoSetDimensionsToSize:CGSizeMake(20, 20)];
        self.btnDelete.hidden = YES;
        
    }
    return self;
}

- (void)setCanBeDelete:(BOOL)canBeDelete
{
    if (canBeDelete) {
        longGestureRec = [[UILongPressGestureRecognizer alloc]
                                                        initWithTarget:self
                                                        action:@selector(longGestureTaped:)];
        longGestureRec.minimumPressDuration = 0.4;
        [self addGestureRecognizer:longGestureRec];
    }else {
        if (longGestureRec) {
            [self removeGestureRecognizer:longGestureRec];
            longGestureRec = nil;
        }
    }
}

#pragma mark ----- Action Handler -----

- (void)longGestureTaped:(UILongPressGestureRecognizer *)gesture
{
    if (self.isShaking == NO) {
        
        if (gesture.state == UIGestureRecognizerStateBegan) {
            if (_delegate && [_delegate respondsToSelector:@selector(didLongGestureTapedImageCell:)]) {
                [_delegate didLongGestureTapedImageCell:self];
            }
            [self beginShake];
        }
    }
    
}

//停止晃动
- (void)stopShake
{
//    CALayer *viewLayer = [self layer];
//    [viewLayer removeAnimationForKey:@"wiggle"];
    self.btnDelete.hidden = YES;
    
    self.isShaking = NO;
    _imageVPhoto.userInteractionEnabled = YES;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (self.isShaking) {
        [self stopShake];
        if (_delegate && [_delegate respondsToSelector:@selector(didStopShaking:)]) {
            [_delegate didStopShaking:self];
        }
        
    }
    
}

//开始晃动
- (void)beginShake
{
//    CALayer*viewLayer=[self layer];
//    CABasicAnimation*animation=[CABasicAnimation animationWithKeyPath:@"transform"];
//    animation.duration=0.1;
//    animation.repeatCount = MAXFLOAT;
//    animation.autoreverses=YES;
//    animation.fromValue=[NSValue valueWithCATransform3D:CATransform3DRotate
//                         (viewLayer.transform, -0.1, 0.1, 0.1, 0.1)];
//    
//    animation.toValue=[NSValue valueWithCATransform3D:CATransform3DRotate
//                       (viewLayer.transform, 0.1, 0.1, 0.1, 0.1)];
//    [viewLayer addAnimation:animation forKey:@"wiggle"];
    
    self.btnDelete.hidden = NO;
    [self bringSubviewToFront:self.btnDelete];
    
    self.isShaking = YES;
    _imageVPhoto.userInteractionEnabled = NO;
    
}

- (UIButton *)btnDelete
{
    if (!_btnDelete) {
        self.btnDelete = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnDelete.translatesAutoresizingMaskIntoConstraints = NO;
        _btnDelete.backgroundColor = [UIColor clearColor];
        [_btnDelete setBackgroundImage:[UIImage imageNamed:@"Login/deletePic@2x.png"] forState:UIControlStateNormal];
        [self.contentView addSubview:_btnDelete];
    }
    return _btnDelete;
}

- (UIImageView *)imageVPhoto
{
    if (!_imageVPhoto) {
        self.imageVPhoto = [UIImageView newAutoLayoutView];
        _imageVPhoto.autoresizingMask = UIViewAutoresizingNone;
        //_imageVPhoto.layer.borderColor = RGBColor(0, 120, 193, 1).CGColor;
        //_imageVPhoto.layer.borderWidth = 0.5;
        _imageVPhoto.contentMode = UIViewContentModeScaleAspectFit;
        _imageVPhoto.layer.cornerRadius
        = 6;
        //_imageVPhoto.backgroundColor = RGBColor(216, 216, 216, 1);
        _imageVPhoto.layer.masksToBounds
        = YES;
        
        
        [self.contentView addSubview:_imageVPhoto];
    }
    return _imageVPhoto;
}

@end
