/////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2018-     Equinor ASA
//
//  ResInsight 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 3 of the License, or
//  (at your option) any later version.
//
//  ResInsight 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 at <http://www.gnu.org/licenses/gpl.html>
//  for more details.
//
/////////////////////////////////////////////////////////////////////////////////

#include "RicCreateMultipleFracturesOptionItemUi.h"

#include "RicCreateMultipleFracturesUi.h"
#include "RimFractureTemplate.h"
#include "RimFractureTemplateCollection.h"
#include "RimOilField.h"
#include "RimProject.h"

CAF_PDM_SOURCE_INIT( RicCreateMultipleFracturesOptionItemUi, "RiuMultipleFractionsOptions" );

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RicCreateMultipleFracturesOptionItemUi::RicCreateMultipleFracturesOptionItemUi()
{
    CAF_PDM_InitField( &m_topKOneBased, "TopKLayer", 1, "Top K Layer" );
    CAF_PDM_InitField( &m_baseKOneBased, "BaseKLayer", 1, "Base K Layer" );
    CAF_PDM_InitFieldNoDefault( &m_fractureTemplate, "Template", "Template" );
    CAF_PDM_InitField( &m_minSpacing, "MinSpacing", 300.0, "Spacing" );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicCreateMultipleFracturesOptionItemUi::setValues( int                  topKOneBased,
                                                        int                  baseKOneBased,
                                                        RimFractureTemplate* fractureTemplate,
                                                        double               minimumSpacing )
{
    m_topKOneBased     = topKOneBased;
    m_baseKOneBased    = baseKOneBased;
    m_fractureTemplate = fractureTemplate;
    m_minSpacing       = minimumSpacing;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RicCreateMultipleFracturesOptionItemUi::topKLayer() const
{
    return m_topKOneBased;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RicCreateMultipleFracturesOptionItemUi::baseKLayer() const
{
    return m_baseKOneBased;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimFractureTemplate* RicCreateMultipleFracturesOptionItemUi::fractureTemplate() const
{
    return m_fractureTemplate();
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double RicCreateMultipleFracturesOptionItemUi::minimumSpacing() const
{
    return m_minSpacing;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RicCreateMultipleFracturesOptionItemUi::isKLayerContained( int oneBasedK ) const
{
    auto minMax = std::minmax( m_topKOneBased, m_baseKOneBased );

    if ( oneBasedK < minMax.first ) return false;
    if ( oneBasedK <= minMax.second ) return true;

    return false;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicCreateMultipleFracturesOptionItemUi::fieldChangedByUi( const caf::PdmFieldHandle* changedField,
                                                               const QVariant&            oldValue,
                                                               const QVariant&            newValue )
{
    if ( changedField == &m_topKOneBased )
    {
        if ( m_topKOneBased > m_baseKOneBased ) m_baseKOneBased = m_topKOneBased;
    }
    else if ( changedField == &m_baseKOneBased )
    {
        if ( m_baseKOneBased < m_topKOneBased ) m_topKOneBased = m_baseKOneBased;
    }

    RiuCreateMultipleFractionsUi* parent = nullptr;
    this->firstAncestorOrThisOfType( parent );
    if ( parent )
    {
        parent->updateButtonsEnableState();
        parent->updateConnectedEditors();
    }
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QList<caf::PdmOptionItemInfo> RicCreateMultipleFracturesOptionItemUi::calculateValueOptions( const caf::PdmFieldHandle* fieldNeedingOptions )
{
    QList<caf::PdmOptionItemInfo> options;

    RimProject* proj = RimProject::current();
    CVF_ASSERT( proj );

    if ( fieldNeedingOptions == &m_fractureTemplate )
    {
        RimOilField* oilField = proj->activeOilField();
        if ( oilField && oilField->fractureDefinitionCollection() )
        {
            RimFractureTemplateCollection* fracDefColl = oilField->fractureDefinitionCollection();

            for ( RimFractureTemplate* fracDef : fracDefColl->fractureTemplates() )
            {
                QString displayText = fracDef->nameAndUnit();

                options.push_back( caf::PdmOptionItemInfo( displayText, fracDef ) );
            }
        }
    }

    return options;
}
