/* AbiSource
 * 
 * Copyright (C) 2002 Dom Lachowicz <cinamod@hotmail.com>
 * Copyright (C) 2004 Robert Staudinger <robsta@stereolyzer.net>
 * Copyright (C) 2005 Daniel d'Andrada T. de Carvalho
 * <daniel.carvalho@indt.org.br>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  
 * 02111-1307, USA.
 */
 
// Class definition include.
#include "OD_Table_ListenerState.h"

// Internal includes
#include "OD_ElementStack.h"
#include "OD_Office_Styles.h"
#include "OD_Style_Style.h"
#include "OD_ListenerStateAction.h"

// AbiWord includes
#include <pd_Document.h>


/**
 * Constructor
 */
OD_Table_ListenerState::OD_Table_ListenerState (PD_Document* pDocument,
                                 OD_Office_Styles* pStyles,
                                 OD_ElementStack& rElementStack) :
                                 OD_ListenerState("Table", rElementStack),
                                 m_pAbiDocument(pDocument),
                                 m_pStyles(pStyles),
                                 m_onFirstPass(true),
                                 m_elementLevel(0),
                                 m_gotAllColumnWidths(true)
{
    if (m_rElementStack.hasElement("office:document-content")) {
        m_onContentStream = true;
    } else {
        m_onContentStream = false;
    }
}


/**
 * Called when the XML parser finds a start element tag.
 * 
 * @param pName The name of the element.
 * @param ppAtts The attributes of the parsed start tag.
 */
void OD_Table_ListenerState::startElement (const XML_Char* pName,
                                           const XML_Char** ppAtts,
                                           OD_ListenerStateAction& rAction)
{
    UT_ASSERT(m_elementLevel >= 0);
    
    
    if (!m_waitingEndElement.empty()) {
        // Do nothing.
        
    } else if (!UT_strcmp(pName, "table:table")) {

        _parseTableStart(ppAtts, rAction);
        
    } else if (!UT_strcmp(pName, "table:table-column")) {
        
        if (m_onFirstPass) {
            const XML_Char* pStyleName;
            const OD_Style_Style* pStyle;
            
            pStyleName = UT_getAttribute("table:style-name", ppAtts);
            if (pStyleName != NULL) {
                pStyle = m_pStyles->getTableColumnStyle(pStyleName,
                                                        m_onContentStream);
                UT_ASSERT(pStyle != NULL);
                
                if (pStyle->getColumnWidth()->empty()) {
                    m_gotAllColumnWidths = false;
                } else {
                    m_columnWidths += *(pStyle->getColumnWidth());
                    m_columnWidths += "/";
                }
            } else {
                m_gotAllColumnWidths = false;
            }
        }
        
    } else if (!UT_strcmp(pName, "table:table-row")) {
        
        if (m_onFirstPass) {
            const XML_Char* pStyleName;
            const OD_Style_Style* pStyle;
            
            pStyleName = UT_getAttribute("table:style-name", ppAtts);
            
            if (pStyleName != NULL) {
                pStyle = m_pStyles->getTableRowStyle(pStyleName,
                                                     m_onContentStream);
                UT_ASSERT(pStyle != NULL);
                
                if (!pStyle->getRowHeight()->empty()) {
                    m_rowHeights += *(pStyle->getRowHeight());
                } else if (!pStyle->getMinRowHeight()->empty()) {
                    m_rowHeights += *(pStyle->getMinRowHeight());
                }
            }
            
            // AbiWord supports unspecified row heights mixed among specified ones.
            // e.g.: "table-row-heights:2.37cm//3.62cm/"
            // So, we aways write the "/" regardless of having a defined height.
            m_rowHeights += "/";
        } else {
            m_row++;
            m_col = 0;
        }
        
    } else if (!UT_strcmp(pName, "table:table-cell")) {
        
        _parseCellStart (ppAtts, rAction);
    }
    
    m_elementLevel++;
}


/**
 * Called when an "end of element" tag is parsed (like <myElementName/>)
 * 
 * @param pName The name of the element
 */
void OD_Table_ListenerState::endElement (const XML_Char* pName,
                                         OD_ListenerStateAction& rAction)
{
    
    UT_ASSERT(m_elementLevel > 0);
    
    if (!m_waitingEndElement.empty()) {
        
        if ( !UT_strcmp(m_waitingEndElement.utf8_str(), pName) ) {
            // Found it. No more wait.
            m_waitingEndElement.clear();
        }
        
    } else if (!UT_strcmp(pName, "table:table")) {
        
        if (m_elementLevel == 1) {
            
            if (m_onFirstPass) {
                m_onFirstPass = false;
            } else {
                m_pAbiDocument->appendStrux(PTX_EndTable, NULL);
                rAction.popState();
            }
        } else {
            // It's the end of a nested table.
            // Do nothing.
        }
        
    } else if (!UT_strcmp(pName, "table:table-cell")) {
    
        if (m_onFirstPass) {
            // Do nothing.
        } else {
            m_pAbiDocument->appendStrux(PTX_EndCell,NULL);
        }
    }
    
    m_elementLevel--;
}


/**
 * Used to parse a <table:table> start element.
 */
void OD_Table_ListenerState::_parseTableStart(const XML_Char** ppAtts,
                                              OD_ListenerStateAction& rAction) {
                                                
    if (m_elementLevel == 0) {
        if (m_onFirstPass) {
            rAction.repeatElement();
        } else {
            const XML_Char* ppAttribs[10];
            UT_UTF8String props;
            const XML_Char* pVal;
            const OD_Style_Style* pStyle = NULL;
            
            pVal = UT_getAttribute("table:style-name", ppAtts);
            if (pVal) {
                pStyle = m_pStyles->getTableStyle(pVal, m_onContentStream);
                UT_ASSERT(pStyle);
            }
            
            // Background color
            if (pStyle != NULL) {
                if (!pStyle->getBackgroundColor()->empty()) {
                    props += "background-color:";
                    props += pStyle->getBackgroundColor()->utf8_str();
                }
            }
            
            // Column widths
            if (m_gotAllColumnWidths) {
                if (!props.empty()) {
                    props += "; ";
                }
                props += "table-column-props:";
                props += m_columnWidths;
            }
            
            // Row heights
            if (!props.empty()) {
                props += "; ";
            }
            props += "table-row-heights:";
            props += m_rowHeights;
            
            
            if (!props.empty()) {
                ppAttribs[0] = "props";
                ppAttribs[1] = props.utf8_str();
                ppAttribs[2] = 0; // Signal the end of the array.
                
                m_pAbiDocument->appendStrux(PTX_SectionTable, ppAttribs);
            } else {
                m_pAbiDocument->appendStrux(PTX_SectionTable, NULL);
            }
            
            
            // Initialize cell variables.
            m_row = 0;
            m_col = 0;
        }
    } else {
        // It's a nested table
        if (m_onFirstPass) {
            m_waitingEndElement = "table:table";
        } else {
            rAction.pushState("Table");
        }
    }
}


/**
 * 
 */
void OD_Table_ListenerState::_parseCellStart (const XML_Char** ppAtts,
                                              OD_ListenerStateAction& rAction)
{
    if (m_onFirstPass) {
        // Do nothing.
    } else {    

        UT_UTF8String props;
        const XML_Char *cell_props[3];        
        const XML_Char* pVal;
        const OD_Style_Style* pStyle = NULL;
        UT_sint32 colSpan;

        m_col++;
        
        
        pVal = UT_getAttribute("table:number-columns-spanned", ppAtts);
        if (pVal) {
            colSpan = atoi(pVal);
            UT_ASSERT(colSpan > 0);
        } else {
            colSpan = 1;
        }
        

        props = UT_UTF8String_sprintf(
            "top-attach: %d; bot-attach: %d; left-attach: %d; right-attach: %d",
            m_row-1, m_row, m_col-1, m_col + (colSpan-1));

        pVal = UT_getAttribute("table:style-name", ppAtts);
        if (pVal) {
            pStyle = m_pStyles->getTableCellStyle(pVal, m_onContentStream);
            UT_ASSERT(pStyle);
        }

        if (pStyle) {
            // thickness
            if (!pStyle->getBorderTop_thickness()->empty()) {
                props += "; top-thickness:";
                props += *(pStyle->getBorderTop_thickness());
            }
            
            if (!pStyle->getBorderBottom_thickness()->empty()) {
                props += "; bot-thickness:";
                props += *(pStyle->getBorderBottom_thickness());
            }
            
            if (!pStyle->getBorderLeft_thickness()->empty()) {
                props += "; left-thickness:";
                props += *(pStyle->getBorderLeft_thickness());
            }
            
            if (!pStyle->getBorderRight_thickness()->empty()) {
                props += "; right-thickness:";
                props += *(pStyle->getBorderRight_thickness());
            }
            
            
            // color
            if (!pStyle->getBorderTop_color()->empty()) {
                props += "; top-color:";
                props += *(pStyle->getBorderTop_color());
            }
            
            if (!pStyle->getBorderBottom_color()->empty()) {
                props += "; bot-color:";
                props += *(pStyle->getBorderBottom_color());
            }
            
            if (!pStyle->getBorderLeft_color()->empty()) {
                props += "; left-color:";
                props += *(pStyle->getBorderLeft_color());
            }
            
            if (!pStyle->getBorderRight_color()->empty()) {
                props += "; right-color:";
                props += *(pStyle->getBorderRight_color());
            }
            
            
            // background color
            if (!pStyle->getBackgroundColor()->empty()) {
                props += "; background-color:";
                props += pStyle->getBackgroundColor()->utf8_str();
            }
        }

        cell_props[0] = "props";
        cell_props[1] = props.utf8_str();
        cell_props[2] = 0;

        m_pAbiDocument->appendStrux(PTX_SectionCell, cell_props);

        // Now parse the cell text content.
        rAction.pushState("TextContent");
    }
}
