//
//  UIView+DHExtension.m
//  DHZL
//
//  Created by Mazy on 2020/6/24.
//  Copyright © 2020 Mazy. All rights reserved.
//

#import "UIView+DHExtension.h"

@implementation UIView (DHExtension)

- (void)addEmptyDataSet: (CGFloat)offset image: (UIImage *)image title: (NSString *)title {
    UIView *tagView = [self viewWithTag:1280];
    if (tagView) {
        [tagView removeFromSuperview];
    }
    
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.tag = 1280;
    [self addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    
    UIImageView *iconView = [[UIImageView alloc] initWithImage: image];
    iconView.contentMode = UIViewContentModeCenter;
    [contentView addSubview:iconView];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.centerY.mas_equalTo(self).mas_offset(-50);
    }];
    
    UILabel *tipLabel = [[UILabel alloc] init];
    tipLabel.text = title;
    tipLabel.textColor = kColorFromRGB(0xCED3DB);
    tipLabel.font = [UIFont systemFontOfSize:14];
    tipLabel.textAlignment = NSTextAlignmentCenter;
    [contentView addSubview:tipLabel];
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.top.mas_equalTo(iconView.mas_bottom).mas_offset(20);
    }];
}

-(void)removeEmptyDataSet: (BOOL)animated {
    UIView *vw = [self viewWithTag:1280];
    if (vw) {

        for (UIView *v in vw.subviews) {
            [v.layer removeAllAnimations];
        }
        
        if (animated) {
            [UIView animateWithDuration:0.25 animations:^{
                vw.alpha = 0;
            } completion:^(BOOL finished) {
                for (UIView *v in vw.subviews) {
                    [v removeFromSuperview];
                }
                [vw removeFromSuperview];
            }];
        } else {
            vw.alpha = 0;
            for (UIView *v in vw.subviews) {
                [v removeFromSuperview];
            }
            [vw removeFromSuperview];
        }
    }
}

- (void)roundCorners: (UIRectCorner)corners radius: (CGFloat)radius {
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii: CGSizeMake(radius, radius)];
    CAShapeLayer *shape = [CAShapeLayer layer];
    shape.path = maskPath.CGPath;
    self.layer.mask = shape;
}

/// SwifterSwift: Set some or all corners radiuses of view.
///
/// - Parameters:
///   - corners: array of corners to change (example: [.bottomLeft, .topRight]).
///   - radius: radius for selected corners.
//func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
//    let maskPath = UIBezierPath(
//        roundedRect: bounds,
//        byRoundingCorners: corners,
//        cornerRadii: CGSize(width: radius, height: radius))
//
//    let shape = CAShapeLayer()
//    shape.path = maskPath.cgPath
//    layer.mask = shape
//}


@end
