//
//  SortItemViewController.m
//  iSales-NPD
//
//  Created by Jack on 16/9/5.
//  Copyright © 2016年 United Software Applications, Inc. All rights reserved.
//

#import "SortItemViewController.h"
#import "SortItemCell.h"

@interface SortItemViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    CGSize tableSize;
    CGPoint tableOrigin;
}
@property (nonatomic,strong) UITableView *sortTable;

@property (nonatomic,strong) UIView *tableBackgroundView;

@end

@implementation SortItemViewController

- (instancetype)initWithTableOrigin:(CGPoint)origin {
    if (self = [super init]) {
        tableOrigin = origin;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    tableSize = CGSizeMake(180, 200);
    self.view.backgroundColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.5];
    
    [self.view addSubview:self.tableBackgroundView];
    
}

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

#pragma mark - view

- (UIView *)tableBackgroundView {
    if (!_tableBackgroundView) {
        
        _tableBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(tableOrigin.x, tableOrigin.y, tableSize.width, tableSize.height + 20)];
        _tableBackgroundView.layer.cornerRadius = 5;
        _tableBackgroundView.layer.masksToBounds = YES;
        _tableBackgroundView.backgroundColor = [UIColor whiteColor];
        
        [_tableBackgroundView addSubview:self.sortTable];
        
    }
    return _tableBackgroundView;
}

- (UITableView *)sortTable {
    if (!_sortTable) {
        _sortTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, tableSize.width, tableSize.height) style:UITableViewStylePlain];
        _sortTable.backgroundColor = [UIColor whiteColor];
        _sortTable.showsVerticalScrollIndicator = NO;
        _sortTable.showsHorizontalScrollIndicator = NO;
        _sortTable.dataSource = self;
        _sortTable.delegate = self;
    }
    return _sortTable;
}

#pragma mark data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SortItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[SortItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    switch (indexPath.row) {
        case 0:{
            cell.sortTitle = @"Last";
        }
            break;
        case 1:{
            cell.sortTitle = @"First";
        }
            break;
        case 2:{
            cell.sortTitle = @"Item number a-z";
        }
            break;
        case 3:{
            cell.sortTitle = @"Item number z-a";
        }
            break;
        case 4:{
            cell.sortTitle = @"Description";
        }
            break;
            
        default:
            break;
    }
    cell.sortIndex = indexPath.row;
    
//    if (self.sortIndex == indexPath.row) {
        cell.selectedSort = YES;
//    }
    
    return cell;
}

#pragma mark delegate

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SortItemCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selected = NO;
    
//    dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 0.25 * NSEC_PER_SEC);
//    dispatch_after(time, dispatch_get_main_queue(), ^{
//       
//    });

//    if (indexPath.row != self.sortIndex) {
//        NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:self.sortIndex inSection:0];
//        SortItemCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
//        oldCell.selectedSort = NO;
        
        self.sortIndex = indexPath.row;
        cell.selectedSort = YES;
        
        if (self.sortBlock) {
            self.sortBlock(self.sortIndex);
        }
        
//    }
    
    [self.view removeFromSuperview];
    
}


@end
