//
//  IFLYDialogView.m
//  YouBaozhang
//
//  Created by Chenly on 2017/4/17.
//  Copyright © 2017年 Yonyou. All rights reserved.
//

#import "IFLYDialogView.h"

#import <MBProgressHUD/MBProgressHUD.h>

@interface IFLYDialogView ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) MBProgressHUD *hud;
@property (nonatomic, strong) UILabel *detailsLabel;
@property (nonatomic, strong) UILabel *noticeLabel;
@property (nonatomic, strong) UITapGestureRecognizer *tapGesture;
@property (nonatomic, strong) UISwipeGestureRecognizer *swipeGesture;

@property (nonatomic, assign) BOOL showing;

@end

@implementation IFLYDialogView

- (instancetype)init {
    if (self = [super init]) {
        
        self.backgroundColor = [UIColor clearColor];
        [self addSubview:self.imageView];
    }
    return self;
}

- (CGSize)intrinsicContentSize {
    return CGSizeMake(120, 100);
}

- (void)layoutSubviews {
    self.imageView.frame = CGRectInset(self.bounds, 10, 10);
    [self layoutNoticeLabel];
}

- (void)layoutDetailsLabel {
    CGFloat width = CGRectGetWidth(self.hud.frame) - 40.f * 2;
    CGSize size = [self.detailsLabel sizeThatFits:CGSizeMake(width, CGFLOAT_MAX)];
    self.detailsLabel.frame = CGRectMake(0, 0, size.width, size.height);
    CGPoint center = [self.superview convertPoint:self.center toView:self.hud];
    center.y += self.intrinsicContentSize.height / 2 + 60.f;
    self.detailsLabel.center = center;
}

- (void)layoutNoticeLabel {
    [self.noticeLabel sizeToFit];
    CGPoint point = CGPointZero;
    point.x = CGRectGetWidth(self.hud.frame) / 2.f;
    point.y = CGRectGetHeight(self.hud.frame) - CGRectGetHeight(self.noticeLabel.frame) - 60.f;
    self.noticeLabel.center = point;
}

#pragma mark - getter & setter

- (MBProgressHUD *)hud {
    
    if (!_hud) {
        _hud = [[MBProgressHUD alloc] init];
        _hud.mode = MBProgressHUDModeCustomView;
        _hud.customView = self;
        _hud.graceTime = 0.1;
        _hud.bezelView.color = [UIColor colorWithWhite:0.2 alpha:0.6];
        _hud.backgroundView.color = [UIColor colorWithWhite:0.2 alpha:0.3];
        _hud.offset = CGPointMake(0, -60);
        _hud.removeFromSuperViewOnHide = YES;
    }
    return _hud;
}

- (UIImageView *)imageView {
    
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
        _imageView.highlightedImage = [UIImage imageNamed:@"ifly_cancel"];
    }
    return _imageView;
}

- (UILabel *)detailsLabel {
    
    if (!_detailsLabel) {
        _detailsLabel = [[UILabel alloc] init];
        _detailsLabel.numberOfLines = 0;
        _detailsLabel.backgroundColor = [UIColor clearColor];
        _detailsLabel.textColor = [UIColor whiteColor];
        _detailsLabel.font = [UIFont systemFontOfSize:15.f];
    }
    return _detailsLabel;
}

- (UILabel *)noticeLabel {
    
    if (!_noticeLabel) {
        _noticeLabel = [[UILabel alloc] init];
        _noticeLabel.backgroundColor = [UIColor clearColor];
        _noticeLabel.textColor = [UIColor whiteColor];
        _noticeLabel.font = [UIFont systemFontOfSize:15.f];
        _noticeLabel.textAlignment = NSTextAlignmentCenter;
        _noticeLabel.text = @"点击结束，上滑取消";
    }
    return _noticeLabel;
}

- (void)setVolume:(float)volume {
    
    _volume = volume;
    int imageIndex = volume;
    if (volume < 1) {
        imageIndex = 1;
    } else if (volume >= 7) {
        imageIndex = 7;
    }
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"ifly_record%d", imageIndex]];
    self.imageView.image = image;
    [self setNeedsDisplay];
}

- (void)setDetailText:(NSString *)detailText {
    
    self.detailsLabel.text = detailText;
    [self layoutDetailsLabel];
}

- (NSString *)detailText {
    return self.detailsLabel.text;
}

- (UITapGestureRecognizer *)tapGesture {

    if (!_tapGesture) {
        _tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(done)];
        [_tapGesture requireGestureRecognizerToFail:self.swipeGesture];
    }
    return _tapGesture;
}

- (UISwipeGestureRecognizer *)swipeGesture {
    
    if (!_swipeGesture) {
        _swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cancel)];
        _swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
    }
    return _swipeGesture;
}

#pragma mark - gesture actions

- (void)done {

    [self hide:YES];
    if ([self.delegate respondsToSelector:@selector(ifly_dialogView:askHidingWithCancel:)]) {
        [self.delegate ifly_dialogView:self askHidingWithCancel:NO];
    }
}

- (void)cancel {
    
    [self hide:YES];
    if ([self.delegate respondsToSelector:@selector(ifly_dialogView:askHidingWithCancel:)]) {
        [self.delegate ifly_dialogView:self askHidingWithCancel:YES];
    }
}

#pragma mark - public

- (void)showInView:(UIView *)view animated:(BOOL)animated volume:(float)volume {
    
    if (self.hud.superview && self.hud.superview != view) {
        [self.detailsLabel removeFromSuperview];
        [self.hud removeFromSuperview];
    }
    
    self.hud.frame = view.bounds;
    [self.hud addSubview:self.detailsLabel];
    [self.hud addSubview:self.noticeLabel];
    [view addSubview:self.hud];
    self.volume = volume;
    [self.hud showAnimated:YES];
    [self.hud addGestureRecognizer:self.tapGesture];
    [self.hud addGestureRecognizer:self.swipeGesture];
}

- (void)hide:(BOOL)animated {
    
    if (!self.hud.superview) {
        return;
    }
    [self.hud hideAnimated:animated];
    [self.detailsLabel removeFromSuperview];
    [self.hud removeGestureRecognizer:self.tapGesture];
    [self.hud removeGestureRecognizer:self.swipeGesture];
}

@end
