//
//  OptionPopOverViewController.m
//  kneet
//
//  Created by Jason Lee on 4/20/15.
//  Copyright (c) 2015 ntels. All rights reserved.
//
#import "OptionPopOverViewController.h"
#import "CustomLabel.h"
#import "CustomImageView.h"
#import "WYPopoverController.h"

#define kfOptionPopOverWidth 180.0f
#define kfOptionPopOverTableViewCellHeight 45.0f

/*
@implementation OptionPopOverData

- (instancetype)initWithDictionary:(NSDictionary *)dic {
    if (self = [super init]) {
        self.iconName = dic[@"iconName"];
        self.menuName = dic[@"menuName"];
        self.target = dic[@"target"];
        self.selector = [dic[@"selector"] pointerValue];
    }
    return self;
}
@end
*/

@implementation OptionPopOverTableViewCell
- (void)awakeFromNib {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

@end

@interface OptionPopOverViewController () <UITableViewDataSource, UITableViewDelegate> {

}
@end


#pragma mark - Class Definition

@implementation OptionPopOverViewController


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

    [self initUI];
    [self prepareViewDidLoad];
}


- (void)initUI {

    CGFloat height = kfOptionPopOverTableViewCellHeight * self.dataArray.count;
    self.preferredContentSize = CGSizeMake(180, height);

    _tableView.dataSource = self;
    _tableView.delegate = self;

    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _tableView.separatorColor = kUILineColor2;
    _tableView.backgroundColor = [UIColor clearColor];

    _tableView.tableFooterView = [[UIView alloc] init]; //this call table events;

    _tableView.scrollEnabled = NO;
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)prepareViewDidLoad {

}


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [_tableView reloadData];
}

#pragma mark - Main Logic

#pragma mark - UITableView DataSource & Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CellIdentifier";

    OptionPopOverTableViewCell *cell = (OptionPopOverTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[OptionPopOverTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSDictionary *option = _dataArray[indexPath.row];
    cell.lblMenuName.text = option[@"menuName"];
    cell.icon.image = [UIImage imageNamed:option[@"iconName"]];

    return cell;
}

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

    NSDictionary *option = _dataArray[indexPath.row];

    id target = option[@"target"];
    SEL selector = [option[@"selector"] pointerValue];

    [_poc dismissPopoverAnimated:YES completion:^{
        [target performSelector:selector withObject:nil afterDelay:0.0f];
    }];
}

- (void)popoverControllerDidDismissPopover:(WYPopoverController *)controller
{
    _poc.delegate = nil;
    _poc = nil;
}

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