//
//  ProfileViewControllerViewController.m
//  k-observer-iphone
//
//  Created by vvv ооо on 26.08.12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ProfileViewController.h"
#import "AppDelegate.h"

extern AppDelegate* appDelegate;


@interface ProfileViewController ()

@end

@implementation ProfileViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    UIScrollView *tempScrollView=(UIScrollView *)self.view;
    
    tempScrollView.contentSize=CGSizeMake(320, 780);
}

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

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

-(void)finishEditing
{
    [self.view endEditing:YES];
    self.navigationItem.rightBarButtonItem = nil;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self finishEditing];
    return YES;
}

- (IBAction)OnUpdateProfilePressed
{
    NSString* lastName = ((UITextField*)[self.view viewWithTag:1]).text;
    NSString* firstName = ((UITextField*)[self.view viewWithTag:2]).text;
    NSString* middleName = ((UITextField*)[self.view viewWithTag:3]).text;
    NSString* phoneNumber = ((UITextField*)[self.view viewWithTag:4]).text;
    
    NSString* uikNumberStr = ((UITextField*)[self.view viewWithTag:5]).text;
    
    int uikNumber = [uikNumberStr intValue];
    
    UISegmentedControl* alarlModeControl = (UISegmentedControl*)[self.view viewWithTag:8];
    
    int status = self->Status;
    
    if (status == 0)
    {
    if ([alarlModeControl selectedSegmentIndex])
    {
        int mapState = [appDelegate.observerClient GetMapState];
        
        if (mapState < 2)
        {
            [appDelegate.observerClient SetMapState:2];
        }
    }
    else
    {
        [appDelegate.observerClient SetMapState:1];
    }
    }
    
    [appDelegate.observerClient UpdateProfileWithLastName:lastName
                                                FirstName:firstName MiddleName:middleName PhoneNumber:phoneNumber UikNumber:uikNumber Status:status];
    
}

- (IBAction)OnObserverSelected
{
    self->Status = 0;
    
    UILabel* observer_label = (UILabel*)[self.view viewWithTag:6];
    UILabel* mobile_label = (UILabel*)[self.view viewWithTag:7];
    
    observer_label.text = @"X";
    mobile_label.text = @"";
}

- (IBAction)OnMobileSelected
{
    self->Status = 1;
    
    UILabel* observer_label = (UILabel*)[self.view viewWithTag:6];
    UILabel* mobile_label = (UILabel*)[self.view viewWithTag:7];
    
    observer_label.text = @"";
    mobile_label.text = @"X";
}


-(void)SwitchAlarmSelector:(BOOL)alarm
{
    UISegmentedControl* alarmModeControl = (UISegmentedControl*)[self.view viewWithTag:8];
    
    if (alarm)
    {
        [alarmModeControl setSelectedSegmentIndex:1];
        
    }
    else
    {
        [alarmModeControl setSelectedSegmentIndex:0];
    }
    

}

- (IBAction)EditingDidBegin
{
    UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Готово" style:UIBarButtonItemStylePlain target:self action:@selector(finishEditing)];
    
    
    self.navigationItem.rightBarButtonItem = anotherButton;
}

@end
