//
//  AddVirtualEventVC.m
//  gaotingshan
//
//  Created by MacBook Dq01 on 16/3/24.
//  Copyright © 2016年 Mac Book DQ02. All rights reserved.
//

#import "AddVirtualEventVC.h"
#import "Singleton.h"
#import "BindingDeviceVC.h"
#import "MyCompeteScheduleVC.h"


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

@property (nonatomic, strong) UIWebView *webView;

@end

@implementation AddVirtualEventVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //创建单例实例
    singleton = [Singleton defaultSingleton];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"添加虚拟赛事";
    
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bg"] forBarMetrics:(UIBarMetricsDefault)];
//    //改变字体颜色
//    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
//    textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
//    self.navigationController.navigationBar.titleTextAttributes = textAttrs;

     [Manager setNavi:self.navigationController.navigationBar];
    
    
#pragma mark --设置导航浪左边按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"nav_back"] landscapeImagePhone:nil style:UIBarButtonItemStylePlain target:self action:@selector(leftBarBtnIsClicked)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    //加载webView
    [self LoadWebView];
    
    //添加观察者， 接收来自BindDeviceVC页面的值
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadWebView:) name:@"reloadWebview" object:nil];
    //addVirtualEventFresh
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadWebView:) name:@"addVirtualEventFresh" object:nil];
    
   // addVirtualCompeteDismiss 接受来自我的赛事安排页面，添加虚拟赛事成功，从我的赛事安排页面直接返回竞赛首页
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(leftBarBtnIsClicked) name:@"addVirtualCompeteDismiss" object:nil];
    
    //接收加入团队页面dismiss后的通知，是在加入团队成功后，进入到赛事列表，然后返回的时候直接将本页面dismiss掉
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(leftBarBtnIsClicked) name:@"competeDetailFreshWeb" object:nil];
    
    
    //addVirtualDismiss，添加虚拟赛事，绑定卡，绑定卡页面未绑定时，直接回到竞赛首页
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissTwo) name:@"addVirtualDismiss" object:nil];
   
}


- (void)LoadWebView {
    //以下是对webView的一些处理。
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64)];
    [_webView setUserInteractionEnabled:YES];
    _webView.delegate = self;
    [_webView setOpaque:NO]; //设置是否透明。
    [_webView setScalesPageToFit:YES]; //自动缩放以适应屏幕。
    _webView.scrollView.bounces = YES;
    _webView.scrollView.showsVerticalScrollIndicator = NO;

    //加载网页
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/terminal/addevent?sesskey=%@",localRequestUrl, singleton.sesskey]];
    [_webView loadRequest:[NSURLRequest requestWithURL:url]];
    [self.view addSubview:_webView];

}



#pragma amrk - 点击导航栏左边按钮
- (void)leftBarBtnIsClicked {
    
    [self dismissViewControllerAnimated:NO completion:nil];
    
}

#pragma mark - 接收到消息后，触发方法
- (void)reloadWebView:(NSNotification *)sender {
    
    //加载网页
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/terminal/addevent?sesskey=%@",localRequestUrl, singleton.sesskey]];
    [_webView loadRequest:[NSURLRequest requestWithURL:url]];
   // [self.view addSubview:_webView];
    
}

-(void)dismissTwo
{
    [self dismissViewControllerAnimated:NO completion:nil];
    [self dismissViewControllerAnimated:NO completion:nil];
}


#pragma mark - UIWebViewDelegate methods
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"添加虚拟赛事：%@", request.URL.absoluteString);
    if ([request.URL.absoluteString containsString:@"terminal/error?err=3"]) {
        //弹出系统提示，如果用户点击确定按钮，跳转到“绑定登山设备”界面。 否则回到竞赛界面
        UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"系统提示" message:@"暂无绑定设备，去绑定登山设备" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
            BindingDeviceVC *bindVC = [[BindingDeviceVC alloc] init];
            UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:bindVC];
            [self presentViewController:naVC animated:NO completion:nil];
            
        }];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            [self dismissViewControllerAnimated:NO completion:nil];
        }];
        
        [alertVc addAction:sureAction];
        [alertVc addAction:cancelAction];
        
        [self presentViewController:alertVc animated:NO completion:nil];
        
        return NO;
    } else if ([request.URL.absoluteString containsString:@"terminal/error?err=9"]) {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还没有完善信息，现在完善吗？" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *ensure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            UserInfoVC *userVc = [[UserInfoVC alloc] init];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:userVc];
            [self presentViewController:nav animated:YES completion:nil];
        }];
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [self dismissViewControllerAnimated:NO completion:nil];
        }];
       
        [alertVC addAction:ensure];
        [alertVC addAction:cancel];
        [self presentViewController:alertVC animated:NO completion:nil];
        return NO;
        
    } else if ([request.URL.absoluteString containsString:@"terminal/success?suc"]) {
        
        MyCompeteScheduleVC *comSchedVc = [[MyCompeteScheduleVC alloc] init];
        comSchedVc.fromCompeteAddSuccess = YES;
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:comSchedVc];
        [self presentViewController:naVC animated:NO completion:nil];
        
        return NO;
    }
    
    return YES;
}


- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
}
-(void)viewWillAppear:(BOOL)animated
{
    //加载网页
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/terminal/addevent?sesskey=%@",localRequestUrl, singleton.sesskey]];
    [_webView loadRequest:[NSURLRequest requestWithURL:url]];
}


-(void)dealloc
{

    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"reloadWebview" object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"addVirtualEventFresh" object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"addVirtualCompeteDismiss" object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"addVirtualDismiss" object:nil];
    
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"competeDetailFreshWeb" object:nil];
}




- (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
