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

#import "MessagesViewController.h"
#import "AppDelegate.h"

extern AppDelegate* appDelegate;

@interface MessagesViewController ()

@end

@implementation MessagesViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        messageHeadersArray = [[NSMutableArray alloc] init];
        messageArray = [[NSMutableArray alloc] init];
        
        [messageHeadersArray addObject:@"Подключение установлено"];
        [messageArray addObject:@"Подключение к серверу успешно установлено"];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    NSArray* iparr = [NSArray arrayWithObjects:[NSIndexPath indexPathWithIndex:0], [NSIndexPath indexPathWithIndex:1], nil];
    
    UITableView* tableView = (UITableView*)[self.view viewWithTag:4];
    
    [tableView insertRowsAtIndexPaths:iparr withRowAnimation:UITableViewRowAnimationAutomatic];
}

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

-(void)viewDidAppear:(BOOL)animated
{
    UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Отправить" style:UIBarButtonItemStylePlain target:self action:@selector(goToSendMessageView)];
    
    
    self.navigationItem.rightBarButtonItem = anotherButton;
}

-(void)viewDidDisappear:(BOOL)animated
{
   
    self.navigationItem.rightBarButtonItem = nil;
}

-(void)goToSendMessageView
{
    
    self.navigationItem.rightBarButtonItem = nil;
    [appDelegate.navController pushViewController:(UIViewController*)appDelegate.sendMessageViewController animated:YES];
}


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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [messageHeadersArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    //Поиск ячейки
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    //Если ячейка не найдена
    if (cell == nil) {
        //Создание ячейки
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    ;
    
    cell.textLabel.text = [messageHeadersArray objectAtIndex:messageHeadersArray.count-indexPath.row-1];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITextView* textView = (UITextView*)[[self.view viewWithTag:1] viewWithTag:2];
    
    [textView setText:[messageArray objectAtIndex:messageHeadersArray.count-indexPath.row-1]]; 
    

}


-(void)addMessage:(NSString*)message withHeader:(NSString*)header
{
  
    UITableView* tableView = (UITableView*)[self.view viewWithTag:4];
    
    [messageHeadersArray addObject:header];
    [messageArray addObject:message];
    
    [tableView reloadData];
    
    [tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    

}


@end
