//
//  ButtonPullDownPrepare.m
//  22222222222222
//
//  Created by liangchenying on 12-4-9.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ButtonPullDownPrepare.h"

@implementation ButtonPullDownPrepare

@synthesize title               = _title;
@synthesize picker_view         = _picker_view;
@synthesize container_view      = _container_view;
@synthesize action_sheet        = _action_sheet;
@synthesize pop_over_controller = _pop_over_controller;
@synthesize self_reference      = _self_reference;

- (NSString*)title
{
	if(_title == nil)
	{
		_title = [[NSString alloc] init];
	}
	return _title;
}

- (id)initWithPicker:(id)Action 
{
    self = [super init];
    
    if (self) {
        self.container_view = Action;
        self.self_reference = self;
    }
    
    return self;
}

- (void)dealloc 
{
    if ([self.picker_view respondsToSelector:@selector(setDelegate:)])   [self.picker_view performSelector:@selector(setDelegate:)   withObject:nil];
    if ([self.picker_view respondsToSelector:@selector(setDataSource:)]) [self.picker_view performSelector:@selector(setDataSource:) withObject:nil];
    
    self.picker_view         = nil;
    self.container_view      = nil;
    self.action_sheet        = nil;
    self.pop_over_controller = nil;
    
    [super dealloc];
}

- (void)showSheetPicker
{
    UIView *sheet_picker_view = [[UIView alloc] init];
    
    UIToolbar *add_toolbar = [self createSheetPickerToolbarTitle:self.title];
    
    [add_toolbar setBarStyle:UIBarStyleBlackTranslucent];
    
 
    [sheet_picker_view addSubview:add_toolbar];
    
    self.picker_view = [self configuredPickerView];
    
    [sheet_picker_view addSubview:self.picker_view];
    
    [sheet_picker_view setFrame:CGRectMake(0, 0, add_toolbar.frame.size.width, add_toolbar.frame.size.height + self.picker_view.frame.size.height)];
    
    [self presentPickerForView:sheet_picker_view];
    
    [sheet_picker_view release];
}

- (UIView *)configuredPickerView
{
    return nil;
}

- (void)notifyDoneButton
{
}

- (void)notifyCancelButton
{
}

- (UIToolbar *)createSheetPickerToolbarTitle:(NSString *)title  
{
    UIToolbar *add_toolbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)] autorelease];      
    
    NSMutableArray *array_toolbar = [[NSMutableArray alloc] init];
    
    [array_toolbar addObject:[self createButton:UIBarButtonSystemItemCancel target:self action:@selector(actionPickerCancel:)]];
    
    [array_toolbar addObject:[self createToolbarTitle:self.title]];
    
    [array_toolbar addObject:[self createButton:UIBarButtonSystemItemDone target:self action:@selector(actionPickerDone:)]];
    
    [add_toolbar setItems:array_toolbar animated:YES];
    
    [array_toolbar release];
    
    return add_toolbar;
}

- (UIBarButtonItem *)createToolbarTitle:(NSString *)title 
{
    UILabel *toolbar_item = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 185, 40)];     // fixed width and height
    
    [toolbar_item setTextAlignment:UITextAlignmentCenter];
    
    [toolbar_item setFont:[UIFont boldSystemFontOfSize:16]];
    
    [toolbar_item setTextColor      :[UIColor whiteColor]];
    [toolbar_item setBackgroundColor:[UIColor clearColor]];
    
    toolbar_item.text = self.title;
    
    UIBarButtonItem *button = [[[UIBarButtonItem alloc]initWithCustomView:toolbar_item] autorelease];
    
    [toolbar_item release];
    
    return button;
}

- (UIBarButtonItem *)createButton:(UIBarButtonSystemItem)type target:(id)target action:(SEL)buttonAction
{
    return [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:type target:target action:buttonAction] autorelease];
}

- (void)presentPickerForView:(UIView *)view 
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) [self configureAndPresentActionSheetForView:view];
    else                                                        [self configureAndPresentPopoverForView    :view];
}

- (void)configureAndPresentActionSheetForView:(UIView *)view 
{
    self.action_sheet = [[UIActionSheet alloc] init];
    
    [self.action_sheet addSubview:view];
    
    [self.action_sheet showInView:self.container_view];
    
    self.action_sheet.bounds = [[UIScreen mainScreen] bounds];
}

- (void)configureAndPresentPopoverForView:(UIView *)view 
{
    UIViewController *viewController = [[UIViewController alloc] init];
    
    viewController.view                        = view;
    viewController.contentSizeForViewInPopover = view.frame.size;
    
    self.pop_over_controller = [[UIPopoverController alloc] initWithContentViewController:viewController];
    
    [self.pop_over_controller presentPopoverFromRect:self.container_view.superview.frame inView:self.container_view.superview permittedArrowDirections:0 animated:YES];
    
    [viewController release];
}

- (IBAction)actionPickerDone:(id)sender 
{
    [self notifyDoneButton];
    [self dismissPicker];
}

- (IBAction)actionPickerCancel:(id)sender 
{
    [self notifyCancelButton];
    [self dismissPicker];
}

- (void)dismissPicker 
{
    if (self.action_sheet) 
    {
        if (self.action_sheet.isVisible) 
        {
            [self.action_sheet dismissWithClickedButtonIndex:0 animated:YES];
        }
    }
    
    if (self.pop_over_controller)
    {
        if (self.pop_over_controller.isPopoverVisible) 
        {
            [self.pop_over_controller dismissPopoverAnimated:YES];
        }
    }
    
    self.action_sheet        = nil;
    self.pop_over_controller = nil;
    self.self_reference      = nil;
}

@end
