//
//  JDViewController.m
//  kneet2
//
//  Created by Created by Jason Lee on 10/1/15.
//  Copyright (c) 2015 ntels. All rights reserved.
//
#import "CustomTableView.h"

#import "OptionPopOverViewController.h"
#import "WYPopoverController.h"

#import "JDViewController.h"

@interface JDViewController () <WYPopoverControllerDelegate, UITableViewDataSource, UITableViewDelegate> {
    WYPopoverController *_poc;
    OptionPopOverViewController *_ovc;
}
@end


#pragma mark - Class Definition

@implementation JDViewController
@synthesize mainView = _mainView;

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


- (void)initUIOnSuper {

}

- (void)initTableViewAsDefaultStyle:(CustomTableView *)tableView {
    tableView.dataSource = self;
    tableView.delegate = self;
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = [UIColor clearColor];
    
//    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 30.0f)]; //this call table events;
}

- (void)prepareViewDidLoadOnSuper {
    
}

- (UIButton *)generateOptionButton {
    
    UIImage *image = [UIImage imageNamed:@"common_head_btn_more"];
    UIImage *imagePress = [UIImage imageNamed:@"common_head_btn_more_press"];
    CGRect btnFrame = CGRectMake(0, 0, image.size.width, image.size.height);
    
    UIButton *button = [[UIButton alloc] initWithFrame:btnFrame];
    button.tag = 9003;
    
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:imagePress forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(toggleOptions:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = item;
    return button;
}

- (void)toggleOptions:(id)sender {
    UIButton *btn = (UIButton *)sender;
    
    if (!_poc) {
        
        _ovc = (OptionPopOverViewController *)[CommonUtil
                                               instantiateViewControllerWithIdentifier:@"OptionPopOverViewController"
                                               storyboardName:@"Common"];
        
        _ovc.dataArray = _popooverOptionArray;
        
        _poc = [[WYPopoverController alloc] initWithContentViewController:_ovc];
        _poc.delegate = self;
        
        [_poc beginThemeUpdates];
        
        _poc.theme.usesRoundedArrow = NO;
        
        _poc.theme.arrowBase = 15;
        _poc.theme.arrowHeight = 8;
        _poc.theme.borderWidth = 0;
//        _poc.theme.tintColor = [UIColor darkGrayColor];
        _poc.theme.outerStrokeColor = kUILineColor2;
        _poc.theme.innerStrokeColor = kUILineColor2;
        
        _poc.theme.innerCornerRadius = 0.0f;
        _poc.theme.outerCornerRadius = 0.0f;
        _poc.theme.minOuterCornerRadius = 0.0f;
        
//        _poc.theme.fillTopColor = kUIBgColor01;
        
        [_poc endThemeUpdates];
        
        
        _ovc.poc = _poc;
    }
    
//    UIBarButtonItem *barButtonItem = self.navigationItem.rightBarButtonItem;
//    UIView *itemView = [barButtonItem valueForKey:@"view"];
    CGRect popRect = btn.bounds;
    popRect.origin.y = 0.0f;
    
    [_poc presentPopoverFromRect:popRect inView:btn
        permittedArrowDirections:WYPopoverArrowDirectionDown | WYPopoverArrowDirectionUp animated:YES options:WYPopoverAnimationOptionFadeWithScale completion:^{
            [_ovc.tableView reloadData];
        }];
    
//    [_poc presentPopoverFromRect:btn.bounds
//                             inView:btn
//           permittedArrowDirections:WYPopoverArrowDirectionUp
//                           animated:YES
//                            options:WYPopoverAnimationOptionFadeWithScale];
}

- (void)resetOptions {
    _ovc = nil;
    _poc = nil;
}

- (void)dismissOptionPopOver:(void (^)(void))completion {
    [_poc dismissPopoverAnimated:YES completion:^{
        if (completion) {
            completion();
        }
    }];
}


#pragma mark - Main Logic


#pragma mark - UITableView DataSource & Delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // Remove seperator inset
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    // Prevent the cell from inheriting the Table View's margin settings
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    
    // Explictly set your cell's layout margins
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}


#pragma mark - UI Events



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



@end
