//
//  OmPrefs.m
//  OmLoc
//
//  Created by david van brink on 8/13/10.
//  Copyright 2010 omino.com. All rights reserved.
//

#import "OmPrefs.h"
#import "OmCoreGraphicsUtils.h"
#import "OmLocColors.h"
#import "OmUiUtils.h"


#define BW 60
#define BH 40
#define PR_MARGIN 18
#define RT 320
#define BT 480

#define PR_MAPSELECT_TOP 70
#define PR_MAPSELECT_HEIGHT 20
#define PR_MAPSELECT_LEFT PR_MARGIN
#define PR_MAPSELECT_BUTTONS_TOP (PR_MAPSELECT_TOP + PR_MAPSELECT_HEIGHT + PR_MARGIN)
#define PR_MAPSELECT_BUTTONS_LEFT (PR_MARGIN)

#define PR_LINE1_Y (PR_MAPSELECT_BUTTONS_TOP + BH + PR_MARGIN)
#define PR_MAIL_TOP (PR_LINE1_Y + PR_MARGIN)
#define PR_MAIL_LEFT PR_MARGIN
#define PR_MAIL_DESC_TOP (PR_MAIL_TOP + BH * 3 / 4)
#define PR_MAIL_DESC_LEFT (PR_MAIL_LEFT + BW + PR_MARGIN)

#define PR_LINE2_Y (PR_DELETE_ALL_TOP - PR_MARGIN)
#define PR_DELETE_ALL_TOP (BT - BH - PR_MARGIN)
#define PR_DELETE_ALL_WIDTH (BW * 2)
#define PR_DELETE_ALL_LEFT PR_MARGIN

enum  {
    B_OK = 1,
    B_MAP_1,
    B_MAP_2,
    B_MAP_3,
    
    B_DELETE_MAP,
    
    B_GPS,
    B_EMAIL_MAP,
    B_EMAIL_AUTHOR
};

@implementation OmPrefs


- (id)initWithFrame:(CGRect)frame 
{
    if ((self = [super initWithFrame:frame])) 
    {
        CGRect r = CGRectMake(RT - BW - PR_MARGIN,BT - BH - PR_MARGIN,BW,BH);
        [self addButton:@"DONE" tag:B_OK rect:r];
        
        r = CGRectMake(PR_MAPSELECT_BUTTONS_LEFT, PR_MAPSELECT_BUTTONS_TOP, BW, BH);
        [self addButton:@"map 1" tag:B_MAP_1 rect:r];
        r.origin.x += BW + PR_MARGIN;
        [self addButton:@"map 2" tag:B_MAP_2 rect:r];
        r.origin.x += BW + PR_MARGIN;
        [self addButton:@"map 3" tag:B_MAP_3 rect:r];
        
        r = CGRectMake(PR_MAIL_LEFT,PR_MAIL_TOP,BW,BH);
        [self addButton:@"mail" tag:B_EMAIL_MAP rect:r];

        r.origin.y += BH + PR_MARGIN;
        [self addButton:@"gps" tag:B_GPS rect:r];

        r.origin.y += BH + PR_MARGIN;
        [self addButton:@"hey" tag:B_EMAIL_AUTHOR rect:r];

        r = CGRectMake(PR_DELETE_ALL_LEFT, PR_DELETE_ALL_TOP, PR_DELETE_ALL_WIDTH, BH);
        UIButton *delB = [self addButton:@"Delete Map" tag:B_DELETE_MAP rect:r];
        [delB setTitleColor:GColor(CO_DANGEROUS_BUTTON) forState:UIControlStateNormal];
    }
    return self;
}

- (void)setPlaceList:(OmPlacesList *)thePlaceList lat:(float)theLat lon:(float)theLon acc:(float)theAcc;
{
    self->placeList = thePlaceList;
    self->lat = theLat;
    self->lon = theLon;
    self->acc = theAcc;
}


void divBar(CGContextRef g,int y)
{
    CGContextMoveToPoint(g, PR_MARGIN, y);
    CGContextAddLineToPoint(g, RT - PR_MARGIN, y);
    CGContextStrokePath(g);
}

- (void)drawRect:(CGRect)rect 
{
    CGContextRef g = UIGraphicsGetCurrentContext();
    G(g);
    GFillRectR(g, [self bounds], CO_ONE_MINUTE_CIRCLE);
    GSetFillColor(g,CO_LABEL_TEXT);
    int fsize = 15;
    GSetFont(g,1,fsize);
    char *mapName = [self->placeList getMapName];
    char mapChar = mapName[strlen(mapName) - 1];
    GPrintf(g, PR_MAPSELECT_LEFT, PR_MAPSELECT_TOP + PR_MAPSELECT_HEIGHT, "Currently using map %c with %d places",mapChar,[self->placeList getWayPointCount]);

    int x = PR_MAIL_DESC_LEFT;
    int y = PR_MAIL_DESC_TOP;
    GPrintf(g,x,y,"Send data & photos by email");
    y += BH + PR_MARGIN;
    GPrintf(g,x,y,"Check latitude & longitude");
    y += BH + PR_MARGIN;
    GPrintf(g,x,y,"Email me, the app author");
    
    divBar(g, PR_LINE1_Y);
    divBar(g, PR_LINE2_Y);
}


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


- (void) buttonAction:(UIButton *)sender
{
    int tag = [sender tag];
    if(tag == B_OK)
    {
        [self dismiss];
    }
    if(tag == B_MAP_1 || tag == B_MAP_2 || tag == B_MAP_3)
    {
        if(tag == B_MAP_1)
            [self->placeList loadMap:MAP_NAME_1];
        if(tag == B_MAP_2)
            [self->placeList loadMap:MAP_NAME_2];
        if(tag == B_MAP_3)
            [self->placeList loadMap:MAP_NAME_3];
        [self repaint];
    }
    if(tag == B_DELETE_MAP)
    {
        self->deleteCurrentMap = OmActionAskNoYes(self, @"Cancel", @"Delete",
                                                  "Permanently delete \"%s\" with %d places?",
                                                  [self->placeList getMapName],
                                                  [self->placeList getWayPointCount]);
    }
    if(tag == B_EMAIL_MAP)
    {
        /* attach the xml */
        NSString *mapFilePath = [self->placeList getMapFilePath];
        NSData *attachmentData = [NSData dataWithContentsOfFile:mapFilePath];
        
        NSMutableString *body = [NSMutableString stringWithCapacity:100];
        //[body initWithString:@""];
        [body appendString:@"map data\n"];
        
        MFMailComposeViewController *k = [MFMailComposeViewController alloc];
        [k init];
        [k setSubject:@"Map Data from iPhone"];
        [k setMailComposeDelegate:self];
        [k addAttachmentData:attachmentData mimeType:@"text/xml" fileName:@"map.xml"];
        
        for(int i = 0; i < [self->placeList getWayPointCount]; i++)
        {
            NSString *imageFilePath = [self->placeList getImageFilePath:i];
            OmWayPoint *p = [self->placeList getWayPoint:i];
            if(imageFilePath)
            {
                [k addAttachmentData:[NSData dataWithContentsOfFile:imageFilePath] mimeType:@"image/jpg" fileName:p->imageName];
#ifdef DEBUG
                [body appendString:imageFilePath];
                [body appendString:@"\n"];
#endif
            }
        }
        
        [k setMessageBody:body isHTML:NO];
        
        [self->controller presentModalViewController:k animated:YES];
    }

    if(tag == B_GPS)
    {
        OmAlertf("gpsish","lat:%.4f lon:%.4f\nacc:%.4f ",
                 self->lat,
                 self->lon,
                 self->acc
                 );
    }
    if(tag == B_EMAIL_AUTHOR)
    {    
        MFMailComposeViewController *k = [MFMailComposeViewController alloc];
        [k init];
        [k setSubject:@"Omplace"];
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:1];
        [array addObject:@"someplace@omino.com"];
        [k setToRecipients:array];
        [k setMessageBody:[NSString stringWithFormat:@"So there I was, using your app at latitude %.4f longitude %.4f, and I was thinking...",self->lat,self->lon] isHTML:NO];
        [k setMailComposeDelegate:self];
        [self->controller presentModalViewController:k animated:YES];
    }

}


// for handling modal dialogs
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // 0 means destruct, 1 meant cancel, for an action sheet
    if(actionSheet == self->deleteCurrentMap)
    {
        if(buttonIndex == 0) // yes!
        {
            [self->placeList clearAll];
            [self->placeList saveToFile];
            [self dismiss];
        }
        self->deleteCurrentMap = nil;
    }
}

@end
