// Change History:

#include "ddp/ddp_field.h"

// **************************************************************************
// DDP_Layout
// **************************************************************************

DDP_Field::DDP_Field(const DDP_Field & rhs) :
    DDP_ExportDef(rhs),
    DDP_Descr(rhs),
    Size(0), MaxDataSize(0), Width(0), Type(0), ApportionLevel(0)
{
    SetSize(rhs.GetSize());
    SetMaxDataSize(rhs.GetMaxDataSize());
    SetWidth(rhs.GetWidth());
    SetType(rhs.GetType());
    SetReoccurGroup(rhs.GetReoccurGroup());
    SetApportionLevel(rhs.GetApportionLevel());
}
//--------------------------------------------------------------------------

bool DDP_Field::get_field_type(byte * buf, uint sz) const
{
    if(GetType() == DATAFIELDTYPE_TEXT)
        PORT_snprintf_(buf, sz, "Text");

    else if(GetType() == DATAFIELDTYPE_INTEGER)
        {
        if(GetWidth())
            PORT_snprintf_(buf, sz, "Text");
        else
            PORT_snprintf_(buf, sz, "Int");
        }

return true;
}
//--------------------------------------------------------------------------

bool DDP_Field::Dump(DDP_PrintFile * file, const byte * prefix) const
{
    if(!DDP_ExportDef::Dump(file, prefix))
        return false;

    const byte * sp = prefix ? prefix : (const byte *)"";

    if(IsNull())
        {
        // NULL Field !!!
        if(!file->Print(PORT_cbyte("LO_%s%sName = NULL\r\n"), sp, GetDefLabel() ))
            return false;
        if(!file->Print(PORT_cbyte("LO_%s%sSize = 0\r\n"), sp, GetDefLabel() ))
            return false;
        if(!file->Print(PORT_cbyte("LO_%s%sCombineSize = 0\r\n"), sp, GetDefLabel() ))
            return false;
        if(!file->Print(PORT_cbyte("LO_%s%sReoccurGroup = NULL\r\n"), sp, GetDefLabel() ))
            return false;
        if(!file->Print(PORT_cbyte("LO_%s%sType = unknown\r\n"), sp, GetDefLabel() ))
            return false;
        }
    else
        {
        if(!file->Print(PORT_cbyte("LO_%s%sName = %s\r\n"), sp, GetDefLabel(), GetCaption() ))
            return 0;

        const byte * group = GetReoccurGroup();

        if(group)
            {
            if(!file->Print(PORT_cbyte("LO_%s%sReoccurGroup = %s\r\n"), sp, GetDefLabel(), group))
                return false;
            }
        else
            {
            if(!file->Print(PORT_cbyte("LO_%s%sReoccurGroup = NULL\r\n"), sp, GetDefLabel() ))
                return false;
            }
        if(!file->Print(PORT_cbyte("LO_%s%sSize = %u\r\n"), sp, GetDefLabel(), get_size()))
            return false;
        if(!file->Print(PORT_cbyte("LO_%s%sCombineSize = %u\r\n"), sp, GetDefLabel(), GetSize()))
            return false;

        byte bb[DDP_PrintFile::LINE_SIZE];
        get_field_type(bb, sizeof(bb));

        if(!file->Print(PORT_cbyte("LO_%s%sType = %s\r\n"), sp, GetDefLabel(), bb))
            return false;
        }

return true;
}
//--------------------------------------------------------------------------

bool DDP_Layout::Load(const DRS_Application * appl)
{
    if(!appl)
        return false;

    uint i, cnt = appl->FieldCount();
    Clear();
    for(i = 0; i < cnt; ++i)
        {
        const DRS_Field * drs_field = &appl->Field(i);
        const DRS_Descriptor * desc = &drs_field->Descriptor();
        
        Add(desc);
        DDP_Field * field = (DDP_Field *)Get(i);
        field->SetSize(drs_field->MaxSize());
        field->SetMaxDataSize(drs_field->MaxSize(false));

        uint type = drs_field->GetType();
        field->SetType(type);
        field->SetReoccurGroup(drs_field->FieldOccurrGroup());
        field->SetApportionLevel(drs_field->FieldApportionLevel());

        if(type == DATAFIELDTYPE_INTEGER)
            field->SetWidth(((DRS_FieldInteger *)drs_field)->GetWidth());
        }
        
return true;
}

/****************************************************************************
 NOTES
 ****************************************************************************/

 