//
//  YMNavigationBar.m
//  PCDBank
//
//  Created by Gusont on 2020/12/1.
//  Copyright © 2020 YMBank. All rights reserved.
//

#import "YMNavigationBar.h"

#define YMNavBarSubViewHeight 44.0
@interface YMNavigationBar()

@property (nonatomic, strong) YMBarItemView *leftQuitButtonItemView;

@property (nonatomic, strong) UIView *separatorView;

@property (nonatomic, strong) UIImageView *backgroundImageView;

@end

@implementation YMNavigationBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self confignNavigationBar];
    }
    return self;
}

- (void)confignNavigationBar
{
    [self.backgroundImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    self.titleItem = [[YMBarItem alloc] init];
    self.KVOController = [FBKVOController controllerWithObserver:self];
    [YMBarItem mj_enumerateProperties:^(MJProperty *property, BOOL *stop) {
        [self.KVOController observe:self.titleItem keyPath:property.name options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nonnull change) {
            [self setTitleItem:object];
        }];
    }];
}

+ (CGFloat)navigationBarTopInterval
{
    return kStatusBarHeight;
}

#pragma mark - property set方法
- (void)setHidesBackButton:(BOOL)hidesBackButton
{
    _hidesBackButton = hidesBackButton;
    self.leftBarButtonItemView.hidden = YES;
}

- (void)setLeftBarButtonItem:(YMBarButtonItem *)leftBarButtonItem
{
    if (leftBarButtonItem) {
        _leftBarButtonItem = leftBarButtonItem;
        [_leftBarButtonItemView removeFromSuperview];
        _leftBarButtonItemView = nil;
        [self.KVOController observe:_leftBarButtonItem keyPath:@"buttonWidth" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld block:^(id  _Nullable observer, YMBarButtonItem  *_Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nonnull change) {
            [self.leftBarButtonItemView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(@(object.buttonWidth));
            }];
        }];
        self.leftBarButtonItemView.hidden = NO;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:leftBarButtonItem.target action:leftBarButtonItem.action];
        [self.leftBarButtonItemView addGestureRecognizer:tap];
        self.leftBarButtonItemView.itemImageView.image = leftBarButtonItem.image;
        self.leftBarButtonItemView.itemTitleLabel.text = leftBarButtonItem.title;
        self.leftBarButtonItemView.itemTitleLabel.textColor = leftBarButtonItem.titleColor;
        self.leftBarButtonItemView.itemTitleLabel.font = leftBarButtonItem.font ?: [UIFont systemFontOfSize:15.f];
        self.leftBarButtonItemView.itemTitleLabel.textAlignment = NSTextAlignmentLeft;
        [self.leftBarButtonItemView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self);
            make.width.equalTo(@(_leftBarButtonItem.buttonWidth));
            make.height.equalTo(@(YMNavBarSubViewHeight));
            make.left.equalTo(@(10));
        }];
        self.leftBarButtonItemView.isAccessibilityElement = YES;
        self.leftBarButtonItemView.accessibilityLabel = @"返回按钮";
        CGSize imageSize = leftBarButtonItem.image.size;
        
        [self.leftBarButtonItemView.itemImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.leftBarButtonItemView).offset(leftBarButtonItem.imageInsets.left);
            make.centerY.equalTo(self.leftBarButtonItemView).offset((leftBarButtonItem.imageInsets.top - leftBarButtonItem.imageInsets.bottom) / 2.0);
            make.size.equalTo(@(imageSize));
        }];
        [self.leftBarButtonItemView.itemTitleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.leftBarButtonItemView.itemImageView.mas_right).offset(leftBarButtonItem.titleInsets.left);
            make.centerY.equalTo(self.leftBarButtonItemView).offset((leftBarButtonItem.titleInsets.top - leftBarButtonItem.titleInsets.bottom) / 2.0);
        }];

        [self.titleItemView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.leftBarButtonItemView.mas_right).priority(350);
        }];
    }else {
        [_leftBarButtonItemView removeFromSuperview];
        _leftBarButtonItemView = nil;
    }
}

- (void)setRightBarButtonItem:(YMBarButtonItem *)rightBarButtonItem
{
    if (rightBarButtonItem) {
        _rightBarButtonItem = rightBarButtonItem;
        [_rightBarButtonItemView removeFromSuperview];
        _rightBarButtonItemView = nil;
        [self.KVOController observe:_rightBarButtonItem keyPath:@"buttonWidth" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld block:^(id  _Nullable observer, YMBarButtonItem  *_Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nonnull change) {
            [self.rightBarButtonItemView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(@(object.buttonWidth));
            }];
        }];
        self.rightBarButtonItemView.hidden = NO;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:rightBarButtonItem.target action:rightBarButtonItem.action];
        [self.rightBarButtonItemView addGestureRecognizer:tap];
        self.rightBarButtonItemView.itemImageView.image = rightBarButtonItem.image;
        self.rightBarButtonItemView.itemTitleLabel.text = rightBarButtonItem.title;
        self.rightBarButtonItemView.itemTitleLabel.textColor = rightBarButtonItem.titleColor;
        self.rightBarButtonItemView.itemTitleLabel.font = rightBarButtonItem.font ?: [UIFont systemFontOfSize:15.f];
        self.rightBarButtonItemView.itemTitleLabel.textAlignment = NSTextAlignmentRight;
        [self.rightBarButtonItemView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.bottom.equalTo(self);
            make.width.equalTo(@(_rightBarButtonItem.buttonWidth));
            make.height.equalTo(@(YMNavBarSubViewHeight));
        }];
        
        CGSize imageSize = rightBarButtonItem.image.size;
        if (imageSize.height > 24) {
            imageSize = CGSizeMake(imageSize.width / imageSize.height * 24, 24);
        }
        [self.rightBarButtonItemView.itemImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.rightBarButtonItemView.itemTitleLabel.mas_left).offset(- rightBarButtonItem.imageInsets.right);
            make.centerY.equalTo(self.rightBarButtonItemView).offset((rightBarButtonItem.imageInsets.top - rightBarButtonItem.imageInsets.bottom) / 2.0);
            make.size.equalTo(@(imageSize));
        }];
        if (imageSize.width > 0) {
            self.rightBarButtonItemView.itemTitleLabel.text = @"";
        }
        [self.rightBarButtonItemView.itemTitleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.rightBarButtonItemView.mas_right).offset(- rightBarButtonItem.titleInsets.right);
            make.centerY.equalTo(self.rightBarButtonItemView).offset((rightBarButtonItem.titleInsets.top - rightBarButtonItem.titleInsets.bottom) / 2.0);
        }];
    }else {
        [_rightBarButtonItemView removeFromSuperview];
        _rightBarButtonItemView = nil;
    }
}

- (void)setLeftQuitButtonItem:(YMBarButtonItem *)leftQuitButtonItem
{
    if (leftQuitButtonItem) {
        self.leftBarButtonItem.buttonWidth = 40;
        
        _leftQuitButtonItem = leftQuitButtonItem;
        [_leftQuitButtonItemView removeFromSuperview];
        _leftQuitButtonItemView = nil;
        [self.KVOController observe:_leftQuitButtonItem keyPath:@"buttonWidth" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld block:^(id  _Nullable observer, YMBarButtonItem  *_Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nonnull change) {
            [self.leftQuitButtonItemView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(@(object.buttonWidth));
            }];
        }];
        self.leftQuitButtonItemView.hidden = NO;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:leftQuitButtonItem.target action:leftQuitButtonItem.action];
        [self.leftQuitButtonItemView addGestureRecognizer:tap];
        self.leftQuitButtonItemView.itemImageView.image = leftQuitButtonItem.image;
        self.leftQuitButtonItemView.itemTitleLabel.text = leftQuitButtonItem.title;
        self.leftQuitButtonItemView.itemTitleLabel.textColor = leftQuitButtonItem.titleColor;
        self.leftQuitButtonItemView.itemTitleLabel.font = leftQuitButtonItem.font ?: [UIFont systemFontOfSize:15.f];
        self.leftQuitButtonItemView.itemTitleLabel.textAlignment = NSTextAlignmentLeft;
        [self.leftQuitButtonItemView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self);
            make.width.equalTo(@(40));
            make.height.equalTo(@(YMNavBarSubViewHeight));
            make.left.equalTo(self.leftBarButtonItemView.mas_right).offset(6);
        }];
        CGSize imageSize = leftQuitButtonItem.image.size;
        
        [self.leftQuitButtonItemView.itemImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.leftQuitButtonItemView).offset(leftQuitButtonItem.imageInsets.left);
            make.centerY.equalTo(self.leftBarButtonItemView).offset((leftQuitButtonItem.imageInsets.top - leftQuitButtonItem.imageInsets.bottom) / 2.0);
            make.size.equalTo(@(imageSize));
        }];
        [self.leftQuitButtonItemView.itemTitleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.leftQuitButtonItemView.itemImageView.mas_right).offset(leftQuitButtonItem.titleInsets.left);
            make.centerY.equalTo(self.leftBarButtonItemView).offset((leftQuitButtonItem.titleInsets.top - leftQuitButtonItem.titleInsets.bottom) / 2.0);
        }];
        [self.titleItemView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.leftQuitButtonItemView.mas_right).priority(350);
        }];
    }else {
        [_leftQuitButtonItemView removeFromSuperview];
        _leftQuitButtonItemView = nil;
    }
}

- (void)setTitleItem:(YMBarItem *)titleItem
{
    if (titleItem) {
        _titleItem = titleItem;
        self.titleItemView.hidden = NO;
        self.titleItemView.itemTitleLabel.text = titleItem.title;
        self.titleItemView.itemTitleLabel.textColor = titleItem.titleColor;
        self.titleItemView.itemTitleLabel.font = titleItem.font;
        CGFloat width = YMBarItemWidth;
        [self.titleItemView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.bottom.equalTo(self.mas_bottom);
            make.width.equalTo(@(kSCREEN_WIDTH - (width * 2.0) - 20)).priority(300);
            make.height.equalTo(@(YMNavBarSubViewHeight));
        }];
        [self.titleItemView.itemTitleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.titleItemView);
            make.top.bottom.equalTo(self.titleItemView);
            make.left.greaterThanOrEqualTo(self.mas_left).offset(50);
        }];
    }else {
        [_titleItemView removeFromSuperview];
        _titleItemView = nil;
    }
}

- (void)setBackgroundImage:(UIImage *)backgroundImage
{
    _backgroundImage = backgroundImage;
    if (_backgroundImage) {
        self.backgroundImageView.image = backgroundImage;
        self.backgroundImageView.hidden = NO;
    }else {
        self.backgroundImageView.hidden = YES;
    }
}

- (void)setSeparatorColor:(UIColor *)separatorColor
{
    _separatorColor = separatorColor;
    self.separatorView.backgroundColor = separatorColor;
}

- (void)setSeparatorHeight:(CGFloat)separatorHeight
{
    _separatorHeight = separatorHeight;
    [self.separatorView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(separatorHeight));
    }];
}

#pragma mark - lazy
- (YMBarItemView *)leftBarButtonItemView
{
    if (!_leftBarButtonItemView) {
        _leftBarButtonItemView = [[YMBarItemView alloc] initWithFrame:CGRectZero];
        _leftBarButtonItemView.backgroundColor = [UIColor clearColor];
        _leftBarButtonItemView.hidden = YES;
        [self addSubview:_leftBarButtonItemView];
    }
    return _leftBarButtonItemView;
}

- (YMBarItemView *)rightBarButtonItemView
{
    if (!_rightBarButtonItemView) {
        _rightBarButtonItemView = [[YMBarItemView alloc] initWithFrame:CGRectZero];
        _rightBarButtonItemView.backgroundColor = [UIColor clearColor];
        _rightBarButtonItemView.hidden = YES;
        [self addSubview:_rightBarButtonItemView];
    }
    return _rightBarButtonItemView;
}

- (YMBarItemView *)leftQuitButtonItemView
{
    if (!_leftQuitButtonItemView) {
        _leftQuitButtonItemView = [[YMBarItemView alloc] initWithFrame:CGRectZero];
        _leftQuitButtonItemView.backgroundColor = [UIColor clearColor];
        _leftQuitButtonItemView.hidden = YES;
        [self addSubview:_leftQuitButtonItemView];
    }
    return _leftQuitButtonItemView;
}

- (YMBarItemView *)titleItemView
{
    if (!_titleItemView) {
        _titleItemView = [[YMBarItemView alloc] initWithFrame:CGRectZero];
        _titleItemView.backgroundColor = [UIColor clearColor];
        _titleItemView.hidden = YES;
        [self addSubview:_titleItemView];
    }
    return _titleItemView;
}

- (UIView *)separatorView
{
    if (!_separatorView) {
        _separatorView = [[UIView alloc] initWithFrame:CGRectZero];
        _separatorView.backgroundColor = [UIColor clearColor];
        [self addSubview:_separatorView];
        [_separatorView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.equalTo(self);
            make.height.equalTo(@(1/[[UIScreen mainScreen] scale]));
        }];
    }
    return _separatorView;
}

- (UIImageView *)backgroundImageView
{
    if (!_backgroundImageView) {
        _backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _backgroundImageView.userInteractionEnabled = YES;
        _backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
        _backgroundImageView.hidden = YES;
        [self addSubview:_backgroundImageView];
    }
    return _backgroundImageView;
}

@end
