//
//  WebViewController.m
//  Apex Mobile
//
//  Created by Ray on 14-3-13.
//  Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
//




#import "WebViewController.h"



@interface WebViewController ()

@end

@implementation WebViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)ReloadData
{
    
    //    Count ++ ;
    //    [mytabelview reloadData];
    [self loadcontent];
    UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
    
    [reF endRefreshing];
    
}
-(void) loadcontent
{
    self.mum.hidden=false;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:self.url]        cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30];
        
        //设置请求方式为get
        
        [request setHTTPMethod:@"GET"];
        
        //添加用户会话id
        
        [request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
        
        //连接发送请求
        

        NSHTTPURLResponse* urlResponse = nil;
        
        NSError *error = [[NSError alloc] init];
        
        self.content= [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
        
        self.filename = [urlResponse suggestedFilename];
        

        dispatch_async(dispatch_get_main_queue(), ^{
            
            self.navigationItem.title = self.filename;
            UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
            if(self.content!=nil)
               [reF removeFromSuperview];

//
            

            [self.webview loadData:self.content MIMEType:urlResponse.MIMEType textEncodingName:nil baseURL:nil];
            
            
            
            
        });
    });
    
    
    
  
}
-(void)changeCell
{
    UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
    reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"refreshing"];
    [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1];
      DebugLog(@"refresh!!!!!!!!");
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIRefreshControl *ref = [[UIRefreshControl alloc]init];
    ref.tag = 200 ;
    ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"drag to refresh"];
    ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
//    ref.hidden = true;
    [ref addTarget:self action:@selector(changeCell) forControlEvents:UIControlEventValueChanged];
    [self.webview.scrollView addSubview:ref];
    
    [self loadcontent];
    
    
    // Do any additional setup after loading the view.
}

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



- (IBAction)onSaveClick:(UIBarButtonItem *)sender {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    NSString *filePath = [path stringByAppendingPathComponent:self.filename];
//    NSError *error = [[NSError alloc] init];
//    BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
//    NSAssert(bo,@"创建目录失败");
    [self.content writeToFile:filePath atomically:YES];
    
    sender.enabled = false;
//    self.navigationItem.rightBarButtonItem = nil;
}

#pragma mark - web view delegate

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    self.mum.hidden=true;
    

}

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