//
//  LMPurchaseScene.m
//  Lemon_iOS_SDK_Demo
//
//  Created by syt on 14-3-31.
//  Copyright (c) 2014年 syt. All rights reserved.
//

#import "LMPurchaseScene.h"

@implementation LMPurchaseScene
{
    RootViewController *_rootvc;
    
    UITableView *_tableView;
    
    NSMutableArray *_itemsDataSource;
}

- (id)initWithFrame:(CGRect)frame rootViewController:(RootViewController *)rootViewController
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        _rootvc = rootViewController;
        
        self.backgroundColor = [UIColor whiteColor];
        
        _itemsDataSource = [[NSMutableArray alloc] initWithArray:_rootvc.items];
         LMDebugS(@"_rootvc.items = %@",_rootvc.items);
        
        _tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self addSubview:_tableView];
        
        UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 40)];
        _tableView.tableHeaderView = headView;
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 40)];
        label.text = @"选择商品进行充值";
        label.backgroundColor = [UIColor redColor];
        label.textAlignment = NSTextAlignmentCenter;
        [headView addSubview:label];
        
        UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        closeButton.frame = CGRectMake(self.frame.size.width-50, 0, 50, 50);
        [closeButton setTitle:@"关闭" forState:UIControlStateNormal];
        [closeButton addTarget:self action:@selector(closeButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [headView addSubview:closeButton];
        
        [rootViewController.view addSubview:self];
    }
    return self;
}

#pragma mark - TableViewDelegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_itemsDataSource count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellName = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"商品:%@",[_itemsDataSource objectAtIndex:indexPath.row]];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    
    cell.detailTextLabel.text = @"首冲翻倍";
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];
    
    return cell;
}

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

    //IAP充值接口调用
    LMDebugS(@"进入充值");
    [[LMPlatform sharedInstance] iapPurchase:1 purchaseItem:[_itemsDataSource objectAtIndex:indexPath.row] payExt:@"pay_Ext" withMask:YES];
}

-(void)closeButtonClick
{
    [self removeFromSuperview];
}

@end
