//
//  LogInViewController.m
//  Firechat
//
//  Created by Justin Sandhu on 6/3/14.
//  Copyright (c) 2014 Firebase. All rights reserved.
//

#import "LogInViewController.h"
#import <Firebase/Firebase.h>

@interface LogInViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
@property (weak, nonatomic) IBOutlet UIButton *enterButton;

@property int pin;
@property NSDictionary*users;
@property Firebase*fire;
@end

@implementation LogInViewController

@synthesize manager;
@synthesize users;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
   
    if ([identifier isEqualToString:@"openUp" ]) {
        
        
        
        NSString* password= users[self.nameField.text];
        
        if ([password isEqualToString:self.passwordField.text])
        return YES;
        else
        return NO;
    }
           
           
     

    
           
        
        
           
        
   
    return YES;
}
    

       

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"openUp"]) {
        ViewController * chatWindow = (ViewController *) segue.destinationViewController;
        chatWindow.name = self.nameField.text;
        chatWindow.pin = self.pin;

    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[self view] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"IMG_0729.jpeg"]]];
    
    
    self.fire = [[Firebase alloc]initWithUrl:@"https://justinsandhu3.firebaseio.com/accounts/"];
    [self.fire observeSingleEventOfType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
        users = snapshot.value;
    }];
    };

    // Do any additional setup after loading the view.


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 69) {
        
    
    if (buttonIndex == 0) {
      self.pin = [[[alertView textFieldAtIndex:0] text] intValue];
        // sets the value to be passed to the ViewController as the interger value of the text from the text field within the UIAlert View
    }
}
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

         @end
