//
//  HearStepVC.m
//  gaotingshan
//
//  Created by MacBook Dq01 on 16/4/28.
//  Copyright © 2016年 Xu Mengzhu. All rights reserved.
//心率计步页面，只有心率和步数

#import "HearStepVC.h"
#import "Singleton.h"
#import "SmartWatchesViC.h"


#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define KMargin 10
#define kHeight 30

@interface HearStepVC () <UIWebViewDelegate> {
    Singleton *singleton;
}

@property (nonatomic, strong) UIWebView *stepWebView;
@property (nonatomic, strong) UIWebView *heartWebView;

@property (nonatomic, strong) UISegmentedControl *segmentedControl;


@end

@implementation HearStepVC

//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//    self.view.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
//    CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI);
//    [self.view setTransform:transform];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
       
    singleton = [Singleton defaultSingleton];
    
    self.navigationItem.title = @"心率计步";
    //改变字体颜色
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    self.navigationController.navigationBar.titleTextAttributes = textAttrs;
    //修改2016-6－14
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bg800"] forBarMetrics:(UIBarMetricsDefault)];
    self.view.backgroundColor = [UIColor whiteColor];
#pragma - 设置导航栏左边的按钮
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 6, 30, 30);
    [leftBtn addTarget:self action:@selector(hearLeftBarButtonItemIsClicked) forControlEvents:(UIControlEventTouchUpInside)];
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"nav_back"] forState:(UIControlStateNormal)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImage *heart_no = [UIImage imageNamed:@"xinlv_yes"];
    heart_no = [heart_no imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *step_no = [UIImage imageNamed:@"jibu_no"];
    step_no = [step_no imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    NSArray *titleArray = @[heart_no, step_no];
//    NSArray *titleArray = @[@"心率图表", @"计步图表"];
    
    _segmentedControl = [[UISegmentedControl alloc] initWithItems:titleArray];
    //修改2016-6－14
    _segmentedControl.frame = CGRectMake(2*KMargin, KMargin, kScreenHeight - 4*KMargin, kHeight);
    //_segmentedControl.selectedSegmentIndex = 0;
    //修改选中颜色
    _segmentedControl.tintColor = [UIColor whiteColor];
    [_segmentedControl addTarget:self action:@selector(segmentedControlIsClicked:) forControlEvents:(UIControlEventValueChanged)];
    [self.view addSubview:_segmentedControl];
    
    _heartWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_segmentedControl.frame) + 2 * KMargin, kScreenHeight, kScreenWidth)];
    [_heartWebView setUserInteractionEnabled:YES];
    _heartWebView.delegate = self;
    [_heartWebView setOpaque:NO]; //设置是否透明。
    [_heartWebView setScalesPageToFit:YES]; //自动缩放以适应屏幕。
    _heartWebView.backgroundColor = [UIColor whiteColor];
    //加载网页
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/terminal/bracelet_heartBeat_report?sesskey=%@",localRequestUrl, singleton.sesskey]];
    [_heartWebView loadRequest:[NSURLRequest requestWithURL:url]];
    [self.view addSubview:_heartWebView];
    

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rePrompt) name:@"rePrompt" object:nil];
    
}


#pragma mark - UISegmentedControl按钮被点击触发的方法
- (void)segmentedControlIsClicked:(UISegmentedControl *)sender {
    if (_segmentedControl.selectedSegmentIndex == 0) {
        
        [_segmentedControl setImage:[[UIImage imageNamed:@"xinlv_yes"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:0];
        [_segmentedControl setImage:[[UIImage imageNamed:@"jibu_no"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:1];
        
        [_stepWebView removeFromSuperview];
        //以下是对webView的一些处理。
        //修改2016-6－14
        _heartWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_segmentedControl.frame) + 2 * KMargin, kScreenHeight, kScreenWidth)];
        [_heartWebView setUserInteractionEnabled:YES];
        _heartWebView.delegate = self;
        [_heartWebView setOpaque:NO]; //设置是否透明。
        [_heartWebView setScalesPageToFit:YES]; //自动缩放以适应屏幕。
        _heartWebView.backgroundColor = [UIColor whiteColor];
        //加载网页
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/terminal/bracelet_heartBeat_report?sesskey=%@",localRequestUrl, singleton.sesskey]];
        [_heartWebView loadRequest:[NSURLRequest requestWithURL:url]];
        [self.view addSubview:_heartWebView];
        
    } else if (_segmentedControl.selectedSegmentIndex == 1) {
        [_segmentedControl setImage:[[UIImage imageNamed:@"xinlv_no"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:0];
        [_segmentedControl setImage:[[UIImage imageNamed:@"jibu_yes"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:1];
        
        [_heartWebView removeFromSuperview];
        //以下是对webView的一些处理。
        //修改2016-6－14
        _stepWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_segmentedControl.frame) + 2 * KMargin, kScreenHeight, kScreenWidth)];
        [_stepWebView setUserInteractionEnabled:YES];
        _stepWebView.delegate = self;
        [_stepWebView setOpaque:NO]; //设置是否透明。
        [_stepWebView setScalesPageToFit:YES]; //自动缩放以适应屏幕。
        _stepWebView.backgroundColor = [UIColor whiteColor];
        //加载网页
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/terminal/bracelet_stepCount_report?sesskey=%@",localRequestUrl, singleton.sesskey]];
        [_stepWebView loadRequest:[NSURLRequest requestWithURL:url]];
        
        [self.view addSubview:_stepWebView];
    }
}

- (void)hearLeftBarButtonItemIsClicked {
  [self dismissViewControllerAnimated:YES completion:nil];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
}

#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    NSLog(@"呵呵：%@", request.URL.absoluteString);
    
    if ([request.URL.absoluteString containsString:@"/error?err=13"]) {
        //弹出提示框
        UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"系统提示" message:@"暂无绑定手表，去绑定智能手表" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            //跳到绑定智能手表页面
            SmartWatchesViC *smartVc = [[SmartWatchesViC alloc] init];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:smartVc];
            [self presentViewController:nav animated:YES completion:nil];
            
        }];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
            [self dismissViewControllerAnimated:YES completion:nil];
            
        }];
        
        [alertVc addAction:sureAction];
        [alertVc addAction:cancelAction];
        
        [self presentViewController:alertVc animated:YES completion:nil];
        return NO;
    }
    
    return YES;
    
}



#pragma mark - 接收消息（来自于“SmartWatchViC”页面）
- (void)rePrompt {
    //弹出提示框
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"系统提示" message:@"暂无绑定手表，去绑定智能手表" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        //跳到绑定智能手表页面
        SmartWatchesViC *smartVc = [[SmartWatchesViC alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:smartVc];
        [self presentViewController:nav animated:YES completion:nil];
        
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }];
    
    [alertVc addAction:sureAction];
    [alertVc addAction:cancelAction];
    
    [self presentViewController:alertVc animated:YES completion:nil];
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(BOOL)shouldAutorotate
{
    return YES;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

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

/*
#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
