//
//  UIView+DDCategory.m
//  DDCategoryKit_Private
//  Created by DDIsFriend on 2022/7/29.

#import "UIView+DDCategory.h"
@implementation UIView (DDCategory)
- (CGFloat)width{
    return self.frame.size.width;
}

- (CGFloat)height{
    return self.frame.size.height;
}

- (CGFloat)x{
    return self.frame.origin.x;
}

- (CGFloat)y{
    return self.frame.origin.y;
}

- (void)setWidth:(CGFloat)width{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (void)setHeight:(CGFloat)height{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (void)setX:(CGFloat)x{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (void)setY:(CGFloat)y{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}


@end

@implementation UIView (Layer)
+ (UIView *)dd_layerWithShadowColor:(CGColorRef)shadowColor shadowOffset:(CGSize)shadowOffset shadowRadius:(CGFloat)shadowRadius shadowOpacity:(CGFloat)shadowOpacity  cornerRadius:(CGFloat)cornerRadius viewBackgroundColor:(nullable UIColor *)viewBackgroundColor{
    UIView *view = [UIView new];

    view.layer.shadowColor = shadowColor;
    view.layer.shadowOffset = shadowOffset;
    view.layer.shadowRadius = shadowRadius;
    view.layer.shadowOpacity = shadowOpacity;
    
    view.layer.cornerRadius = cornerRadius;
    
    view.backgroundColor = viewBackgroundColor;
    
    view.layer.shouldRasterize = YES;
    view.layer.rasterizationScale = UIScreen.mainScreen.scale;
    return view;
}

- (void)dd_layerWithShadowColor:(CGColorRef)shadowColor shadowOffset:(CGSize)shadowOffset shadowRadius:(CGFloat)shadowRadius shadowOpacity:(CGFloat)shadowOpacity  cornerRadius:(CGFloat)cornerRadius viewBackgroundColor:(nullable UIColor *)viewBackgroundColor{
    if (!self) return ;
    
    self.layer.shadowColor = shadowColor;
    self.layer.shadowOffset = shadowOffset;
    self.layer.shadowRadius = shadowRadius;
    self.layer.shadowOpacity = shadowOpacity;

    self.layer.cornerRadius = cornerRadius;
    
    self.backgroundColor = viewBackgroundColor;
    
    self.layer.shouldRasterize = YES;
    self.layer.rasterizationScale = UIScreen.mainScreen.scale;
}
@end

@implementation UIView (Controller)

- (nullable UIViewController *)dd_currentViewController {
    return [UIView dd_viewControllerOfView:self];
}

+ (nullable UIViewController *)dd_viewControllerOfView:(nonnull UIView *)view {
    // 遍历响应者链。返回第一个找到视图控制器
    UIResponder *responder = view;
    while ((responder = [responder nextResponder])){
        if ([responder isKindOfClass: [UIViewController class]]){
            return (UIViewController *)responder;
        }
    }
    // 如果没有找到则返回nil
    return nil;
}

@end
