//
//  WPAllWallpaperViewController.m

#import "WPAllWallpaperViewController.h"
#import "WPDetailViewController.h"

#import "WallPapaerADManager.h"
#define WPScreen_w  [UIScreen mainScreen].bounds.size.width
#define WPScreen_h [UIScreen mainScreen].bounds.size.height
#define WP_COLOR_WITH_HEX(HEX) [UIColor colorWithRed:((HEX >> 16) & 0xFF) / 255.0f green:((HEX >> 8) & 0xFF) / 255.0f blue:((HEX) & 0xFF) / 255.0f alpha:1.0f]
@interface WPAllWallpaperViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *table;
@property (nonatomic,strong) NSArray *data;
@property(nonatomic,strong)UIButton *backItem;

@end

@implementation WPAllWallpaperViewController
- (void)backItemEvent{
    __weak typeof(self) weakSelf = self;
    [[WallPapaerADManager shareInstance] showIntersitialAD_Back:^(NSInteger actionType) {
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
//    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar addSubview:self.backItem];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.backItem removeFromSuperview];
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"";
    self.navigationItem.hidesBackButton = YES;
    UIButton *backItem = [[UIButton alloc] initWithFrame:CGRectMake(20, 0, 74, 44)];
    [backItem setImage:[UIImage imageNamed:@"backitem"] forState:UIControlStateNormal];
    [backItem addTarget:self action:@selector(backItemEvent) forControlEvents:UIControlEventTouchUpInside];
    self.backItem = backItem;
    
    self.data = @[@[@"fj1",@"fj2",@"fj3",@"fj4",@"fj5",@"fj6",@"fj7",@"fj8",@"fj9",@"fj10"],@[@"mv1",@"mv2",@"mv3",@"mv4",@"mv5",@"mv6",@"mv7",@"mv8",@"mv9",@"mv10"],@[@"kt1",@"kt2",@"kt3",@"kt4",@"kt5",@"kt6",@"kt7",@"kt8",@"kt9"],@[@"rw1",@"rw2",@"rw3",@"rw4",@"rw5",@"rw6",@"rw7",@"rw8",@"rw9",@"rw10",@"rw11",@"rw12",@"rw13",@"rw14",@"rw15",@"rw16",@"rw17",@"rw18",@"rw19"]];
    UIImageView *bgImage = [[UIImageView alloc] initWithFrame:self.view.bounds];
    bgImage.image = [UIImage imageNamed:@"bg"];
    bgImage.userInteractionEnabled = YES;
    [self.view addSubview:bgImage];
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WPScreen_w, WPScreen_h) style:UITableViewStyleGrouped];
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.backgroundColor = [UIColor clearColor];
    self.table.separatorColor = [UIColor clearColor];
    [self.view addSubview:self.table];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.data.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat space = (WPScreen_w-50)/4;
    if (indexPath.section == 3) {
        return 5*(10+space*1.5)+10;
    }
    return 3*(10+space*1.5)+10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CGFloat space = (WPScreen_w-50)/4;
    
    for (int i = 0; i < [self.data[indexPath.section] count]; i ++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10+i%4*(10+space), 10+i/4*(10+space*1.5), space, space*1.5)];
        [btn setImage:[UIImage imageNamed:self.data[indexPath.section][i]] forState:UIControlStateNormal];
        btn.layer.cornerRadius = 5;
        btn.layer.masksToBounds = YES;
        [btn addTarget:self action:@selector(headerEvent:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = indexPath.section;
        [cell.contentView addSubview:btn];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WPScreen_w, 50)];
    headerLabel.textColor = WP_COLOR_WITH_HEX(0X333333);
    headerLabel.font = [UIFont boldSystemFontOfSize:20];
    NSString *str = @"";
    if (section == 0) {
        str = @"风景壁纸";
    }else if (section == 1) {
        str = @"人物壁纸";
    }else if (section == 2) {
        str = @"卡通壁纸";
    }else if (section == 3) {
        str = @"剧照壁纸";
    }
    headerLabel.text = [NSString stringWithFormat:@"    %@",str];
    return headerLabel;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc] init];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WPDetailViewController *wpdetail = [[WPDetailViewController alloc] init];
    wpdetail.data = self.data[indexPath.section];
    [self.navigationController pushViewController:wpdetail animated:YES];
}
- (void)headerEvent:(UIButton *)btn{
    WPDetailViewController *wpdetail = [[WPDetailViewController alloc] init];
    wpdetail.data = self.data[btn.tag];
    [self.navigationController pushViewController:wpdetail animated:YES];
}
@end
