//
//  YMHomeNavView.m
//  PCDBank
//
//  Created by Gusont on 2021/2/7.
//  Copyright © 2021 YMBank. All rights reserved.
//

#import "SLCustNavView.h"
#import "SLMikeSegment.h"
#import "SLAddFriendVc.h"

static const NSInteger ymNavBtnTag = 666665;

static const NSInteger ymNavQuitBtnTag = 333333;

@implementation SLCustNavView

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        [self confignUI];
    }
    return self;
}

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

- (void)confignUI
{
    self.backgroundColor = [UIColor whiteColor];
    [self.infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.bottom.equalTo(self);
        make.height.mas_equalTo(44);
    }];
}

#pragma mark - 事件点击

- (void)tfTap
{

}

- (void)btnClick:(UIButton *)btn
{
    if (self.rightBtnBlock) {
        switch (btn.tag - ymNavBtnTag) {
            case YMNavBarAdd:
            {
                UIViewController *vc = self.viewController;
                //加一个蒙层
                __block UIView *bView = [[UIView alloc] initWithFrame:vc.view.frame];
                bView.backgroundColor = [UIColor clearColor];
                typeof(UIView *) __weak wkBView = bView;
                [bView addTapWithBlock:^{
                    [wkBView removeFromSuperview];
                }];
                [vc.view addSubview:bView];
                NSArray *arr = @[[SLMikeSegmentModel initWithSegTitle:@"发起群聊" segImage:@"icon_home_chats"],
                                 [SLMikeSegmentModel initWithSegTitle:@"添加朋友" segImage:@"icon_home_add_f"],
                                 [SLMikeSegmentModel initWithSegTitle:@"扫一扫  " segImage:@"icon_scan"]];
                
                CGFloat height = arr.count * 50;
                SLMikeSegment *mikeSegment = [[SLMikeSegment alloc] initWithFrame:CGRectMake(kSCREEN_WIDTH - 132, kNavBarHeight + 6, 120, height) titleArray:arr isHor:YES];
                mikeSegment.backgroundColor = [UIColor clearColor];
                WS(weakSelf);
                mikeSegment.mikeSegmentBlock = ^(SLMikeSegmentModel *model) {
                    [bView removeFromSuperview];
                    if ([model.segTitle isEqualToString:@"添加朋友"]) {
                        SLAddFriendVc *vc = [SLAddFriendVc loadViewControllewWithNib];
                        [weakSelf.viewController navPushViewController:vc animated:YES];;
                    }
                    
                };
                [bView addSubview:mikeSegment];
                UIImageView *imgView = [[UIImageView alloc] initWithImage:ImageName(@"icon_arrow_wrapper")];
                [bView addSubview:imgView];
                [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.equalTo(mikeSegment.mas_right);
                    make.bottom.equalTo(mikeSegment.mas_top);
                    make.width.mas_equalTo(42);
                    make.height.mas_equalTo(8);
                }];
            }
                break;
            case YMNavBarSearch:
            {
                
            }
                break;
                
            default:
                break;
        }
    }
//    if (self.details.count > btn.tag - ymNavBtnTag) {
//        if (self.rightBtnBlock) {
//            self.rightBtnBlock( btn.tag - ymNavBtnTag);
//        }
//    }
}

#pragma mark - 导航栏设置
/// 导航栏配置
/// @param details <#details description#>
- (void)confignNavViewWith:(NSArray<SLPageModelDetail *> *)details
{
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:UIButton.class]) {
            [obj removeFromSuperview];
        }
    }];
    self.infoLabel.hidden = NO;
    self.details = details;
    [self setNavBtnListWith:details];
    
}

/// 导航栏按钮配置
/// @param details <#details description#>
- (void)setNavBtnListWith:(NSArray<SLPageModelDetail *> *)details
{
    [details enumerateObjectsUsingBlock:^(SLPageModelDetail * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = ymNavBtnTag + idx;
        [btn setImage:ImageName(obj.bgImage) forState:UIControlStateNormal];
        [self addSubview:btn];
        [btn setSelected:NO];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_right).offset(-12 * KScaleW - 41 * idx);
            make.centerY.equalTo(self.infoLabel);
            make.width.equalTo(@(40));
            make.height.equalTo(@(40));
        }];
        
    }];
}

-(UIImage *)scaleImage:(UIImage *)image scale:(CGFloat)scale{
    NSData *imageData = UIImagePNGRepresentation(image);
    UIImage *imageNew = [UIImage imageWithData:imageData scale:scale];
    return imageNew;
}

#pragma mark - instantiate
- (UILabel *)infoLabel
{
    if (!_infoLabel) {
        _infoLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _infoLabel.textColor = SL333Color;
        _infoLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:17.0];
        _infoLabel.text = @"标题";
        [self addSubview:_infoLabel];
    }
    return _infoLabel;
}

- (LeftImageDesignTxtxField *)searchTextField
{
    if (!_searchTextField) {
        _searchTextField = [[LeftImageDesignTxtxField alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
        _searchTextField.placeholder = @"请输入搜索内容";
//        _searchTextField.layer.borderColor = [YMBorderGrey CGColor];
        _searchTextField.layer.borderWidth = 1;
        _searchTextField.layer.cornerRadius = 16.0;
        _searchTextField.font = [UIFont systemFontOfSize:14];
        _searchTextField.clipsToBounds = YES;
        _searchTextField.leftWithImage = [UIImage imageNamed:@"icon3_search"];
//        _searchTextField.enabled = NO;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tfTap)];
        [_searchTextField addGestureRecognizer:tap];
        [self addSubview:_searchTextField];
    }
    return _searchTextField;
}

@end
