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

#import "LocalDocumentsViewController.h"

@interface LocalDocumentsViewController ()

@end

@implementation LocalDocumentsViewController
- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
-(void)initpanel
{
    
    self.toolsinfo = [[NSMutableArray alloc] init];
        NSMutableDictionary* map = [[NSMutableDictionary alloc] init];
    
    
    NSFileManager *manager;
    manager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    NSDirectoryEnumerator *direnum;
    direnum = [manager enumeratorAtPath: path];//枚举home下的目录
    NSLog(@"home=%@,direnum=%@",path,direnum);
    NSMutableArray *files;
    files = [NSMutableArray arrayWithCapacity:5];
    NSString *filename;
    while (filename = [direnum nextObject])
    {
        if ([[filename pathExtension] isEqualToString:@"pdf"])
        {
            [map setValue:filename forKey:@"title"];
//            [map setValue:@"rect_search_documents" forKey:@"img"];
            [self.toolsinfo addObject:map.copy];
            
            [files addObject: filename];
            NSLog (@"%@", filename);
        }
    }
//    NSEnumerator *fileenum;
//    fileenum = [files objectEnumerator];//枚举数组files
//    while (filename = [fileenum nextObject]) {
//        NSLog (@"%@", filename);
//    }
//    
    
    
    
    
    
    
    

    
//
//    
//    [map setValue:@"View document" forKey:@"title"];
//    [map setValue:@"rect_view_download_documents" forKey:@"img"];
//    [self.toolsinfo addObject:map.copy];
//    
    
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initpanel];
}

- (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.
 }
 */



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.toolsinfo.count;
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DebugLog(@"cellForRowAtIndexPath");
    
    NSString *CellIdentifier = @"documentitem";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    NSString* title = [self.toolsinfo[indexPath.row] valueForKey:@"title"];
    
    
//    cell.accessoryView
    //    UIImageView* iv= [[UIImageView alloc] initWithImage:];
    cell.textLabel.text = title;
//    cell.imageView.image = [UIImage imageNamed:[self.toolsinfo[indexPath.row] valueForKey:@"img"]];
    
    return cell;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    NSString* filename =[self.toolsinfo[indexPath.row] valueForKey:@"title"];
    
    NSString *filePath = [path stringByAppendingPathComponent:filename];
    [self openFile:filePath];
    
//    NSFileManager *manager;
//    manager = [NSFileManager defaultManager];
//
//    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//    NSString *path = [paths objectAtIndex:0];
//    NSString* filename =[self.toolsinfo[indexPath.row] valueForKey:@"title"];
//    
//        NSString *filePath = [path stringByAppendingPathComponent:filename];
//    
//    
//    
//    PdfViewController *ViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PdfViewController"];
//    ViewController.filepath = filePath;
//    //    ViewController. = self.function_name;
//    //    ViewController.behavior =BEHAVIOR_SEARCH;
//    
//    [self.navigationController pushViewController:ViewController animated:YES];
    
    //    SearchViewController *searchViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"NewsView"];
    //    searchViewController.function_name = @"Download Document";
    //    [self.navigationController pushViewController:searchViewController animated:YES];
    //    return;
    
//    NSString* title = [self.toolsinfo[indexPath.row] valueForKey:@"title"];
//    if([title isEqualToString:@"Search documents"])
//    {
//        SearchViewController *searchViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"SearchViewController"];
//        searchViewController.function_name = @"Download Document";
//        [self.navigationController pushViewController:searchViewController animated:YES];
//    }
//    else if([title isEqualToString:@"View document"])
//    {
//        LocalDocumentsViewController *viewcontroller=[self.storyboard instantiateViewControllerWithIdentifier:@"LocalDocumentsViewController"];
//        //        viewcontroller.module_name = @"Announcements";
//        [self.navigationController pushViewController:viewcontroller animated:YES];
//    }
    
}


//-(void)tableView:(UITableView *)tableView1 accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
//    
//    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//    NSString *path = [paths objectAtIndex:0];
//    NSString* filename =[self.toolsinfo[indexPath.row] valueForKey:@"title"];
//    
//    NSString *filePath = [path stringByAppendingPathComponent:filename];
//    [self openFile:filePath];
//
////        NSURL *url = [[NSURL alloc] initFileURLWithPath:filePath];
////
////    UIDocumentInteractionController *documentController =[UIDocumentInteractionController interactionControllerWithURL: url];
////    documentController.delegate = self;
////    [documentController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
////    
////    return;
//    
////        NSURL *url = [[NSURL alloc] initFileURLWithPath:filePath];
////        UIDocumentInteractionController *documentController =[UIDocumentInteractionController interactionControllerWithURL: url];
////    
////        return;
//}


-(void)openFile: (NSString *) file_url  {
    
    NSURL *file_URL = [NSURL fileURLWithPath:file_url];
    
    if (file_URL != nil) {
        if (self.fileInteractionController == nil) {
            //_fileInteractionController = [[UIDocumentInteractionController alloc] init];
            _fileInteractionController = [UIDocumentInteractionController interactionControllerWithURL:file_URL];
            _fileInteractionController.delegate = self;
            //  [_fileInteractionController retain];//不适用ARC,记得RETAIN 使用ARC注释这一行
        }
        else {
            self.fileInteractionController.URL = file_URL;
        }
        
// [self.fileInteractionController presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
        [self.fileInteractionController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
//        [self.fileInteractionController  presentPreviewAnimated:YES];
        
    }
}
#pragma mark methods for the UIDocumentInteractionControllerDelegate

- (void)previewDocumentWithURL:(NSURL*)url
{
    UIDocumentInteractionController* preview = [UIDocumentInteractionController interactionControllerWithURL:url];
    preview.delegate = self;
    [preview presentPreviewAnimated:YES];

}

- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{

}

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
    return self;
}

- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
    return self.view.frame;
}

- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
    return self.view;
}
@end
