
package GT::Mail::Editor::Text;

use vars qw/$ERROR_MESSAGE/;
use strict;
use bases 'GT::Mail::Editor' => '';

$ERROR_MESSAGE = 'GT::Mail::Editor';

sub display {
# ----------------------------------------------------------------
    my ( $self, $tags ) = @_;
    my $page = $self->{text_tpl_name};

    if ( $self->{fields}{page} and $self->{fields}{page} =~ /^(?:editor|email)_/ ) {
        $page = $self->{fields}{page};
    }
    my $ret = $self->print_page( $page, $tags );
    $self->{displayed} = 1;
    return $ret;
}

sub message_from_input {
# ----------------------------------------------------------------
    my ( $self ) = @_;
    $self->set_headers;

# If we have a part ID, this isn't a new text part
    my ( $part, $id );
    $part = $self->{part};
    $part->set( 'content-type' => 'text/plain; charset="'.( $self->{fields}{charset} || 'US-ASCII' ).'"' );
    if ( exists( $self->{fields}{msg} ) ) {
        $part->body_data( $self->{fields}{msg} );
    }
}

sub munge_message {
# ----------------------------------------------------------------
    my ( $self ) = @_;
    
    my $root_part = $self->{message}->root_part;

# Simple case if the message is not multipart
    my ( $text_part, $html_part, $related_part, $alt_part );
    if ( !$root_part->is_multipart ) {
        $text_part = $root_part;
    }

# We have a multipart. First thing we do is look for an alternative part
# to use.
    else {
    
# First we look for the proper alternative mime parts
        $alt_part = ($self->{message}->find_multipart( 'alternative' ))[0];
        if ( $alt_part ) {
            my @alt_parts = $alt_part->parts;
            for ( @alt_parts ) {
                if ( $_->content_type eq 'text/plain' ) {
                    $text_part = $self->{message}->delete_part( $_ );
                }
                elsif ( $_->content_type eq 'text/html' ) {
                    $html_part = $self->{message}->delete_part( $_ );
                }
            }
            if ( !$text_part and $html_part ) {
                $text_part = $self->{message}->new_part(
                    'content-type' => 'text/plain',
                    -body_data     => $self->html_to_text( $html_part->body_data )
                );
            }
            elsif ( !$text_part ) {
                $text_part = $self->{message}->new_part(
                    'content-type' => 'text/plain',
                    -body_data     => ''
                );
            }

# Make anything we can not view an attachment
            $self->{message}->move_parts_last(
                $root_part,
                map {
                    unless ( $_->is_multipart ) {
                        $_->set( 'content-disposition' => 'attachment' );
                    }
                    $_;
                } $alt_part->parts
            );

            if ( $alt_part == $root_part ) {
                $alt_part->set( 'content-type' => 'multipart/mixed' );
            }
            else {
                $self->{message}->delete_part( $alt_part );
            }
            $self->{message}->add_parts_start( $self->{message}->root_part, $text_part );
        }
        else {

# Else we can just stick the text part at the beginning
            for my $part ( $self->{message}->all_parts ) {
                my $disp = $part->mime_attr( 'content-disposition' );
                next if $disp and $disp eq 'attachment';
                if ( $part->content_type eq 'text/plain' ) {
                    $text_part = $self->{message}->delete_part( $part );
                }
                elsif ( $part->content_type eq 'text/html' ) {
                    $html_part = $self->{message}->delete_part( $part );
                }
            }
            if ( !$text_part and $html_part ) {
                $text_part = $self->{message}->new_part(
                    'content-type' => 'text/plain',
                    -body_data     => $self->html_to_text( $html_part->body_data )
                );
            }
            elsif ( !$text_part ) {
                $text_part = $self->{message}->new_part(
                    'content-type' => 'text/plain',
                    -body_data     => ''
                );
            }
            $self->{message}->add_parts_start( $self->{message}->root_part, $text_part );
        }
    }
    my $parent = $self->{message}->parent_part( $text_part );
    if ( $parent and $parent->content_type eq 'multipart/related' ) {
        $parent->set( 'content-type' => 'multipart/mixed' );
    }
    $self->fix_alt_parts;
    $self->fix_related_parts;
    $self->delete_empty_multiparts;
    $self->find_attachments( $text_part );

    if ( @{[$self->{message}->all_parts]} == 1 and $self->{message}->root_part->is_multipart ) {
        $self->{message}->delete_part( $text_part );
        my $root_part = $self->{message}->root_part;
        $root_part->set( 'content-type' => 'text/plain' );
        $root_part->body_data( $text_part->body_data );
    }
    $self->{part} = $text_part;
}

sub html_part { return }
sub text_part { return shift()->{part} }

1;

