//
//  CESecondViewController.m
//  DemoCustomXmliOSApi
//
//  Created by sviluppo on 26/03/14.
//  Copyright (c) 2014 CUSTOM. All rights reserved.
//

#import "CESecondViewController.h"
#import <CPTiOSApi/CustomXmliOSApi.h>
#import <CPTiOSApi/CustomXmlDictionaryItem.h>
#import "CEFirstViewController.h"

@interface CESecondViewController () <UITableViewDataSource, UITableViewDelegate>

@end

@implementation CESecondViewController

@synthesize paramsArray;
@synthesize tableParams;
@synthesize textParam;


static CESecondViewController *instance =nil;
+(CESecondViewController *)getInstance
{
    return instance;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    //Set Table Color (tableBTDevices)
    [tableParams.layer setBackgroundColor: [[UIColor whiteColor] CGColor]];
    [tableParams.layer setBorderColor: [[UIColor grayColor] CGColor]];
    [tableParams.layer setBorderWidth: 1.0];
    [tableParams.layer setCornerRadius:8.0f];
    [tableParams.layer setMasksToBounds:YES];
    
    
    [textParam.layer setBackgroundColor: [[UIColor whiteColor] CGColor]];
    [textParam.layer setBorderColor: [[UIColor grayColor] CGColor]];
    [textParam.layer setBorderWidth: 1.0];
    [textParam.layer setCornerRadius:8.0f];
    [textParam.layer setMasksToBounds:YES];
    textParam.autocorrectionType = UITextAutocorrectionTypeNo;
    textParam.autocapitalizationType = UITextAutocapitalizationTypeNone;
    
    CEFirstViewController * projectControl = [ CEFirstViewController getInstance];
    if ( (projectControl!= nil) && (projectControl.paramsArray!= nil) )
    {
        paramsArray =  [NSMutableArray arrayWithArray:projectControl.paramsArray];
    }
    else
    {
        paramsArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    iParamsSelected = -1;
    [tableParams reloadData];
    textParam.text=@"";
    instance = self;
    
    

}

-(void)viewWillDisappear:(BOOL)animated
{
    if ( iParamsSelected != -1 )
    {
        NSString * text = ((CustomXmlDictionaryItem *)[paramsArray objectAtIndex:iParamsSelected])->Value;
        if ( ![text isEqualToString:textParam.text] )
        {
            ((CustomXmlDictionaryItem *)[paramsArray objectAtIndex:iParamsSelected])->Value = textParam.text;
        }
    }
}



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

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [textParam resignFirstResponder];
    
    
    
}


- (void)reloadUI
{
    iParamsSelected = -1;
    [tableParams reloadData];
    textParam.text=@"";
}



//Set the number of sections into the table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Set the number of lines for the table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([tableView isEqual:tableParams])
        return [paramsArray count];
    
    return 0;
}

// Set Cell Text
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSUInteger row = [indexPath row];
	static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell;
    
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    if ([tableView isEqual:tableParams])
    {
        

        
        //NSString *eaAccessoryName = [NSString stringWithFormat:@"%@ (%@)",                                     [[projectsArray objectAtIndex:row] name], [[projectsArray objectAtIndex:row] serialNumber]];
        CustomXmlDictionaryItem *xmlitem = [paramsArray objectAtIndex:row];
        
        // Configure the cell...
        cell.textLabel.text = xmlitem->Tag;
        cell.imageView.image = nil;

        
        
        if ( iParamsSelected != -1 )
        {
            //If a device is selected
            if (iParamsSelected == row)
            {
                cell.imageView.image = [UIImage imageNamed:@"ball.png"];
            }
        }
        
    }
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    

    CustomXmlDictionaryItem *xmlitem;
    //NSError *error;
    //CEFirstViewController* mainView = [CEFirstViewController getInstance];
    
    // [tableProjects reloadData];
    
    
    NSUInteger row = [indexPath row];
    
    if ([tableView isEqual:tableParams])
    {
        if ( iParamsSelected != -1 )
        {
            NSString * text = ((CustomXmlDictionaryItem *)[paramsArray objectAtIndex:iParamsSelected])->Value;
            if ( ![text isEqualToString:textParam.text] )
            {
                ((CustomXmlDictionaryItem *)[paramsArray objectAtIndex:iParamsSelected])->Value = textParam.text;
            }
        }
        
        iParamsSelected = row;
        xmlitem =  [paramsArray objectAtIndex:row];
        textParam.text = xmlitem->Value;
    }
    
    [tableParams reloadData];

}




@end
