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

#import "CompeteScoreVC.h"
#import "Singleton.h"
#import "EachScoreVC.h"
#import "CustomNavViewController.h"

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

@property (nonatomic, strong) UIWebView *webView;

@end

@implementation CompeteScoreVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    singleton = [Singleton defaultSingleton];
    
    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 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"nav_back"] landscapeImagePhone:nil style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemIsClicked:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];

    
    //以下是对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 = [Manager screenFrameWebview];
    _webView.delegate = self;
    //加载网页
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/wap/event_list",localRequestUrl]];
    [_webView loadRequest:[NSURLRequest requestWithURL:url]];
    [self.view addSubview:_webView];
    
}

#pragma mark - 导航栏左边按钮被点击
- (void)leftBarButtonItemIsClicked:(UIBarButtonItem *)sender {
    
    [self dismissViewControllerAnimated:NO completion:nil];
    
}   

#pragma mark - webView的代理方法
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"CompeteScore  %@", request.URL.absoluteString);
    
    if ([request.URL.absoluteString containsString:@"wap/event_repeater_ranking?eventId="]) {
        //跳到我的成绩页面
        MyScoreVC *scoreVC = [[MyScoreVC alloc] init];
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:scoreVC];
        NSArray *ary = [request.URL.absoluteString componentsSeparatedByString:@"eventId="];
        singleton.scoreId = ary[1];
        [self presentViewController:naVC animated:NO completion:nil];
        return NO;
        
    }
    return YES;
}

#pragma  mark －－获取栈顶控制器
- (UIViewController *)getPresentedViewController
{
    UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *topVC = appRootVC;
    if (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    
    return topVC;
}


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