//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void RimCellRangeFilter::defineEditorAttribute(const caf::PdmFieldHandle* field, QString uiConfigName, caf::PdmUiEditorAttribute * attribute)
{
    caf::PdmUiSliderEditorAttribute* myAttr = dynamic_cast<caf::PdmUiSliderEditorAttribute*>(attribute);
    if (!myAttr || !parentContainer())
    {
        return;
    }

    const cvf::StructGridInterface* grid = selectedGrid();
    
    if (!grid) return;

    if (field == &startIndexI || field == &cellCountI)
    {
        myAttr->m_minimum = 1;
        myAttr->m_maximum = static_cast<int>(grid->cellCountI());
    }
    else if (field == &startIndexJ  || field == &cellCountJ)
    {
        myAttr->m_minimum = 1;
        myAttr->m_maximum = static_cast<int>(grid->cellCountJ());
    }
    else if (field == &startIndexK || field == &cellCountK)
    {
        myAttr->m_minimum = 1;
        myAttr->m_maximum = static_cast<int>(grid->cellCountK());
    }
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void RimCellRangeFilter::fieldChangedByUi(const caf::PdmFieldHandle* changedField, const QVariant& oldValue, const QVariant& newValue)
{
    if (changedField == &m_gridIndex)
    {
        const cvf::StructGridInterface* grid = selectedGrid();

        if (grid && grid->cellCountI() > 0 && grid->cellCountJ() > 0 && grid->cellCountK() > 0)
        {
            cellCountI = static_cast<int>(grid->cellCountI());
            startIndexI = 1;

            cellCountJ = static_cast<int>(grid->cellCountJ());
            startIndexJ = 1;

            cellCountK = static_cast<int>(grid->cellCountK());
            startIndexK = 1;
        }

        parentContainer()->updateDisplayModeNotifyManagedViews(this);

        return;
    }
    
    if (changedField != &name)
    {
        computeAndSetValidValues();
    
        parentContainer()->updateDisplayModeNotifyManagedViews(this);
    }
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void RimCellRangeFilter::defineEditorAttribute(const caf::PdmFieldHandle* field, QString uiConfigName, caf::PdmUiEditorAttribute * attribute)
{
    caf::PdmUiSliderEditorAttribute* myAttr = static_cast<caf::PdmUiSliderEditorAttribute*>(attribute);
    if (!myAttr || !m_parentContainer)
    {
        return;
    }

    RigGridBase* grid = selectedGrid();

    if (field == &startIndexI || field == &cellCountI)
    {
        myAttr->m_minimum = 1;
        myAttr->m_maximum = static_cast<int>(grid->cellCountI());
    }
    else if (field == &startIndexJ  || field == &cellCountJ)
    {
        myAttr->m_minimum = 1;
        myAttr->m_maximum = static_cast<int>(grid->cellCountJ());
    }
    else if (field == &startIndexK || field == &cellCountK)
    {
        myAttr->m_minimum = 1;
        myAttr->m_maximum = static_cast<int>(grid->cellCountK());
    }

    RigActiveCellInfo* actCellInfo = m_parentContainer->activeCellInfo();
    if (grid == m_parentContainer->mainGrid() && actCellInfo)
    {
        cvf::Vec3st min, max;
        actCellInfo->IJKBoundingBox(min, max);

        // Adjust to Eclipse indexing
        min.x() = min.x() + 1;
        min.y() = min.y() + 1;
        min.z() = min.z() + 1;

        max.x() = max.x() + 1;
        max.y() = max.y() + 1;
        max.z() = max.z() + 1;

        startIndexI.setUiName(QString("I Start (%1)").arg(min.x()));
        startIndexJ.setUiName(QString("J Start (%1)").arg(min.y()));
        startIndexK.setUiName(QString("K Start (%1)").arg(min.z()));
        cellCountI.setUiName(QString("  Width (%1)").arg(max.x() - min.x() + 1));
        cellCountJ.setUiName(QString("  Width (%1)").arg(max.y() - min.y() + 1));
        cellCountK.setUiName(QString("  Width (%1)").arg(max.z() - min.z() + 1));
    }
    else
    {
        startIndexI.setUiName(QString("I Start"));
        startIndexJ.setUiName(QString("J Start"));
        startIndexK.setUiName(QString("K Start"));
        cellCountI.setUiName(QString("  Width"));
        cellCountJ.setUiName(QString("  Width"));
        cellCountK.setUiName(QString("  Width"));
    }
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void RimCellRangeFilter::setDefaultValues()
{
    CVF_ASSERT(parentContainer());

    const cvf::StructGridInterface* grid = selectedGrid();

    if (!grid) return;

    Rim3dView* rimView = nullptr;
    this->firstAncestorOrThisOfTypeAsserted(rimView);
    auto actCellInfo = RigReservoirGridTools::activeCellInfo(rimView);
    
    RimCase* rimCase = nullptr;
    this->firstAncestorOrThisOfTypeAsserted(rimCase);
   
    const cvf::StructGridInterface* mainGrid = RigReservoirGridTools::mainGrid(rimCase);

    if (grid == mainGrid && actCellInfo)
    {
        cvf::Vec3st min, max;
        actCellInfo->IJKBoundingBox(min, max);

        // Adjust to Eclipse indexing
        min.x() = min.x() + 1;
        min.y() = min.y() + 1;
        min.z() = min.z() + 1;

        max.x() = max.x() + 1;
        max.y() = max.y() + 1;
        max.z() = max.z() + 1;

        startIndexI = static_cast<int>(min.x());
        startIndexJ = static_cast<int>(min.y());
        startIndexK = static_cast<int>(min.z());
        cellCountI = static_cast<int>(max.x() - min.x() + 1);
        cellCountJ = static_cast<int>(max.y() - min.y() + 1);
        cellCountK = static_cast<int>(max.z() - min.z() + 1);
    }
    else
    {
        startIndexI = 1;
        startIndexJ = 1;
        startIndexK = 1;
        cellCountI = static_cast<int>(grid->cellCountI() );
        cellCountJ = static_cast<int>(grid->cellCountJ() );
        cellCountK = static_cast<int>(grid->cellCountK() );
    }
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void RimCellRangeFilter::computeAndSetValidValues()
{
    CVF_ASSERT(m_parentContainer);

    RigGridBase* grid = selectedGrid();
    if (grid && grid->cellCount() > 0 )
    {
        cellCountI = cvf::Math::clamp(cellCountI.v(),   1, static_cast<int>(grid->cellCountI()));
        startIndexI = cvf::Math::clamp(startIndexI.v(), 1, static_cast<int>(grid->cellCountI()));

        cellCountJ = cvf::Math::clamp(cellCountJ.v(),   1, static_cast<int>(grid->cellCountJ()));
        startIndexJ = cvf::Math::clamp(startIndexJ.v(), 1, static_cast<int>(grid->cellCountJ()));

        cellCountK = cvf::Math::clamp(cellCountK.v(),   1, static_cast<int>(grid->cellCountK()));
        startIndexK = cvf::Math::clamp(startIndexK.v(), 1, static_cast<int>(grid->cellCountK()));
    }
    this->updateIconState();
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void RimCellRangeFilter::setDefaultValues()
{
    CVF_ASSERT(m_parentContainer);

    RigGridBase* grid = selectedGrid();

    RigActiveCellInfo* actCellInfo = m_parentContainer->activeCellInfo();
    if (grid == m_parentContainer->mainGrid() && actCellInfo)
    {
        cvf::Vec3st min, max;
        actCellInfo->IJKBoundingBox(min, max);

        // Adjust to Eclipse indexing
        min.x() = min.x() + 1;
        min.y() = min.y() + 1;
        min.z() = min.z() + 1;

        max.x() = max.x() + 1;
        max.y() = max.y() + 1;
        max.z() = max.z() + 1;

        startIndexI = static_cast<int>(min.x());
        startIndexJ = static_cast<int>(min.y());
        startIndexK = static_cast<int>(min.z());
        cellCountI = static_cast<int>(max.x() - min.x() + 1);
        cellCountJ = static_cast<int>(max.y() - min.y() + 1);
        cellCountK = static_cast<int>(max.z() - min.z() + 1);
    }
    else
    {
        startIndexI = 1;
        startIndexJ = 1;
        startIndexK = 1;
        cellCountI = static_cast<int>(grid->cellCountI() );
        cellCountJ = static_cast<int>(grid->cellCountJ() );
        cellCountK = static_cast<int>(grid->cellCountK() );
    }
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void RimCellRangeFilter::defineUiOrdering(QString uiConfigName, caf::PdmUiOrdering& uiOrdering)
{
    bool readOnlyState = isRangeFilterControlled();

    std::vector<caf::PdmFieldHandle*> objFields;
    this->fields(objFields);
    for (auto& objField : objFields)
    {
        objField->uiCapability()->setUiReadOnly(readOnlyState);
    }

    const cvf::StructGridInterface* grid = selectedGrid();

    RimCase* rimCase = nullptr;
    this->firstAncestorOrThisOfTypeAsserted(rimCase);
    const cvf::StructGridInterface* mainGrid = RigReservoirGridTools::mainGrid(rimCase);

    Rim3dView* rimView = nullptr;
    this->firstAncestorOrThisOfTypeAsserted(rimView);
    auto actCellInfo = RigReservoirGridTools::activeCellInfo(rimView);

    if (grid == mainGrid && actCellInfo)
    {
        cvf::Vec3st min, max;
        actCellInfo->IJKBoundingBox(min, max);

        // Adjust to Eclipse indexing
        min.x() = min.x() + 1;
        min.y() = min.y() + 1;
        min.z() = min.z() + 1;

        max.x() = max.x() + 1;
        max.y() = max.y() + 1;
        max.z() = max.z() + 1;

        startIndexI.uiCapability()->setUiName(QString("I Start (%1)").arg(min.x()));
        startIndexJ.uiCapability()->setUiName(QString("J Start (%1)").arg(min.y()));
        startIndexK.uiCapability()->setUiName(QString("K Start (%1)").arg(min.z()));
        cellCountI.uiCapability()->setUiName(QString("  Width (%1)").arg(max.x() - min.x() + 1));
        cellCountJ.uiCapability()->setUiName(QString("  Width (%1)").arg(max.y() - min.y() + 1));
        cellCountK.uiCapability()->setUiName(QString("  Width (%1)").arg(max.z() - min.z() + 1));
    }
    else
    {
        startIndexI.uiCapability()->setUiName(QString("I Start"));
        startIndexJ.uiCapability()->setUiName(QString("J Start"));
        startIndexK.uiCapability()->setUiName(QString("K Start"));
        cellCountI.uiCapability()->setUiName(QString("  Width"));
        cellCountJ.uiCapability()->setUiName(QString("  Width"));
        cellCountK.uiCapability()->setUiName(QString("  Width"));
    }
    
    uiOrdering.add(&name);
    uiOrdering.add(&filterMode);
    uiOrdering.add(&m_gridIndex);
    uiOrdering.add(&propagateToSubGrids);
    uiOrdering.add(&startIndexI);
    uiOrdering.add(&cellCountI);
    uiOrdering.add(&startIndexJ);
    uiOrdering.add(&cellCountJ);
    uiOrdering.add(&startIndexK);
    uiOrdering.add(&cellCountK);

    if(RiaApplication::enableDevelopmentFeatures())
    {
        auto group = uiOrdering.addNewGroup("Single Cell Filtering (TEST)");
        group->setCollapsedByDefault(true);

        group->add(&m_useIndividualCellIndices);
        group->add(&m_individualCellIndices);

        m_individualCellIndices.uiCapability()->setUiReadOnly(!m_useIndividualCellIndices);
    }
    uiOrdering.skipRemainingFields(true);
}