// TTSLexiconDlg.cpp :
//

#include "stdafx.h"
#include "remotetest.h"
#include "TTSLexiconDlg.h"
#include "VAIBOTTS.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTTSLexiconDlg dialog


CTTSLexiconDlg::CTTSLexiconDlg(CVAIBOTTS* pTTS)
{
	//{{AFX_DATA_INIT(CTTSLexiconDlg)
		//
	//}}AFX_DATA_INIT
    m_pTTS = pTTS;
    m_eLexicon = -1/*E_VAIBOTTS_LEXICON_SONYTTS_J*/;
}


void CTTSLexiconDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTTSLexiconDlg)
    DDX_Control(pDX, IDC_LISTVIEW_WORD, m_listViewWord);
    DDX_Control(pDX, IDC_EDIT_WORD, m_editWord);
    DDX_Control(pDX, IDC_EDIT_KANA, m_editPhone);
    DDX_Control(pDX, IDC_COMBO_PART, m_comboPart);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CTTSLexiconDlg, CDialog)
	//{{AFX_MSG_MAP(CTTSLexiconDlg)
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED( IDC_BUTTON_ADD, OnButtonAdd )
    ON_BN_CLICKED( IDC_BUTTON_DELETE, OnButtonDelete )
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTTSLexiconDlg Message handler

BOOL    CTTSLexiconDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    SetIcon(m_hIcon, TRUE);         // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon

    ListView_SetExtendedListViewStyle( ( HWND )m_listViewWord.m_hWnd, LVS_EX_FULLROWSELECT );

    m_listViewWord.InsertColumn( 0, "Word", LVCFMT_LEFT, 90, -1 );
    m_listViewWord.InsertColumn( 1, "Pronounce", LVCFMT_LEFT, 90, -1 );
    m_listViewWord.InsertColumn( 2, "Part", LVCFMT_LEFT, 80, -1 );

    return TRUE;
}

void    CTTSLexiconDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this);

        SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialog::OnPaint();
    }
}

HCURSOR     CTTSLexiconDlg::OnQueryDragIcon()
{
    return (HCURSOR) m_hIcon;
}

// Setup the dictionary
void    CTTSLexiconDlg::SetLexicon( int eLexicon )
{
    // Change the title name.
    switch ( eLexicon ) {
        case E_VAIBOTTS_LEXICON_SONYTTS_J:  // SONY TTS
            SetWindowText( "SONY TTS" );
            break;
        case E_VAIBOTTS_LEXICON_SAPI_J:     // SAPI Japanese
            SetWindowText( "SAPI TTS Japanese" );
            break;
        case E_VAIBOTTS_LEXICON_SAPI_E:     // SAPI English
            SetWindowText( "SAPI TTS English" );
            break;
        default:
            return; // Error check
    }

    // Empty list
    m_listViewWord.DeleteAllItems();

    // Get the dictionary list.
    if ( m_pTTS ) {

        int nWord;
        m_pTTS->GetWordNumInLexicon( ( E_VAIBOTTS_LEXICON )eLexicon, nWord );

        for ( int i = 0; i < nWord; i++ ) {
            char    strWord[ 256 ];
            char    strPronunciation[ 256 ];
            char    strPart[ 256 ];
            m_pTTS->GetWordInLexicon( ( E_VAIBOTTS_LEXICON )eLexicon, i, strWord );
            m_pTTS->GetPronunciationInLexicon( ( E_VAIBOTTS_LEXICON )eLexicon, i, strPronunciation );
            m_pTTS->GetPartInLexicon( ( E_VAIBOTTS_LEXICON )eLexicon, i, strPart );

            CString strTemp;
            strTemp.Format( "%s, \"%s\", %s\r\n",  strWord, strPronunciation, strPart );

            m_listViewWord.InsertItem( i, "" );
            m_listViewWord.SetItemText( i, 0, strWord );
            m_listViewWord.SetItemText( i, 1, strPronunciation );
            m_listViewWord.SetItemText( i, 2, strPart );
        }
    }


    // Register parts of speech.
    if ( m_eLexicon != eLexicon ) {
        int     i;
        int     nIdx;
        switch ( eLexicon ) {
            case E_VAIBOTTS_LEXICON_SONYTTS_J:  // SONY TTS
                m_comboPart.ResetContent();
                for ( i = 0; i < NUM_E_VAIBOTTS_PART_SONYTTS; i++ ) {
                    nIdx = m_comboPart.InsertString( -1, strSonyTTSParts[ i ] );
                    m_comboPart.SetItemData( nIdx, E_VAIBOTTS_PART_SONYTTS_HEAD + i );
                }
                m_comboPart.SetCurSel( 0 );
                break;
            case E_VAIBOTTS_LEXICON_SAPI_J:     // SAPI Japanese
            case E_VAIBOTTS_LEXICON_SAPI_E:     // SAPI English
                m_comboPart.ResetContent();
                nIdx = m_comboPart.InsertString( -1, "Noun" );
                m_comboPart.SetItemData( nIdx, E_VAIBOTTS_PART_SAPI_NOUN );
                nIdx = m_comboPart.InsertString( -1, "Verb" );
                m_comboPart.SetItemData( nIdx, E_VAIBOTTS_PART_SAPI_VERB );
                nIdx = m_comboPart.InsertString( -1, "Modifier" );
                m_comboPart.SetItemData( nIdx, E_VAIBOTTS_PART_SAPI_MODIFIER );
                nIdx = m_comboPart.InsertString( -1, "Function" );
                m_comboPart.SetItemData( nIdx, E_VAIBOTTS_PART_SAPI_FUNCTION );
                nIdx = m_comboPart.InsertString( -1, "Interjection" );
                m_comboPart.SetItemData( nIdx, E_VAIBOTTS_PART_SAPI_INTERJECTION );
                m_comboPart.SetCurSel( 0 );
                break;
            default:
                break;
        }
    }

    m_eLexicon = eLexicon;

}

void    CTTSLexiconDlg::OnButtonAdd()
{
    char    strWord[ 256 ];
    char    strPhone[ 256 ];
    E_VAIBOTTS_PART ePart;

    // Get a string.
    m_editWord.GetWindowText( strWord, 256 );
    m_editPhone.GetWindowText( strPhone, 256 );
    if ( !strlen( strWord ) || !strlen( strPhone ) ) {
        return; // Invalid.
    }

    // Get the part of speech
    int     nIdx;
    if ( ( nIdx = m_comboPart.GetCurSel() ) < 0 ) {
        return; // Invalid.
    }
    else {
        ePart = ( E_VAIBOTTS_PART )m_comboPart.GetItemData( nIdx );
    }

    // Registration
    if ( m_pTTS ) {
        if ( m_pTTS->AddWordInLexicon( ( E_VAIBOTTS_LEXICON )m_eLexicon, strWord, strPhone, ePart ) == E_VAIBOTTS_NOERROR ) {
            // Rebuild the list
            SetLexicon( m_eLexicon );
        }
    }
}

void    CTTSLexiconDlg::OnButtonDelete()
{

    // Get the word to delete.
    int     nIdx;
    POSITION pos = m_listViewWord.GetFirstSelectedItemPosition();
    if ( pos == NULL ) {
        // Nothing is chosen.
        return;
    }
    nIdx = m_listViewWord.GetNextSelectedItem( pos );


    // Deletion
    if ( m_pTTS ) {
        if ( m_pTTS->DeleteWordInLexicon( ( E_VAIBOTTS_LEXICON )m_eLexicon, nIdx ) == E_VAIBOTTS_NOERROR ) {
            // The reconstruction of the list
            SetLexicon( m_eLexicon );
        }
    }
}


