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

#import "TestView.h"
#import "TestModal.h"
#include "OmCoreGraphicsUtils.h"
//#include "misc_test_appViewController.h"
#import "TestCamOverlay.h"
#include "OmUiUtils.h"
#include "OmController.h"

#import <MessageUI/MessageUI.h>

@implementation TestView

enum
{
    B_BUTTON1 = 1,
    B_BUTTON2,
    B_BUTTON3,
    B_BUTTON4,
    B_BUTTON5,
    B_BUTTON6,
    B_BUTTON7,
    B_BUTTON8,
    B_BUTTON9,
};


static UIButton *addButton(UIView *viewey,NSString *s,NSInteger tag,int x,int y,int w,int h)
{
    UIButton *bt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [viewey addSubview:bt];
    [bt setTitle:s forState:UIControlStateNormal];
    [bt setFrame:CGRectMake(x,y,w,h)];
    [bt addTarget:viewey action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [bt setTag:tag];
    return bt;
}


- (void) snap
{
    // no camera? just go home.
    //if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    //    return;
    
    UIImagePickerController *im = [[UIImagePickerController alloc] init];
    im.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    im.delegate = self;
    im.allowsEditing = NO;
    [controller presentModalViewController:im animated:YES];
}

- (void) buttonAction:(UIButton *)sender
{
    frameNumber++;
    
    int tag = [sender tag];
    if(tag == B_BUTTON1)
    {
        frameNumber++;
        [self setNeedsDisplayInRect:[self bounds]];

        TestModal *mv = [TestModal alloc];
        [OmController animateViewSlide:mv parentController:controller];

//        OmController *vc = [[OmController alloc] init];
//        
//        [mv setController:controller];
//        
//        [vc attachView:mv];
//        
//        vc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
//        [controller presentModalViewController:vc animated:YES];
    }
    if(tag == B_BUTTON2)
    {
        [self snap];
    }
    if(tag == B_BUTTON3)
    {
        TestCamOverlay *mv = [TestCamOverlay alloc];
        OmController *vc = [OmController animateViewFlip:mv parentController:controller];
//        OmController *vc = [[OmController alloc] init];
//        [mv setController:controller];
//        [vc attachView:mv];
        //vc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [controller presentModalViewController:vc animated:YES];
    }
    if(tag == B_BUTTON4)
    {
        UIAlertView *v = OmAlertAskNoYes(self, "Yes or no?", @"No!", @"Yes!", "Click yes, or no...");
    }
    
    if(tag == B_BUTTON5)
    {
        UIActionSheet *v =OmActionAskNoYes(self,@"cancel",@"delete","Delete this thing?");    
    }

    if(tag == B_BUTTON6)
    {
        /* attach the xml */
        NSData *attachmentData = [@"<a> hi there </a>" dataUsingEncoding:NSASCIIStringEncoding];

        MFMailComposeViewController *k = [MFMailComposeViewController alloc];
        [k init];
        [k setMessageBody:@"xml goes here" isHTML:NO];
        [k setMailComposeDelegate:self];
        [k addAttachmentData:attachmentData mimeType:@"text/xml" fileName:@"data.xml"];
        [self->controller presentModalViewController:k animated:YES];
    }

    

}    


- (id)initWithFrame:(CGRect)frame 
{
    if ((self = [super initWithFrame:frame]))
    {
        [self addButton:@"b1" tag:B_BUTTON1 x:20 y:20 w:80 h:40];
        addButton(self,@"b2",B_BUTTON2,130,20,80,40);
        addButton(self,@"b3",B_BUTTON3,220,20,80,40);
        addButton(self,@"b4",B_BUTTON4, 20,80,80,40);
        addButton(self,@"b5",B_BUTTON5,120,80,80,40);
        addButton(self,@"b6",B_BUTTON6,220,80,80,40);
        addButton(self,@"b7",B_BUTTON7, 20,140,80,40);
        addButton(self,@"b8",B_BUTTON8,120,140,80,40);
        UIButton *b9 = addButton(self,nil,B_BUTTON9,220,140,80,40);
        UIImage *hammer = [UIImage imageNamed:@"hammer2.png"];
        [b9 setImage:hammer forState:UIControlStateNormal];
        
        UITextField *tf = [[UITextField alloc] init];
        [tf setFrame:CGRectMake(20,200,200,20)];
        
        tf.placeholder = @"(what is this place?)";
        
        tf.textColor = GColor(0xffff00);
        tf.backgroundColor = GColor(0x202020);
        tf.autocorrectionType = UITextAutocorrectionTypeNo;
        tf.returnKeyType = UIReturnKeyDone;
        [self addSubview:tf];
        tf.delegate = self;
        
        self->label = [[UILabel alloc] init];
        [self->label setFrame:CGRectMake(20,220,200,20)];
        self->label.text = @"tbd";
        [self addSubview:self->label];
    }
    return self;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    self->label.text = escapeForHtml(textField.text);
    return YES;
}


- (void)drawRect:(CGRect)rect
{
    CGContextRef g = UIGraphicsGetCurrentContext();
    
    /* text needs this :( */
    CGAffineTransform tt = CGAffineTransformMake(1.0,0.0, 0.0, -1.0, 0.0, 0.0);
    CGContextSetTextMatrix(g, tt);

    
    GFillRectR(g,[self bounds],0x400000);
    GFillRectR(g,CGRectMake((frameNumber % 32) * 10,0,10,5),0x40e010);
    
    GSetFillColor(g, 0xffffff);
    GSetFont(g,3,24);
    
    GPrintf(g, 10, 400, "alert:%d",self->alertButton);
    GPrintf(g, 10, 420, "action:%d",self->actionSheetButton);
    
    {
        // count the screens, show em
        int screenCount = [[UIScreen screens]count];
        GSetFont(g,3,11);
        GPrintf(g,10,430, "screens: %d",screenCount);
        for(int i = 0; i < screenCount; i++)
        {
            UIScreen *screen = [[UIScreen screens] objectAtIndex:i];
            CGRect r = [screen bounds];
            GPrintf(g,10,440 + i * 10,"%d: %d x %d",i,r.size.width,r.size.height);
        }
    }
    
}

-(void)setController:(UIViewController *)aController
{
    self->controller = aController;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    frameNumber++;
    [self setNeedsDisplayInRect:[self bounds]];
        
    TestModal *mv = [TestModal alloc];
    
    OmController *oc = [OmController animateViewFlip:mv parentController:controller];
//    OmController *vc = [[OmController alloc] init];
//    OmController *vc = [OmController allocAndInitWithOmView:mv];
//    
//    [mv setController:controller];
//    
//    [vc attachView:mv];
//    
//    vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
//    [controller presentModalViewController:vc animated:YES];
}


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



// for handling alert dialogs
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    self->alertButton = buttonIndex;
    [self setNeedsDisplayInRect:[self bounds]];
}

// for handling alert dialogs
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    self->actionSheetButton = buttonIndex;
    [self setNeedsDisplayInRect:[self bounds]];
}



@end
