/*--------------------------------------------------------------------------------------+
|  $Copyright: (c) 2020 Bentley Systems, Incorporated. All rights reserved. $
+--------------------------------------------------------------------------------------*/
#include "_ustation.h"

#include "rebarSDKExample.h"
#include "rebarSDK_KBR.h"

int KBR_RebarShape::Compare(const KBR_RebarShape &other, int mode /* = 0 */, int flag /* = 0 */) const
{
    int ret = 0;
    BeAssert(m_Shape != nullptr && L"Shape pointer cannot be NULL");
    BeAssert(other.m_Shape != nullptr && L"Shape pointer cannot be NULL");

    switch (mode)
    {
        case 0:
        {
            ret = DoubleCompare(m_Shape->GetDiameter(), other.GetShape()->GetDiameter());
            if (0 == ret)
            {
                ElementId id1 = m_Shape->GetElementId();
                ElementId id2 = other.GetShape()->GetElementId();
                ret = id1 > id2 ? 1 : id1 < id2 ? -1 : 0;
            }
            break;
        }
        default:
            break;
    }

    return ret;
}

/////////////////////////////////////////////////////////////////////////////////////////////////

void RebarSDK_KRB(WCharCP)
{
    BeFileName fileName;
    DgnModelRefP modelRef = ACTIVEMODEL;
    if (mdlDialog_fileCreate(fileName, NULL, 0, L"", L"*.kbr", NULL, L"KBR_STRINGS") == SUCCESS)
    {
        BrString firstPart = L"BF2D@--@P1";
        RebarCatalogCP catalog = RebarCatalog::Fetch(modelRef);
        KBR_RebarShapes<0> kbr_shapes;
        for (RebarShapeCP shape : *catalog)
        {
            KBR_RebarShape *kbr_shape = new KBR_RebarShape(shape);
            kbr_shapes.Add(kbr_shape);
        }

        intptr_t i;
        BrString kbr_str = firstPart;

        BeFileStatus status;
        WCharCP file = nullptr;
        BeTextFilePtr pTextFile = BeTextFile::Open(status, fileName.c_str(), TextFileOpenType::Write, TextFileOptions::KeepNewLine);
        for (i = 0; i < kbr_shapes.GetSize(); i++)
        {
            kbr_str = firstPart;

            KBR_RebarShape *kbr_shape = kbr_shapes[i];
            // length
            RebarShapeCP shape = kbr_shape->GetShape();
            double length = shape->GetShapeData().GetLength();
            double pin_radius_in_mm = RebarCode::GetPinRadius((LPCTSTR)shape->GetShapeData().GetSizeKey(), modelRef, shape->GetShapeData().GetIsStirrup());
            double mmFactor = modelRef->GetModelInfoCP()->GetUorPerMeter() / 1000.0;
            pin_radius_in_mm /= mmFactor;
            BrString str;
            str.Format(L"@l%f.0", length);
            kbr_str += str;

            // number of elements
            intptr_t num_elems = shape->RelationCountRebarElementMaster();
            str.Format(L"@n%d", num_elems);
            kbr_str += str;

            // diameter
            str.Format(L"@d%s", shape->GetShapeData().GetSizeKey());
            kbr_str += str;

            // the geometry script
            RebarScript rebar_script;
            shape->GetRebarShapeType()->GetRebarScript(rebar_script, pin_radius_in_mm);
            shape->GetString(str, pin_radius_in_mm);
            kbr_str += str;

            pTextFile->PutLine(kbr_str, true);
        }

        pTextFile->Close();
    }
}

