// Copyright (C) Stichting Deltares 2023. All rights reserved.
//
// This file is part of the application DAM - Live.
//
// DAM - Live is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml.Serialization;
using Deltares.Standard;
using Deltares.Standard.Attributes;
using Deltares.Standard.EventPublisher;
using Deltares.Standard.Language;
using Deltares.Standard.Validation;
namespace Deltares.Dam.Data;
///
/// Represents the calculation specifications at project level for DAM.
/// These are the main choices that specify the calculation
///
public class DamProjectCalculationSpecification : IVisibleEnabled, IDomain, IRepairer, IDisposable
{
private static AnalysisType selectedAnalysisType = AnalysisType.NoAdaption;
private DamFailureMechanismeCalculationSpecification currentSpecification;
public DamProjectCalculationSpecification()
{
DamCalculationSpecifications = new List();
//waterLevelTimeSeriesFileName = @"d:\src\delftgeosystems\trunk\data\Dam\RRD\Groot Salland\DAM UI Testdata\inputshortstart_dam.xml";
DataEventPublisher.OnAfterChange += DataEventPublisher_OnAfterChange;
}
[ReadOnly(true)] //For now make read only as only 1 spec is allowed at this moment
[Validate]
public List DamCalculationSpecifications { get; }
///
/// Gets or sets the analysis type for serialization purpose only.
/// This "dummy" property is and must be only used for serialization/deserialization purposes as the real static property
/// SelectedAnalysisType is NOT serialized. This is way its name is deliberately strange.
///
///
/// The analysis type for serialization purpose only.
///
public AnalysisType AnalysisTypeForSerializationPurposeOnly
{
get
{
return selectedAnalysisType;
}
set
{
selectedAnalysisType = value;
}
}
///
/// Gets or sets the type of the selected analysis.
///
///
/// The type of the selected analysis.
///
[Label("Analysis type")]
public static AnalysisType SelectedAnalysisType
{
get
{
return selectedAnalysisType;
}
set
{
// Create local instance of DamProjectCalculationSpecification because event handlers need sender
var r = new DamProjectCalculationSpecification();
DataEventPublisher.BeforeChange(r, "SelectedAnalysisType");
selectedAnalysisType = value;
DataEventPublisher.AfterChange(r, "SelectedAnalysisType");
}
}
[XmlIgnore]
public DamFailureMechanismeCalculationSpecification CurrentSpecification
{
get
{
if (currentSpecification == null && DamCalculationSpecifications.Count > 0)
{
currentSpecification = DamCalculationSpecifications[0];
}
return currentSpecification;
}
set
{
DataEventPublisher.BeforeChange(this, "CurrentSpecification");
currentSpecification = value;
DataEventPublisher.AfterChange(this, "CurrentSpecification");
}
}
[XmlIgnore] [Browsable(false)] public IVisibleEnabledProvider VisibleEnabledProvider { get; set; }
[Validate]
public ValidationResult[] Validate()
{
if (DamCalculationSpecifications.Count > 1)
{
return new[]
{
new ValidationResult(ValidationResultType.Error, LocalizationManager.GetTranslatedText(this, "MaxOneCalculationSpecification"),
this, "DamCalculationSpecifications", "DamCalculationSpecifications", this)
};
}
return new ValidationResult[0];
}
public void Dispose()
{
DataEventPublisher.OnAfterChange -= DataEventPublisher_OnAfterChange;
}
public ICollection GetDomain(string property)
{
switch (property)
{
case "SelectedAnalysisType":
return new[]
{
AnalysisType.AdaptGeometry,
AnalysisType.NoAdaption
};
default: return null;
}
}
public void Repair(object subject, string property, string id)
{
switch (id)
{
case "DamCalculationSpecifications":
RepairTooManySpecifications();
break;
}
}
public string GetRepairDescription(object subject, string property, string id)
{
switch (id)
{
case "DamCalculationSpecifications":
return LocalizationManager.GetTranslatedText(this, "KeepFirstCalculationSpecification");
default: return "";
}
}
public bool IsVisible(string property)
{
if (DamCalculationSpecifications.Count <= 0)
{
return false;
}
switch (property)
{
case "SelectedAnalysisType": return Location.DamProjectType == DamProjectType.Design;
case "CurrentSpecification":
{
bool bol = (DamCalculationSpecifications.Count > 0) &&
(CurrentSpecification.FailureMechanismSystemType ==
FailureMechanismSystemType.StabilityInside ||
CurrentSpecification.FailureMechanismSystemType ==
FailureMechanismSystemType.StabilityOutside);
return bol;
}
case "SelectedStabilityKernelType":
{
return VisibleEnabledProvider != null && VisibleEnabledProvider.IsVisible(this, "SelectedStabilityKernelType");
}
default: return true;
}
}
public bool IsEnabled(string property)
{
switch (property)
{
default: return true;
}
}
private void DataEventPublisher_OnAfterChange(object sender, PublishEventArgs e)
{
if (sender == currentSpecification)
{
DataEventPublisher.AfterChange(this);
}
}
private void RepairTooManySpecifications()
{
for (int i = DamCalculationSpecifications.Count - 1; i > 0; i--)
{
DamCalculationSpecifications.Remove(DamCalculationSpecifications[i]);
}
}
}