//
//  WPOnePageViewController.m

#import "WPOnePageViewController.h"
#import "WPSearchViewController.h"
#import "WPAllWallpaperViewController.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 WPOnePageViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *table;
@property (nonatomic,strong) NSArray *data;

@end

@implementation WPOnePageViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"";
    self.data = @[@[@"h1",@"h2",@"h3"],@[@"fj9",@"kt5",@"mv9",@"rw4",@"rw12",@"mv4",@"fj1",@"kt3",@"fj2",@"mv2"]];
    UIImageView *bgImage = [[UIImageView alloc] initWithFrame:self.view.bounds];
    bgImage.image = [UIImage imageNamed:@"bg"];
    bgImage.userInteractionEnabled = YES;
    [self.view addSubview:bgImage];
    
    UIButton *searchBtn = [[UIButton alloc] initWithFrame:CGRectMake((WPScreen_w-300)/2, 100, 300, 96)];
    [searchBtn setBackgroundImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
    [searchBtn addTarget:self action:@selector(searchEvent) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:searchBtn];
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 220, WPScreen_w, WPScreen_h-220) 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];
}
- (void)searchEvent{
    WPSearchViewController *vc = [[WPSearchViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.data.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 130;
    }else{
        CGFloat space = (WPScreen_w-20-50)/4;
        return 3*(10+space*1.5)+30;
    }
}
- (void)headerEvent{
    WPAllWallpaperViewController *wpdetail = [[WPAllWallpaperViewController alloc] init];
    [self.navigationController pushViewController:wpdetail animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        for (int i = 0; i < 3; i ++) {
            UIImageView *header = [[UIImageView alloc] initWithFrame:CGRectMake(10+i*130, 0, 120, 120)];
            header.image = [UIImage imageNamed:self.data[indexPath.section][i]];
            header.layer.cornerRadius = 10;
            header.layer.masksToBounds = YES;
            header.contentMode = UIViewContentModeScaleAspectFill;
            header.userInteractionEnabled = YES;
            
            UIButton *headerbtn = [[UIButton alloc] initWithFrame:header.bounds];
            [headerbtn addTarget:self action:@selector(headerEvent) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:header];
        }
    }else{
        CGFloat space = (WPScreen_w-20-50)/4;
        UIView *bgView =[[UIView alloc] initWithFrame:CGRectMake(10, 10, WPScreen_w-20, 3*(10+space*1.5)+20)];
        bgView.backgroundColor = [UIColor whiteColor];
        bgView.layer.cornerRadius = 20;
        bgView.layer.masksToBounds = YES;
        bgView.userInteractionEnabled = YES;
        [cell.contentView addSubview:bgView];
        
        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];
            [bgView 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];
    headerLabel.text = [NSString stringWithFormat:@"    %@",section == 0 ? @"推荐壁纸" : @"精选壁纸"];
    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];
    WPAllWallpaperViewController *wpdetail = [[WPAllWallpaperViewController alloc] init];
    [self.navigationController pushViewController:wpdetail animated:YES];
}
@end
