//
//  MFPageContentView.m
//  KKCivilAd
//
//  Created by 雷俊博 on 2019/11/30.
//  Copyright © 2019 雷俊博. All rights reserved.
//

#import "MFPageContentView.h"
#import "UIView+frameAdjust.h"

@interface MFPageContentView ()<UIScrollViewDelegate>
// 外界父控制器
@property (nonatomic, strong)UIViewController* parentViewController;
//存储子控制器
@property (nonatomic, strong)NSMutableArray<UIViewController *>* childViewControllers;
//记录加载的上一个控制器
@property (nonatomic, strong)UIViewController* lastVC;
//记录刚开始时的偏移量
@property (nonatomic, assign)CGFloat startOffsetX;

@property (nonatomic, strong)UIScrollView *scrollView;

@end

@implementation MFPageContentView

#pragma mark - life cycle
- (instancetype)initWithFrame:(CGRect)frame parentVC:(UIViewController *)parentVC childVCs:(NSMutableArray *)childVCs
{
    self = [super initWithFrame:frame];
    if (self) {
        self.parentViewController = parentVC;
        self.childViewControllers = childVCs;
        [self setupSubViews];
    }
    return self;
}

- (void)setupSubViews
{
    //处理偏移量
    UIView *tempView = [[UIView alloc]initWithFrame:CGRectZero];
    [self addSubview:tempView];
    [self addSubview:self.scrollView];
}


#pragma mark - public methods
//给外界用以获取MFPageTitleView选中按钮的下标
- (void)setPageContentViewCurrentIndex:(NSInteger)currentIndex
{
    if (self.lastVC) {
        [self.lastVC beginAppearanceTransition:NO animated:NO];
        [self.lastVC endAppearanceTransition];
    }
    
    CGFloat offsetX = currentIndex * self.width;
    
    //添加子控制器以及子控制器的 view
    UIViewController *childVC = self.childViewControllers[currentIndex];
    [self.parentViewController addChildViewController:childVC];
    [childVC beginAppearanceTransition:YES animated:YES];
    [self.scrollView addSubview:childVC.view];
    [childVC endAppearanceTransition];
    childVC.view.frame = CGRectMake(offsetX, 0, self.width, self.height);
    
    //记录上个展示的子控制器
    self.lastVC = childVC;
    
    //处理内容偏移
    self.scrollView.contentOffset = CGPointMake(offsetX, 0);
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.startOffsetX = scrollView.contentOffset.x;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.lastVC) {
        [self.lastVC beginAppearanceTransition:NO animated:NO];
        [self.lastVC endAppearanceTransition];
    }
    
    CGFloat offsetX = scrollView.contentOffset.x;
    int index =(int)(offsetX/scrollView.frame.size.width);
    UIViewController *childVC = self.childViewControllers[index];
    [self.parentViewController addChildViewController:childVC];
    [childVC beginAppearanceTransition:YES animated:YES];

    [self.scrollView addSubview: childVC.view];
    
    //记录上个展示的子控制器
    self.lastVC = childVC;
    childVC.view.frame = CGRectMake(offsetX, 0, self.width, self.height);
    
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat progress = 0;
    NSInteger originalIndex = 0;
    NSInteger targetIndex = 0;
    //判断是左滑还是右滑
    CGFloat currentOffsetX = self.scrollView.contentOffset.x;
    CGFloat scrollViewW = self.scrollView.width;
    if (currentOffsetX > self.startOffsetX) {//左滑
        progress = currentOffsetX / scrollViewW - floor(currentOffsetX / scrollViewW);
        originalIndex = (NSInteger)(currentOffsetX / scrollViewW);
        targetIndex = originalIndex + 1;
        if (targetIndex >= self.childViewControllers.count) {
            progress = 1;
            targetIndex = self.childViewControllers.count - 1;
        }
        //如果完全划过去
        if ((currentOffsetX - self.startOffsetX) == scrollViewW) {
            progress = 1;
            targetIndex = originalIndex;
        }
    }else {//右滑
        progress = 1 - (currentOffsetX / scrollViewW - floor(currentOffsetX / scrollViewW));
        targetIndex = (NSInteger)(currentOffsetX / scrollViewW);
        originalIndex = targetIndex + 1;
        if (originalIndex >= self.childViewControllers.count) {
            originalIndex = self.childViewControllers.count - 1;
        }
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(pageContentViewScroll:originalIndex:targetIndex:)]) {
        [self.delegate pageContentViewScroll:progress originalIndex:originalIndex targetIndex:targetIndex];
    }
    
}


#pragma mark - getters and setters
- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
        _scrollView.delegate = self;
        _scrollView.bounces = NO;
        [_scrollView setPagingEnabled:YES];
        _scrollView.showsVerticalScrollIndicator = false;
        _scrollView.showsHorizontalScrollIndicator = false;
        _scrollView.contentSize = CGSizeMake(self.childViewControllers.count * self.width, 0);
    }
    return _scrollView;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
