#import "EnhancedText.h"
#import "EmacsText.h"
#import "TokenString.h"
#import "MailController.h"
#import "MailMessage.h"
#import "MailFromFile.h"
#import "NeXTMail.h"
#import "UnixScript.h"
#import "MSMail.h"
#import "MIMEMail.h"
#import "FileShelfView.h"

@implementation MailController

- init
{
   [super init];
   [self setDocType: "send"];
   MUA = [[StringStorage alloc] init: "MailEnclosure (0.01)"];
   MTA = [[StringStorage alloc] init: "/usr/lib/sendmail"];
   return self;
}

- free
{
   [MUA free];
   [MTA free];
   return [super free];
}
   
- awakeFromNib
{
   [self empty];
   [[shelf window] makeKeyAndOrderFront: self];
   [ccField setNextText: text];		     /* I have to do this here since setText broke connections */
   return self;
}

- empty
{
   [self selectType: [typeList itemList]];
   [toField setStringValue: ""];
   [ccField setStringValue: ""];
   [subjectField setStringValue: ""];
   [text empty: self];
   [shelf empty];
   [toField selectText: self];
   return self;
}

- deliver:sender
{
   id mailMessage = nil;

   switch([[[typeList itemList] selectedCell] tag])
   {
    case 0:				     /* plain text */
      mailMessage = [[MailFromFile alloc] initForMUA: [MUA stringValue] andMTA: [MTA stringValue]];
      break;
    case 1:				     /* Unix script */
      mailMessage = [[UnixScript alloc] initForMUA: [MUA stringValue] andMTA: [MTA stringValue]];
      break;
    case 2:				     /* MSMail */
      mailMessage = [[MSMail alloc] initForMUA: [MUA stringValue] andMTA: [MTA stringValue]];
      break;
    case 3:				     /* NeXTMail */
      mailMessage = [[NeXTMail alloc] initForMUA: [MUA stringValue] andMTA: [MTA stringValue]];
      break;
    case 4:				     /* MIME */
      mailMessage = [[MIMEMail alloc] initForMUA: [MUA stringValue] andMTA: [MTA stringValue]];
      break;
    default:
      break;
   }

   if(!mailMessage)
   {
      NXLogError("Unsupported type\n");
      return nil;
   }

   if([mailMessage supportsAttachments])
       [mailMessage setAttachments: [shelf fileNames]];

   [mailMessage deliverTo: [toField stringValue]
                  subject: [subjectField stringValue]
                       cc: [ccField stringValue]
                     text: [text stringValue]];

   [panel orderOut: self];
   [self empty];
   [mailMessage free];
   return self;
}

- selectType: sender
{
   int tag = [[sender selectedCell] tag];

   NXLogError("PopUp tag: %d", tag);
   switch(tag)
   {
    case 0:
      [shelf setEnabled: NO];
      break;
    default:
      [shelf setEnabled: YES];
      break;
   }
   return self;
}

- setTypeList: anObject
{
					     /* select first item in matrix and on button */
   typeList = [anObject target];
   [typeList setTarget: self];
   [typeList setAction: @selector(selectType:)];
   [[typeList itemList] selectCellWithTag: 0];
   [anObject setTitle: [[[typeList itemList] selectedCell] title]];
   return self;
}

/* I need to place an EmacsText in a ScrollView. But Grouping in IB
 * creates one that can not be resized.  So I just swap out one
 * from a ScrollView/Text pulled right from the IB Palette.
 */
- setText: anObject
{
   id origText = [anObject docView];	     /* get the TeXT IB put in the scroller */
   id newText;
   NXRect theRect;

   [origText getFrame: &theRect];	     /* get its frame */
					     /* create an EmacsText using that Rect */
   newText = [[EmacsText alloc] initFrame: &theRect];

   [anObject setDocView: newText];	     /* set it as the scrollers docView */
   text = newText;			     /* get the pointer for later use */
   [origText free];			     /* free the one IB put in there. */
   return self;
}

@end

