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

#import "CompeteViewController.h"
#import "UIViewController+MMDrawerController.h"
#import "MMDrawerBarButtonItem.h"
#import "LoginViewController.h"
#import "AddVirtualEventVC.h"
#import "Singleton.h"
#import "BindingDeviceVC.h"
#import "CompeteDetailVC.h"

@interface CompeteViewController () <UIWebViewDelegate> {
    Singleton *singleton;
}
@property (nonatomic, strong) UIWebView *webView;

@end

@implementation CompeteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建单例实例
    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 - 设置导航栏左边的按钮
 //   self.navigationItem.leftBarButtonItem = [Manager rightItemTarget:self img:@"menu" tag:1000000 action:@selector(leftBtnIsClicked)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"menu"] landscapeImagePhone:nil style:UIBarButtonItemStylePlain target:self action:@selector(leftBtnIsClicked)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];

    
#pragma - 设置首页导航栏右边的button
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"add"] landscapeImagePhone:nil style:UIBarButtonItemStylePlain target:self action:@selector(rightBtnIsClicked)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
    // create webview
    [self creatWebView];

    
    UIScrollView *scrollView = self.webView.scrollView;
    // 添加下拉刷新控件
    scrollView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/terminal/event/introduce_index",localRequestUrl]];
            [_webView loadRequest:[NSURLRequest requestWithURL:url]];
    }];
}

#pragma mark 创建webView
-(void)creatWebView
{
#pragma - 以下是对webView的一些处理。
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    [_webView setUserInteractionEnabled:YES];//是否支持交互
    [_webView setOpaque:NO]; //opaque是不透明的意思
    [_webView setScalesPageToFit:YES]; //自动缩放以适应屏幕
    _webView.scrollView.showsVerticalScrollIndicator = NO;

        _webView.delegate = self;
    //加载网页的方式
    //1.创建并加载远程网页
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/terminal/event/introduce_index",localRequestUrl]];
    [_webView loadRequest:[NSURLRequest requestWithURL:url]];
    [self.view addSubview:_webView];
    
}
#pragma mark - 导航栏左侧button点击事件处理
- (void)leftBtnIsClicked {
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:^(BOOL finished) {
    }];
}



#pragma mark - 导航栏右侧button点击事件处理
- (void)rightBtnIsClicked {
//    NSDictionary *accoutDic = [NSDictionary dictionaryWithContentsOfFile:AccountPath];
    if (![singleton.string isEqualToString:@"9002"]) {
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:loginVC];
        [self presentViewController:naVC animated:YES completion:nil];

    } else {
        //跳转到“添加虚拟赛事界面”
        AddVirtualEventVC *virtualVC = [[AddVirtualEventVC alloc] init];
        
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:virtualVC];
        [self presentViewController:naVC animated:YES completion:nil];
    }
}

#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
//    NSLog(@"竞赛：%@", request.URL.absoluteString);
    if ([request.URL.absoluteString containsString:@"event/event_info?eventId"]) {
        //跳到“赛事详情”页面
        CompeteDetailVC *comDVC = [[CompeteDetailVC alloc] init];
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:comDVC];
        NSArray *ary = [request.URL.absoluteString componentsSeparatedByString:@"eventId="];
        //        NSRange range = {request.URL.absoluteString.length - 3, 3};//第一个数代表起始位置, 第二个数代表长度
        //        NSString *str = [request.URL.absoluteString substringWithRange:range];
        //        NSLog(@"%@", ary[1]);
        comDVC.urlStr = ary[1];
        [self presentViewController:naVC animated:NO completion:nil];
        
        return NO;
    }
    
    return YES;
}

#pragma mark - webViewDelegate
- (void)webViewDidFinishLoad:(nonnull UIWebView *)webView
{
    [self.webView.scrollView.mj_header endRefreshing];
    
}

#pragma mark webView加载错误执行的函数
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    MapInfo *info = [MapInfo shareMapInfo];
    if (info.internetStatus == AFNetworkReachabilityStatusNotReachable) {
        [FileDownloadTool alert:@"网络错误或链接超时" view:self.webView];
    }
    
    [self.webView.scrollView.mj_header endRefreshing];
}

-(void)viewWillAppear:(BOOL)animated
{
//    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/terminal/event/introduce_index",localRequestUrl]];
//    [_webView loadRequest:[NSURLRequest requestWithURL:url]];
}

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