//
//  TestModal.m
//  misc_test_app
//
//  Created by david van brink on 7/26/10.
//  Copyright 2010 omino.com. All rights reserved.
//

#import "TestModal.h"
#include "OmCoreGraphicsUtils.h"
#include "OmXmlHelper.h"
#include <CoreFoundation/CoreFoundation.h>

@implementation TestModal


-(void)subviewDrawing:(OmView *)view
{
    CGRect rect = view.bounds;
    rect.origin.x += 10;
    rect.origin.y += 10;
    rect.size.width -= 20;
    rect.size.height -= 20;
    CGContextRef g = UIGraphicsGetCurrentContext();
    GFillRectR(g, rect, 0xff0000);
}

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        
        
        // add a subview
        OmView *subview = [OmView alloc];
        [subview initWithFrame:CGRectMake(10,350,100,100)];
        [subview setBackgroundColor:GColor(0x00ffff)];
        [self addSubview:subview];
        
//        [subview delegateDrawing:self selector:@selector(subviewDrawing)];
    

        // Initialization code
    }
    return self;
}


static void printPathBroken(CGContextRef g,int *printTextYP,char *path)
{
    int x = 5;
    while(*path)
    {
        CGPoint p = GPrintf(g,x,*printTextYP,"%c",*path);
        x = p.x;
        if(*path == '/')
        {
            *printTextYP += 14;
            x = 10;
        }
        path++;
    }
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect 
{
    int printTextY; // tracking window y-pos
    CGContextRef g = UIGraphicsGetCurrentContext();
    CGAffineTransform tt = CGAffineTransformMake(1.0,0.0, 0.0, -1.0, 0.0, 0.0);
    CGContextSetTextMatrix(g, tt);

    GFillRectR(g,rect,0xff0000);
    
    printTextY = 0;
    GSetFont(g,2,12);
    GSetFillColor(g, 0);
    
    NSFileManager *filemgr;
    NSString *currentpath;
    
    filemgr = [[NSFileManager alloc] init];

    char s[500];
    
    
    // My sandbox location is "NSHomeDirectory/Documents?
    currentpath = NSHomeDirectory();
    
    currentpath = [currentpath stringByAppendingString:@"/Documents/"];
    printTextY += 14;
    CFStringGetCString((CFStringRef)currentpath, s, sizeof(s),kCFStringEncodingASCII);
    
    printPathBroken(g,&printTextY,s);
//    GPrintf(g, 5, printTextY, "current: %s",s);
    
    NSData *newFileContents = [NSData alloc];

    NSString *newFilePath = [currentpath stringByAppendingString:@"omloc_data.txt"];
    [filemgr createFileAtPath:newFilePath contents:newFileContents attributes:NULL];

#define NSSTRING_TO_C(_nsstring,_charptr) char _charptr[402]; [_nsstring getCString:_charptr maxLength:400 encoding:0];
    
    {
        NSString *newFilePath = [currentpath stringByAppendingString:@"omloc_stuff.xml"];
        NSSTRING_TO_C(newFilePath,newFilePathS);
        

        // for fun, let us write an xml file?
        NSDate *d = [NSDate date];
        char dateS[512];
        [[d description] getCString:dateS maxLength:400 encoding:0];
        OmXmlHelper *x = [OmXmlHelper alloc];
        [x autorelease];
        [x beginWriting:newFilePathS comment:"omino xml" outerElement:"Foo"];
        [x beginElement:"date"];
        [x addAttribute:"a" value:"1"];
        [x addText:dateS];
        [x endElement];
        [x beginElement:"A"];
        [x beginElement:"B"];
        [x beginElement:"C"];
        [x endElement];
        [x beginElement:"D"];
        [x addAttribute:"d" value:"4"];
        [x endElement];
        [x addText:"in b"];
        [x close];
    }
    
    NSArray *filelist = [filemgr contentsOfDirectoryAtPath:currentpath error:NULL];
    
    int count = [filelist count];
    
    for (int i = 0; i < count; i++)
    {
        NSString *f = [filelist objectAtIndex:i];
        CFStringGetCString((CFStringRef)f, s, sizeof(s),kCFStringEncodingASCII);
        printTextY += 13;
        GPrintf(g,10,printTextY,"%s",s);
    }
    
    printTextY += 13;
    GPrintf(g,20,printTextY,"hi again");
    printTextY += 13;
    GPrintf(g,20,printTextY,"%d screens",[[UIScreen screens] count]);
}



- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    // ask our controller to do modal
    [self dismiss];
    
}





- (void)dealloc {
    [super dealloc];
}


@end
