/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is mozilla.org Code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 2001
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Adam Lock <adamlock@netscape.com>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include "global.h"

#include "MailFrame.h"
#include "GeckoProtocolHandler.h"
#include "nsEmbedString.h"

#include "nsIURI.h"

struct MailMsg
{
    char *mSubject;
    char *mSender;
    char *mDate;
    char *mBody;
};

MailMsg gSampleMessages[] = 
{
    {
        "URGENT ASSITANCE PLS",
        "alexander nene",
        "09/06/2003 14:38",
        "<html><body>"
        "<p>Attention: Sir,<br>\n"
        "<p>Good day. I am ALEXANDER NENE, Solicitor and Notary Public, The Personal Attorney"
        "to MR HENRI CARLTON, The president of DIAMOND SAFARIESCO.LTD.ACCRA-GHANA who is a"
        "National of your country. On the 21st of April 2000, my client, his wife and their "
        "only son were Involved in a car accident along ACCRA/KUMASI Express Road."
        "<p>Unfortunately, they all lost their lives in the event of the accident, since then "
        "I have made several enquiries to locate any of my clients extended relatives and"
        "this has also proved unsuccessful.After these several unsuccessful attempts, I decided"
        "to trace his Relatives over the Internet, to locate any member of his family but of"
        "no avail, hence I contacted youI contacted you to assist in repatriating the money and"
        "property left Behind before they get confiscated or declare unserviceable by the bank"
        "where my client lodged this huge deposits. Particularly, the Bank where the deceased had"
        "an account valued at about 28.3 million dollars.Conseqently, the bank issued me a notice "
        "to provide the next of kin or Have the account confiscated within a short time. Since "
        "I have been Unsuccessful in locating the relatives for over some years now, I seek your "
        "consent to present you as the next of kin of the deceased since you share the same surname "
        "so that the proceeds of this account valued at 48.3 million dollars can be paid to you "
        "for both of us to share the money; 70% to me and 25% to you, while 5% should be for "
        "expenses or tax as your government may require. I have all necessary legal documents that"
        "can be used to backup the claim.All I require is your honest cooperation to enable us see"
        "this deal Through. "
        "<p>I guarantee that this will be executed under a legitimate arrangement"
        "that will protect you from any breach of the law. Please get in touch with me through my"
        "email to enable us discuss further.<br><br> \n"
        "<p>Thanks for you kind co-operation<br><br> \n"
        "<p>ALEXANDER NENE<br><br><br></body></html>"
    },
    {
        "Reminder: Network Outage Tonight",
        "IT Dept",
        "09/06/2003 15:22",
        "<html><body>The network will be going down tonight so please log off "
        "your computers before going home.</body></html>"
    },
    {
        "Expense Reports Due",
        "Finance Dept",
        "09/06/2003 15:40",
        "<html><body>Please submit expense reports ASAP if you want to see payment "
        "in your accounts this month!</body></html>"
    },
    {
        "Flight confirmation",
        "Expediocity",
        "12/06/2003 12:31",
        "<html><body>Thank you for booking with Expediocity, Your flight reservation has been "
        "confirmed and details may be found online <a href=\"http://www.mozilla.org\">here</a>.</body></html>"
    }
};

static bool gMailChannelCallbackRegistered = FALSE;

class MailChannelCallback : public GeckoChannelCallback
{
public:
    virtual nsresult GetData(
        nsIURI *aURI,
        nsIChannel *aChannel,
        nsACString &aContentType,
        void **aData,
        PRUint32 *aSize)
    {
        nsCAutoString txt;
        aContentType.Assign("text/html");

        nsCAutoString path;
        aURI->GetPath(path);

        long i;
        if (sscanf(path.get(), "//%ld", &i) == 1)
        {
            txt = gSampleMessages[i].mBody;
        }
        else
        {
            return NS_ERROR_FAILURE;
        }

        size_t size = txt.Length();
        *aData = (void *) nsMemory::Alloc(size + 1);
        if (!*aData)
            return NS_ERROR_OUT_OF_MEMORY;
        memset(*aData, 0, size + 1);
        memcpy(*aData, txt.get(), size);
        *aSize = size;
        return NS_OK;
    }
};

BEGIN_EVENT_TABLE(MailFrame, GeckoFrame)
    EVT_LIST_ITEM_SELECTED(XRCID("articles"), MailFrame::OnArticleClick)
END_EVENT_TABLE()

MailFrame::MailFrame(wxWindow* aParent)
{
    wxXmlResource::Get()->LoadFrame(this, aParent, wxT("mail_frame"));

    SetName("mail");

    SetIcon(wxICON(appicon));

    if (!gMailChannelCallbackRegistered)
    {
        GeckoProtocolHandler::RegisterHandler("wxmail", "wxMail handler", new MailChannelCallback);
        gMailChannelCallbackRegistered = TRUE;
    }

    // Set up the article pane
    wxListCtrl *articleListCtrl = (wxListCtrl *) FindWindowById(XRCID("articles"), this);
    if (articleListCtrl)
    {
        articleListCtrl->InsertColumn(0, "Subject", wxLIST_FORMAT_LEFT, 200);
        articleListCtrl->InsertColumn(1, "Sender", wxLIST_FORMAT_LEFT, 100);
        articleListCtrl->InsertColumn(2, "Date", wxLIST_FORMAT_LEFT, 100);

        for (long i = 0; i < sizeof(gSampleMessages) / sizeof(gSampleMessages[0]); i++)
        {
            articleListCtrl->InsertItem(i, gSampleMessages[i].mSubject);
            articleListCtrl->SetItem(i, 1, gSampleMessages[i].mSender);
            articleListCtrl->SetItem(i, 2, gSampleMessages[i].mDate);
        }
    }

    SetupDefaultGeckoWindow();

    wxWindow *hdrPanel = FindWindowById(XRCID("mail_header_panel"), this);
    if (hdrPanel)
    {
        hdrPanel->Show(FALSE);
    }


    CreateStatusBar();
}

void MailFrame::OnArticleClick(wxListEvent &event)
{
    if (mWebBrowser)
    {
        long idx = event.GetIndex();
        wxString url = wxString::Format("wxmail://%ld", idx);
        if (!url.IsEmpty())
        {
            wxStaticText *txtFrom = (wxStaticText *) FindWindowById(XRCID("mail_from"), this);
            if (txtFrom)
                txtFrom->SetLabel(gSampleMessages[idx].mSender);
            wxStaticText *txtDate = (wxStaticText *) FindWindowById(XRCID("mail_date"), this);
            if (txtDate)
                txtDate->SetLabel(gSampleMessages[idx].mDate);
            wxStaticText *txtSubject = (wxStaticText *) FindWindowById(XRCID("mail_subject"), this);
            if (txtSubject)
                txtSubject->SetLabel(gSampleMessages[idx].mSubject);

            wxWindow *hdrPanel = FindWindowById(XRCID("mail_header_panel"), this);
            if (hdrPanel)
                hdrPanel->Show(TRUE);

            nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(mWebBrowser);
            webNav->LoadURI(NS_ConvertASCIItoUCS2(url.c_str()).get(),
                                   nsIWebNavigation::LOAD_FLAGS_NONE,
                                   nsnull,
                                   nsnull,
                                   nsnull);
        }
    }
}

