//
//  myFacebookViewController.m
//  m
//
//  Created by C4N TECHNOLOGY on 9/21/12.
//  Copyright (c) 2012 C4N TECHNOLOGY. All rights reserved.
//

#import "myFacebookViewController.h"
#import "AppDelegate.h"
#import "ViewController.h"

#import "SCProtocols.h"
#import <AddressBook/AddressBook.h>
#import "TargetConditionals.h"


@interface myFacebookViewController()
< UITableViewDataSource,
UIImagePickerControllerDelegate,
FBFriendPickerDelegate,
UINavigationControllerDelegate,
FBPlacePickerDelegate,
CLLocationManagerDelegate,
UIActionSheetDelegate>

@property (strong, nonatomic) FBUserSettingsViewController *settingsViewController;
@property (strong, nonatomic) IBOutlet FBProfilePictureView *userProfileImage;
@property (strong, nonatomic) IBOutlet UILabel *userNameLabel;
@property (strong, nonatomic) IBOutlet UILabel *idLabel;
@property (strong, nonatomic) IBOutlet UILabel *sexLabel;
@property (strong, nonatomic) IBOutlet UILabel *locationLabel;
@property (strong, nonatomic) IBOutlet UILabel *birthdayLabel;


@property (strong, nonatomic) UIImagePickerController *imagePicker;
@property (strong, nonatomic) UIActionSheet *imagePickerActionSheet;
@property (strong, nonatomic) UIActionSheet *mealPickerActionSheet;
@property (retain, nonatomic) NSArray *mealTypes;

@property (strong, nonatomic) NSObject<FBGraphPlace> *selectedPlace;
@property (strong, nonatomic) NSString *selectedMeal;
@property (strong, nonatomic) NSArray *selectedFriends;
@property (strong, nonatomic) UIImage *selectedPhoto;
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) FBCacheDescriptor *placeCacheDescriptor;
@property (strong, nonatomic) UIPopoverController *popover;

@property (nonatomic) CGRect popoverFromRect;
@property (strong, nonatomic) UIActivityIndicatorView *activityIndicator;



- (void)populateUserDetails;
- (void)updateSelections;
- (void)updateCellIndex:(int)index withSubtitle:(NSString *)subtitle;

- (void)postPhotoThenOpenGraphAction;
- (void)postOpenGraphActionWithPhotoURL:(NSString *)photoID;
- (void)centerAndShowActivityIndicator;
- (void)setPlaceCacheDescriptorForCoordinates:(CLLocationCoordinate2D)coordinates;

@end

@implementation myFacebookViewController
@synthesize userNameLabel = _userNameLabel;
@synthesize idLabel = _idLabel;
@synthesize sexLabel = _sexLabel;
@synthesize locationLabel = _locationLabel;
@synthesize userProfileImage = _userProfileImage;
@synthesize selectedPlace = _selectedPlace;
@synthesize selectedMeal = _selectedMeal;
@synthesize selectedFriends = _selectedFriends;
@synthesize birthdayLabel = _birthdayLabel;

@synthesize selectedPhoto = _selectedPhoto;
@synthesize imagePicker = _imagePicker;


@synthesize locationManager = _locationManager;
@synthesize popover = _popover;
@synthesize imagePickerActionSheet = _imagePickerActionSheet;
@synthesize mealPickerActionSheet = _mealPickerActionSheet;
@synthesize popoverFromRect = _popoverFromRect;
@synthesize activityIndicator = _activityIndicator;
@synthesize settingsViewController = _settingsViewController;
@synthesize mealTypes = _mealTypes;
@synthesize placeCacheDescriptor = _placeCacheDescriptor;

#pragma mark open graph


// FBSample logic
// This is a helper function that returns an FBGraphObject representing a meal

// FBSample logic
// Creates the Open Graph Action with an optional photo URL.
// FBSample logic
// Handles the user clicking the Announce button, by either creating an Open Graph Action
// or first uploading a photo and then creating the action.


- (void)centerAndShowActivityIndicator {
    CGRect frame = self.view.frame;
    CGPoint center = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame));
    self.activityIndicator.center = center;
    [self.activityIndicator startAnimating];
}
#pragma mark UIImagePickerControllerDelegate methods



#pragma mark -

#pragma mark UIActionSheetDelegate methods

#pragma mark -

#pragma mark Data fetch



// FBSample logic
// Displays the user's name and profile picture so they are aware of the Facebook
// identity they are logged in as.
- (void)populateUserDetails {
    
       
    
    if (FBSession.activeSession.isOpen) {
        [[FBRequest requestForMe] startWithCompletionHandler:
         ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
             if (!error) {
                 self.userNameLabel.text = user.name;
                 self.idLabel.text = user.id;
                 self.userProfileImage.profileID = [user objectForKey:@"id"];
                 
                 NSString *gender = [user objectForKey:@"gender"];
                 self.sexLabel.text = (@"%@",gender);
                 
                 NSString *location = [[user objectForKey:@"location"] objectForKey:@"name"];
                 self.locationLabel.text = (@"%@",location);
                 
//                 NSString *birthday = [user objectForKey:@"birthday"];
//                 NSLog(@"%@",birthday);
//                 self.birthdayLabel.text = (@"%@",birthday);
                 
                 NSString *email = [user objectForKey: @"email"];
                 NSLog(@"%@",email);
                 
                 NSString *relationship = [user objectForKey:@"birthday"];
                 NSLog(@"%@",relationship);
                 
             }
         }];
    }
}

- (void)dealloc {
    _locationManager.delegate = nil;
    _imagePicker.delegate = nil;
    _imagePickerActionSheet.delegate = nil;
    _mealPickerActionSheet.delegate = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"C4N Sample";
    
    // Get the CLLocationManager going.
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    // We don't want to be notified of small changes in location, preferring to use our
    // last cached results, if any.
    self.locationManager.distanceFilter = 50;
    [self.locationManager startUpdatingLocation];
    
    // This avoids a gray background in the table view on iPad.
    
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
                                              initWithTitle:@"Settings"
                                              style:UIBarButtonItemStyleBordered
                                              target:self
                                              action:@selector(settingsButtonWasPressed:)];
    
    self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.activityIndicator.hidesWhenStopped = YES;
    [self.view addSubview:self.activityIndicator];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(sessionStateChanged:)
                                                 name:SCSessionStateChangedNotification
                                               object:nil];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (FBSession.activeSession.isOpen) {
        [self populateUserDetails];
    }
}

-(void)settingsButtonWasPressed:(id)sender {
    if (self.settingsViewController == nil) {
        self.settingsViewController = [[FBUserSettingsViewController alloc] init];
    }
    [self.navigationController pushViewController:self.settingsViewController animated:YES];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    // Release any retained subviews of the main view.
        self.imagePicker = nil;
    self.popover = nil;
    self.imagePickerActionSheet = nil;
    self.mealPickerActionSheet = nil;
}

- (void)sessionStateChanged:(NSNotification*)notification {
    // A more complex app might check the state to see what the appropriate course of
    // action is, but our needs are simple, so just make sure our idea of the session is
    // up to date and repopulate the user's name and picture (which will fail if the session
    // has become invalid).
    [self populateUserDetails];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)setPlaceCacheDescriptorForCoordinates:(CLLocationCoordinate2D)coordinates {
    self.placeCacheDescriptor =
    [FBPlacePickerViewController cacheDescriptorWithLocationCoordinate:coordinates
                                                        radiusInMeters:1000
                                                            searchText:@"restaurant"
                                                          resultsLimit:50
                                                      fieldsForRequest:nil];
}
@end

Add a code snippet to your website: www.paste.org