//
//  GMFaceShotBottomView.m
//  Gengmei
//
//  Created by 刘鹿杰 on 2019/8/20.
//  Copyright © 2019 更美互动信息科技有限公司. All rights reserved.
//

#import "GMFaceShotBottomView.h"
#import <AudioToolbox/AudioToolbox.h>
@import GMFoundation;
#define BtnWidth 65


@interface ALDotNotificationView : GMView

@property (nonatomic, strong) GMView *dotView;

@end

@implementation ALDotNotificationView

static CGFloat kRedDotViewWidth = 3;

- (void)setup {
    [super setup];
    [self addSubview:self.dotView];
}

- (void)showWithSuperView:(UIView *)superView top:(CGFloat)top left:(CGFloat)left {
    [superView addSubview:self];
    self.frame = CGRectMake(left, top, kRedDotViewWidth, kRedDotViewWidth);
}

- (void)hide {
    [self removeFromSuperview];
}

- (GMView *)dotView {
    if (!_dotView) {
        _dotView = [[GMView alloc] init];
        _dotView.backgroundColor = UIColor.auxiliaryTextGreen;
        _dotView.layer.cornerRadius = kRedDotViewWidth / 2.0;
        _dotView.layer.masksToBounds = YES;
        [self addSubview:_dotView];
        [_dotView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
    return _dotView;
}
@end

@interface GMFaceShotBottomView()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) GMView *container;
@property (nonatomic, strong) ALDotNotificationView *dotView;

@end

@implementation GMFaceShotBottomView

static NSString *kShowedSkinDotView = @"kShowedSkinDotView";

-(void)setup {
    [super setup];
    _scrollView = [[UIScrollView alloc] init];
    _scrollView.userInteractionEnabled = YES;
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.contentSize = CGSizeMake(BtnWidth * 5 + 1, 0);
    _scrollView.contentInset = UIEdgeInsetsMake(0, BtnWidth, 0, BtnWidth);
//    _scrollView.contentOffset = CGPointMake(-BtnWidth, 0);
    _scrollView.delegate = self;
    [self addSubview:_scrollView];
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    
    NSArray *array = @[@"颜值分析", @"肤质分析", @"面孔起源"];
    NSString *lastShowDate = [[NSUserDefaults standardUserDefaults] objectForKey:kShowedSkinDotView];
    NSString *currentDate = [[NSDate date] dateFormatToString];
    
    for (int i = 0; i < array.count ; i++ ) {
        GMButton *btn = [[GMButton alloc] init];
        [btn.titleLabel setFont:[UIFont gmBoldFont:12]];
        [btn setTitle:array[i] forState:UIControlStateNormal];
        [btn setTitleColor:UIColor.headlineText forState:UIControlStateNormal];
        [btn setTitleColor:UIColor.auxiliaryTextGreen forState:UIControlStateSelected];
        if (i == 0) {
            btn.selected = YES;
            [btn.titleLabel setFont:[UIFont gmBoldFont:12]];
        } else {
            [btn.titleLabel setFont:[UIFont gmFont:12]];
        }
        btn.tag = i;
        [btn addTarget:self action:@selector(btnCLick:) forControlEvents:UIControlEventTouchUpInside];
        [_scrollView addSubview:btn];
        if (i == 1 && (!lastShowDate || ![currentDate isEqualToString:lastShowDate])) {
            _dotView = [[ALDotNotificationView alloc] init];
            [_dotView showWithSuperView:btn top:6 left:BtnWidth - 7];
            [[NSUserDefaults standardUserDefaults] setObject:currentDate forKey:kShowedSkinDotView];
        }
        [btn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(BtnWidth * i);
            make.size.mas_equalTo(CGSizeMake(BtnWidth, 30));
            make.centerY.mas_equalTo(self.mas_centerY);
        }];
    }
}

-(void)setSelectedType:(GMScanAnimationType)selectedType{
    _selectedType = selectedType;
    self.scrollView.contentOffset = CGPointMake(BtnWidth * (selectedType - 2), 0);
    [self selectedBtnWithTag:selectedType];
    if (selectedType == GMScanAnimationTypeScanSkin) {
        [_dotView hide];
    }
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
//    debugLog(@"=========targetContentOffset%.2f", targetContentOffset->x)
#warning 待修改
    if (targetContentOffset->x < 0) { // 选中第一个按钮
        [self btnChage:0];
    } else if (targetContentOffset->x >= 0 && targetContentOffset->x < BtnWidth) {  // 选中第二个按钮
        [self btnChage:1];
    } else {
        [self btnChage:2];
    }
}

-(void)btnCLick:(GMButton *)sender{
    if (sender.selected) {
        return;
    }
    if (sender.tag == 0) {
        [self.scrollView setContentOffset:CGPointMake(-BtnWidth * 2, 0) animated:YES];
    } else if(sender.tag == 1){
        [_dotView hide];
        [self.scrollView setContentOffset:CGPointMake(-BtnWidth, 0) animated:YES];
    } else if(sender.tag == 2){
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    [self btnChage:sender.tag];
}

- (void)selectedBtnWithTag:(NSInteger)tag{
    for (GMButton *btn in _scrollView.subviews) {
        if ([btn isKindOfClass:[GMButton class]]) {
            btn.selected = btn.tag == tag;
            [btn.titleLabel setFont:btn.tag == tag ? [UIFont gmBoldFont:12] : [UIFont gmFont:12]];
        }
    }
}

// 改变按钮选中状态
-(void)btnChage:(NSInteger)tag {
    AudioServicesPlaySystemSound(1519);
    [self selectedBtnWithTag:tag];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.delegate && [self.delegate respondsToSelector:@selector(faceShotBottomCellDidSelected:)]) {
            [self.delegate faceShotBottomCellDidSelected:tag];
            NSString *tabName = @"";
            if (tag == 0) {
                tabName = @"AI测脸";
            } else if (tag == 1) {
                tabName = @"AI测肤";
            } else if (tag == 2) {
                tabName = @"AI面孔";
            }
            [Phobos track:@"on_click_tab" attributes:@{@"page_name": @"face_scan", @"position": @(tag + 1),@"tab_name": tabName}];
        }
    });
}

@end
