//
//  XLJNewFetureController.m
//  NewFeatureDemo
//
//  Created by m on 2016/10/11.
//  Copyright © 2016年 XLJ. All rights reserved.
//

#import "XLJNewFetureController.h"
#import "XLJCollectionViewCell.h"
#import "PrivateAlertService.h"
#import "KKPrivateAlertView.h"

static NSString * const reuseIdentifier = @"CellIdentfifier";

@interface XLJNewFetureController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) NSMutableArray *mArray;

@property (nonatomic, strong) UIViewController *homeViewController;
@property (nonatomic, strong) UIPageControl *pageControl;

@property (nonatomic, strong) UICollectionView *collectionView;

//流水布局
@property (nonatomic, strong) UICollectionViewLayout *flowLayout;
/**按钮的位置0.87*/
@property (nonatomic, assign) CGFloat starHeightButton;
@property (nonatomic, assign) CGSize buttonSize;
@property (nonatomic, copy) NSString *buttonTitle;
@property (nonatomic, copy) NSString *buttonImage;
@property (nonatomic, strong) UIColor *titleColor;

@end

@implementation XLJNewFetureController

- (NSMutableArray *)mArray
{
    if (!_mArray) {
        _mArray = @[].mutableCopy;
    }
    return _mArray;
}

- (UIPageControl *)pageControl{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc] init];
        _pageControl.centerX = self.view.frame.size.width * 0.5;
        _pageControl.centerY = self.view.frame.size.height - 120;
        _pageControl.userInteractionEnabled = NO;
        _pageControl.hidden =YES;
    }
    
    return _pageControl;
}

- (UIViewController *)homeViewController
{
    if (!_homeViewController) {
        _homeViewController = [[UIViewController alloc] init];
    }
    return _homeViewController;
}

- (UICollectionViewLayout *)flowLayout
{
    if (!_flowLayout) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        
        //设置cell的尺寸
        flowLayout.itemSize = [UIScreen mainScreen].bounds.size;
        //清空行距
        flowLayout.minimumLineSpacing = 0;
        //设置滚动方向
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _flowLayout = flowLayout;
        
    }
    
    return _flowLayout;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:self.flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}
#pragma mark -- 重新赋值
- (instancetype)initWithNSArray:(NSMutableArray *)array withButton:(XLJExButton *)startButton withButtonHeight:(CGFloat)startHeight withViewController:(UIViewController *)controller
{
    if (self = [super init]) {
        self.mArray = array;
        
        self.starHeightButton = startHeight;
        self.homeViewController = controller;
    }
    
    return self;
}

- (instancetype)initWithNSArray:(NSMutableArray *)array withButtonSize:(CGSize)buttonSize withButtonTitle:(NSString *)buttonTitle withButtonImage:(NSString *)imageName withButtonTitleColor:(UIColor *)titleColor withButtonHeight:(CGFloat)startHeight withViewController:(UIViewController *)controller
{
    if (self = [super init]) {
        self.mArray = array;
        self.buttonSize = buttonSize;
        self.buttonImage = imageName;
        self.buttonTitle = buttonTitle;
        self.starHeightButton = startHeight;

        self.homeViewController = controller;
        self.titleColor = titleColor;
    }
    return self;
}

- (instancetype)initWithNSArray:(NSMutableArray *)array withButton:(XLJExButton *)startButton
{
    if (self = [super init]) {
        self.mArray = array;
    }
    
    return self;
}
- (instancetype)initWithNSArray:(NSMutableArray *)array
{
    if (self = [super init]) {
        self.mArray = array;
    }
    return self;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    //加载子视图
    [self initilizationSub];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    
    [self.navigationController.navigationBar navBarBackGroundColor:K_MainColor image:nil isOpaque:YES];//颜色
       
    [self.navigationController.navigationBar navBarBottomLineHidden:YES];
}

- (void)initilizationSub
{
    [self.view addSubview:self.collectionView];
    self.collectionView.delegate = self;
    //注册cell
    [self.collectionView registerClass:[XLJCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

    //分页显示
    self.collectionView.pagingEnabled = YES;
    //弹黄效果
    self.collectionView.bounces = NO;
    //不显示滚动条
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    
    //pageControl
    [self.view addSubview:self.pageControl];
    self.pageControl.numberOfPages = self.mArray.count;
}

#pragma mark -- UICollectionViewDataSource
//组数，可以不写默认为1
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每组有几个cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.mArray.count;

}

//设置每个cell的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //1.首先从缓冲池中取cell
    //2.检查当前是否注册cell,如果注册cell,就会帮你创建cell
    //3.如果没有注册，就注册
    XLJCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    NSIndexPath *indexPathStr = self.mArray[indexPath.row];
    
    NSString *imageName = [NSString stringWithFormat:@"%@",indexPathStr];
    cell.image = [UIImage imageNamed:imageName];
    cell.startHeight = self.starHeightButton;
    cell.toController = self.homeViewController;
    cell.imageNameStr = self.buttonImage;
    cell.startButton.XLJLable.text = self.buttonTitle;
    cell.startButton.XLJLable.textColor = self.titleColor;
    cell.startButton.size = self.buttonSize;
    
    //判断是否是最后一页
    [cell setCurrentPageIndex:indexPath.row lastPageIndex:self.mArray.count -1];

    return cell;
}

#pragma mark -- Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.pageControl.currentPage = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    if (self.pageControl.currentPage == 2){
        if (![[NSUserDefaults standardUserDefaults]boolForKey:@"haveAgreement"]) {
            /*
            [KKPrivateAlertView showWithBlock:^(NSInteger num) {
               
                if (num == 200) {
                     NSLog(@"同意");
                    //[self jumpToHTML5:@"http://kefu.weface.com.cn/ys/index1.html" andJAnalytics:@"隐私协议"];
                     [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"haveAgreement"];
                 }else if (num == 201){
                     NSLog(@"不同意");
                     [self exitApplication];
                 }else if (num == 203 ){
                     
                     NSLog(@"用户协议");
                     
                     NSString *urlString = @"http://nginx.weface.com.cn/agreement/kkshAgreement.html";
                     
                     [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];
                     
                     //[self jumpToHTML5:@"http://nginx.weface.com.cn/agreement/kkshAgreement.html" andJAnalytics:@"用户协议"];
                 }else if ( num == 204){
                     NSLog(@"用户协议");
                     
                     NSString *urlString = @"http://kefu.weface.com.cn/ys/index1.html";
                     
                     [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];
                     
                     //[self jumpToHTML5:@"http://kefu.weface.com.cn/ys/index1.html" andJAnalytics:@"隐私协议"];
                 }
                
            }];
            */
            
            /*
             NSArray* contents = @[@"感谢您信任并使用看看生活APP",@"在您使用我们的产品/服务时，需要连接移动网络或者WLAN，产生的流量费用请咨询当地运营商。",@"看看生活为了更好地保护您的隐私和信息安全,根据国家相关法律规定定制了《看看生活用户协议》、《看看生活隐私协议》，请您在使用前务必仔细阅读并透底理解，您同意并接受全部条款后开始使用我们的产品/服务。"];
                     
                     [[PrivateAlertService shareInstance]showWithContents:contents inViewController:self Callback:^(NSInteger index) {
                        if (index == 200) {
                             NSLog(@"同意");
                            [self jumpToHTML5:@"http://kefu.weface.com.cn/ys/index1.html" andJAnalytics:@"隐私协议"];
                             [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"haveAgreement"];
                         }else if (index == 201){
                             NSLog(@"不同意");
                             [self exitApplication];
                         }else if (index == 203 ){
                             NSLog(@"用户协议");
                             [self jumpToHTML5:@"http:nginx.weface.com.cn/agreement/kkshAgreement.html" andJAnalytics:@"用户协议"];
                         }else if ( index == 204){
                             NSLog(@"用户协议");
                             [self jumpToHTML5:@"http:kefu.weface.com.cn/ys/index1.html" andJAnalytics:@"隐私协议"];
                         }
                     }];
             */
        }
        
    }
    
}

- (void)exitApplication{
    exit(0);
}

-(void)jumpToHTML5:(NSString*)url andJAnalytics:(NSString*)janalytics
{
    WebViewController *tc=[WebViewController new];
    tc.URL =[NSURL URLWithString:url];
    tc.hidesBottomBarWhenPushed =YES;
    tc.janalytics = janalytics;
    [self.navigationController pushViewController:tc animated:YES];

}


@end



