#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import <UIKit/CDStructures.h>
#import <UIKit/UIPushButton.h>
#import <UIKit/UIThreePartButton.h>
#import <UIKit/UINavigationBar.h>
#import <UIKit/UIWindow.h>
#import <UIKit/UIView-Hierarchy.h>
#import <UIKit/UIHardware.h>
#import <UIKit/UITable.h>
#import <UIKit/UITableCell.h>
#import <UIKit/UITableColumn.h>
#import <UIKit/UITextView.h>
#import <UIKit/UISegmentedControl.h>
#import "HelloApplication.h"


@implementation HelloApplication

- (BOOL)respondsToSelector:(SEL)selector {
NSLog(@"respondsToSelector: %s", selector);
return [super respondsToSelector:selector];
}

- (int) numberOfRowsInTable: (UITable *)table
{
return [fileArray count];
}

- (UITableCell *) table: (UITable *)table cellForRow: (int)row column: (int)col
{
return [fileArray objectAtIndex: row];
}

- (UITableCell *) table: (UITable *)table cellForRow: (int)row column: (int)col
reusing: (BOOL) reusing
{
return pbCell;
}

- (void) segmentedControl: (UISegmentedControl *)segment selectedSegmentChanged: (int)seg
{
NSLog(@"Selected segement: %i", seg);
}

- (void) applicationDidFinishLaunching: (id) unused
{

NSString *imagePath = @"/var/root/Media/DCIM/100APPLE/";
NSArray *myPhotos = [[NSFileManager defaultManager]directoryContentsAtPath: imagePath];

UIWindow *window;

window = [[UIWindow alloc] initWithContentRect: [UIHardware fullScreenApplicationContentRect]];

struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
rect.origin.x = rect.origin.y = 0.0f;

UIView *mainView;
mainView = [[UIView alloc] initWithFrame: rect];

// UIImage *fun = [[UIImage alloc]initWithContentsOfFile:@"/var/root/Media/DCIM/100APPLE/IMG_0063.JPG"];
// UIImageView *funView = [[UIImageView alloc] initWithFrame: rect];
// [funView setImage:fun];
// [funView setAlpha:1.0];

float segWidth = 200.0f;

UISegmentedControl *modeSwitch = [[UISegmentedControl alloc] initWithFrame:CGRectMake(320.0f / 2.0f - (segWidth / 2.0f), 6.0f, segWidth, 30.0f) withStyle:2 withItems:nil];
[modeSwitch insertSegment:0 withTitle:@"Mine" animated:NO];
[modeSwitch insertSegment:1 withTitle:@"Flickr" animated:NO];
[modeSwitch insertSegment:2 withTitle:@"Popular" animated:NO];
// [modeSwitch insertSegment:0 withTitle:@"Install" animated:NO];
// [modeSwitch insertSegment:1 withTitle:@"Update" animated:NO];
// [modeSwitch insertSegment:2 withTitle:@"Uninstall" animated:NO];
// [modeSwitch insertSegment:3 withTitle:@"Sources" animated:NO];
[modeSwitch selectSegment:0];
[modeSwitch setDelegate:self];

UINavigationBar *bar = [[UINavigationBar alloc]initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 43.0f)];

UITextView *textView = [[UITextView alloc]initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
[textView setEditable:YES];
[textView setTextSize:14];

NSEnumerator *enumerator = [myPhotos objectEnumerator];
id anObject;
NSDictionary *file;

while (anObject = [enumerator nextObject]) {

NSRange match = [anObject rangeOfString:@"IMG_0063"];
int length = match.length;
if (length > 0) {
NSLog(@"Yes");
}
else {
NSLog(@"NO");
}
NSString *filePath = [NSString localizedStringWithFormat:@"%@%@", imagePath, anObject];
file = [[NSFileManager defaultManager] fileAttributesAtPath: filePath traverseLink:NO];

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:
anObject, @"name",
[file objectForKey:@"NSFileModificationDate"], @"date",
nil];
NSLog(@"object: %@", dict);

}

pbCell = [[UIImageAndTextTableCell alloc] init];
[pbCell setTitle: @"Hello world!\n"];
[pbCell setImage: [[UIImage alloc] initWithContentsOfFile:@"/var/root/Media/DCIM/100APPLE/IMG_0063.THM"]];

// UIPushButton *button = [[UIThreePartButton alloc] initWithTitle:
// @"Touch Me"];
// buttonCell = [[UITableCell alloc] init];
// [buttonCell addSubview: button];
// [button sizeToFit];


UITable *table = [[UITable alloc] initWithFrame: CGRectMake(0.0f, 48.0f, 320.0f, 480.0f - 16.0f - 32.0f)];
UITableColumn *col = [[UITableColumn alloc] initWithTitle: @"HelloApp" identifier: @"hello" width: 320.0f];

[table addTableColumn: col];
[table setDataSource: self];
[table setDelegate: self];
[table reloadData];


[window orderFront: self];
[window makeKey: self];
[window _setHidden: NO];

[window setContentView: mainView];

[mainView addSubview: textView];

[mainView addSubview: table];
[mainView addSubview: bar];
[mainView addSubview: modeSwitch];

}

@end