//
//  IntroduceViewController.m
//  gaotingshan
//
//  Created by Mac Book DQ02 on 16/3/12.
//  Copyright © 2016年 Mac Book DQ02. All rights reserved.
//

#import "IntroduceViewController.h"
#import "LeftDrawerViewController.h"
#import "UIView+UIView_Extension.h"
#import "CustomTabBarController.h"

#define HWNewfeatureCount 4


@interface IntroduceViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) MMDrawerController *drawerController;

@end

@implementation IntroduceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1.创建一个scrollView：显示所有的新特性图片
    self.view = [[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.view.backgroundColor = [UIColor clearColor];
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    
    // 2.添加图片到scrollView中
    CGFloat scrollW = self.scrollView.width;
    CGFloat scrollH = self.scrollView.height;
    for (int i = 0; i < HWNewfeatureCount; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.width = scrollW;
        imageView.height = scrollH;
        imageView.y = 0;
        imageView.x = i * scrollW;
        // 显示图片
        NSString *name = [NSString stringWithFormat:@"app_start_%d.jpg", i + 1];
        imageView.image = [UIImage imageNamed:name];
        [self.scrollView addSubview:imageView];
        // 如果是第一张imageView，就往里面添加其他内容
//        if (i == HWNewfeatureCount - 4) {
//            [self setupFirstImageView:imageView];
//        }
        
        // 如果是最后一个imageView，就往里面添加其他内容
        if (i == HWNewfeatureCount - 1) {
            [self setupLastImageView:imageView];
        }
        else
        {
            [self setupFirstImageView:imageView];
        }
    }

    // 3.设置scrollView的其他属性
    // 如果想要某个方向上不能滚动，那么这个方向对应的尺寸数值传0即可
    self.scrollView.contentSize = CGSizeMake(HWNewfeatureCount * scrollW, 0);
    self.scrollView.bounces = NO; // 去除弹簧效果
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    
    // 4.添加pageControl：分页，展示目前看的是第几页, 在这里不需要
//    UIPageControl *pageControl = [[UIPageControl alloc] init];
//    pageControl.numberOfPages = HWNewfeatureCount;
//    pageControl.backgroundColor = [UIColor redColor];
//    pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:253 green:98 blue:42 alpha:1.0];
//    pageControl.pageIndicatorTintColor = [UIColor colorWithRed:189 green:189 blue:189 alpha:1.0];
//    pageControl.centerX = scrollW * 0.5;
//    pageControl.centerY = scrollH - 50;
//    [self.view addSubview:pageControl];
//    self.pageControl = pageControl;
    
    
}

/**
 *  初始化第一个imageView
 *
 *  @param imageView 第一个imageView
 */

- (void)setupFirstImageView:(UIImageView *)imageView {
    // 开启交互功
    imageView.userInteractionEnabled = YES;
    // 1.立即体验按钮（checkbox）
    UIButton *skipBtn = [[UIButton alloc] init];
    [skipBtn setImage:[UIImage imageNamed:@"skip"] forState:UIControlStateNormal];
    skipBtn.tag = 20001;
    skipBtn.width = 100;
    skipBtn.height = 40;
    skipBtn.centerX = imageView.width * 0.8;
    skipBtn.centerY = imageView.height * 0.1;
    [skipBtn addTarget:self action:@selector(startBtnIsClick) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:skipBtn];
    
}

/**
 *  初始化最后一个imageView
 *
 *  @param imageView 最后一个imageView
 */
- (void)setupLastImageView:(UIImageView *)imageView
{
    // 开启交互功能
    imageView.userInteractionEnabled = YES;
    // 1.立即体验按钮（checkbox）
    UIButton *startBtn = [[UIButton alloc] init];
    [startBtn setImage:[UIImage imageNamed:@"startnow"] forState:UIControlStateNormal];
    startBtn.width = 100;
    startBtn.height = 28;
    startBtn.centerX = imageView.width * 0.5;
    startBtn.centerY = imageView.height * 0.86;
    [startBtn addTarget:self action:@selector(startBtnIsClick) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:startBtn];
    // EdgeInsets: 自切
    // contentEdgeInsets:会影响按钮内部的所有内容（里面的imageView和titleLabel）
    //    shareBtn.contentEdgeInsets = UIEdgeInsetsMake(10, 100, 0, 0);
    
    // titleEdgeInsets:只影响按钮内部的titleLabel
//    startBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    
    // imageEdgeInsets:只影响按钮内部的imageView
    //    shareBtn.imageEdgeInsets = UIEdgeInsetsMake(20, 0, 0, 50);
    
    //    shareBtn.titleEdgeInsets
    //    shareBtn.imageEdgeInsets
    //    shareBtn.contentEdgeInsets
    
}

- (void)startBtnIsClick {
    
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [delegate setMMDrawerController];
  /*
    LeftDrawerViewController *lc = [[LeftDrawerViewController alloc]init];
    CustomTabBarController *tabarVC= [[CustomTabBarController alloc]init];
    _drawerController = [[MMDrawerController alloc]initWithCenterViewController:tabarVC leftDrawerViewController:lc];
    [self.drawerController setShowsShadow:YES];
    
#pragma mark - set the width of leftView
    [self.drawerController setMaximumLeftDrawerWidth:self.view.frame.size.width*0.75];
#pragma mark - gesture 左右滑动，三个页面间切换
    [self.drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    [self.drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = _drawerController;
  */  
    // 切换到HWTabBarController
    /*
     切换控制器的手段
     1.push：依赖于UINavigationController，控制器的切换是可逆的，比如A切换到B，B又可以回到A
     2.modal：控制器的切换是可逆的，比如A切换到B，B又可以回到A
     3.切换window的rootViewController
     */
    //    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    //    window.rootViewController = [[HWTabBarViewController alloc] init];
    
    // modal方式，不建议采取：新特性控制器不会销毁
    //    HWTabBarViewController *main = [[HWTabBarViewController alloc] init];
    //    [self presentViewController:main animated:YES completion:nil];
}


#pragma mark - UIScrollViewDelegate Motheds
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    double page = scrollView.contentOffset.x / scrollView.width;
    // 四舍五入计算出页码
//    self.pageControl.currentPage = (int)(page + 0.5);
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int index=fabs(scrollView.contentOffset.x) / (scrollView.frame.size.width);
    _myPageCtr.currentPage=index;
}*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
