//
//  T1StoreDemoViewController.m
//  T1StoreDemo
//
//  Created by Peter Skinner on 1/18/11.
//  Copyright 2011 Ten One Design. All rights reserved.
//

#import "T1StoreDemoViewController.h"
#import "T1Store.h"

@implementation T1StoreDemoViewController


 - (void)loadView {
	 
	 // Set up buttons to open the store.  These buttons can also be created in IB.
 self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 self.view.backgroundColor = [UIColor whiteColor];
 
 UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];		//initWithFrame:CGRectMake(50, 300, 200,60) ];
 [myButton setFrame:CGRectMake(20, 60, 280,40)];
 [myButton setTitle:@"Show Store" forState:UIControlStateNormal];
 [myButton setTitle:@"Showing Store" forState:UIControlEventTouchDown];
 myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
 myButton.backgroundColor = [UIColor clearColor];
 [myButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
 [self.view addSubview:myButton]; 
 
 UIButton *myButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];		//initWithFrame:CGRectMake(50, 300, 200,60) ];
 [myButton2 setFrame:CGRectMake(20, 120, 280,40)];
 [myButton2 setTitle:@"Show Product In Cart" forState:UIControlStateNormal];
 [myButton2 setTitle:@"Showing Product In Cart" forState:UIControlEventTouchDown];
 myButton2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
 myButton2.backgroundColor = [UIColor clearColor];
 [myButton2 addTarget:self action:@selector(buttonAction2:) forControlEvents:UIControlEventTouchUpInside];
 [self.view addSubview:myButton2]; 
     
 UIButton *myButton3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];		//initWithFrame:CGRectMake(50, 300, 200,60) ];
 [myButton3 setFrame:CGRectMake(20, 180, 280,40)];
 [myButton3 setTitle:@"Show Product In Cart Without Store" forState:UIControlStateNormal];
 [myButton3 setTitle:@"Showing Product In Cart Without Store" forState:UIControlEventTouchDown];
 myButton3.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
 myButton3.backgroundColor = [UIColor clearColor];
 [myButton3 addTarget:self action:@selector(buttonAction3:) forControlEvents:UIControlEventTouchUpInside];
 [self.view addSubview:myButton3]; 
     
 UIButton *myButton4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];		//initWithFrame:CGRectMake(50, 300, 200,60) ];
 [myButton4 setFrame:CGRectMake(20, 240, 280,40)];
 [myButton4 setTitle:@"Reset Store" forState:UIControlStateNormal];
 [myButton4 setTitle:@"Resetting Store" forState:UIControlEventTouchDown];
 myButton4.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
 myButton4.backgroundColor = [UIColor clearColor];
 [myButton4 addTarget:self action:@selector(buttonAction4:) forControlEvents:UIControlEventTouchUpInside];
 [self.view addSubview:myButton4]; 
 
 }



-(void)buttonAction:(id)sender {
	
	T1Store *myStore = [T1Store sharedT1Store];
	
	//	Possible customizations (see T1Store.h header file for descriptions)
	
	myStore.testMode = YES;
//	myStore.tintColor = [UIColor colorWithRed:.93 green:.2 blue:.2 alpha:1];
//	myStore.storeTitle = @"myAppName Store";
//	myStore.storeFocus = @"gaming";	// all, stylus, gaming, pogo connect
//	myStore.developerEarningRatio = 0.3;
	
	myStore.delegate = self;
	[myStore show:self];
}

-(void)buttonAction2:(id)sender {
    NSArray *products = @[@{@"number": @"T1-PGCT-302",@"quantity":@1}];
    [[T1Store sharedT1Store] setDelegate:self];
	[[T1Store sharedT1Store] showForViewController:self withProducts:products inCart:YES allowOthers:YES];
}

-(void)buttonAction3:(id)sender {
    NSArray *products = @[@{@"number": @"T1-PGCT-302",@"quantity":@1}];
    [[T1Store sharedT1Store] setDelegate:self];
	[[T1Store sharedT1Store] showForViewController:self withProducts:products inCart:YES allowOthers:NO];
}

-(void)buttonAction4:(id)sender {
	[[T1Store sharedT1Store] setTintColor:nil];
	[[T1Store sharedT1Store] freeMemory:self];
}

// Delegate methods (be sure to set T1Store delegate to self if you want these to be called)
- (void)store:(T1Store *)store didCompletePurchase:(NSDictionary *)purchase {
	NSLog(@"purchase %@",purchase);
}
- (BOOL)store:(T1Store *)store shouldFailWithError:(NSError *)error {
	NSLog(@"store load failed");
	return YES;
}
- (void)storeDidDismissModalView:(T1Store *)store  {
    NSLog(@"store view was closed");
	
	// This demo app can load several different types of stores.
	// Since the next store type displayed may be different, we'll want to clear out any cached assets.
	// You probably won't want to do this in your app unless you really prefer a clean cache.
    [store freeMemory:self];
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}



@end
