// Copyright (C) Stichting Deltares 2026. All rights reserved.
//
// This file is part of the application DAM - UI.
//
// DAM - UI 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.
//
// 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 General Public License for more details.
//
// You should have received a copy of the GNU 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.Generic;
using System.ComponentModel;
using System.Linq;
using Deltares.Standard.EventPublisher;
namespace Deltares.Dam.Data.Sensors;
///
/// Class to hold the collected sensor data
///
public class SensorData : IDisposable
{
public SensorData()
{
DataEventPublisher.OnAfterChange += DataEventPublisher_OnAfterChange;
DataEventPublisher.OnDataListModified += DataEventPublisher_OnDataListModified;
}
///
/// Gets or sets the sensors.
///
///
/// The sensors.
///
public List Sensors { get; set; } = new List();
///
/// Gets or sets the sensor groups.
///
///
/// The sensor groups.
///
public List SensorGroups { get; set; } = new List();
///
/// Gets or sets the sensor locations.
///
///
/// The sensor locations.
///
[Browsable(false)]
[ReadOnly(true)]
public List SensorLocations { get; } = new List();
///
/// Updates the pick sensors for groups.
///
public void UpdatePickSensorsForGroups()
{
foreach (Group sensorGroup in SensorGroups)
{
sensorGroup.PickSensors = Sensors;
}
}
public Sensor GetSensorById(int id)
{
return Sensors.FirstOrDefault(sensor => sensor.ID == id);
}
public Group GetGroupById(int id)
{
return SensorGroups.FirstOrDefault(sensorGroup => sensorGroup.ID == id);
}
public SensorLocation GetSensorLocationByLocationName(string name)
{
return SensorLocations.FirstOrDefault(sensorLocation => sensorLocation.LocationName == name);
}
///
/// Gets the groups for use in selection lists in de UI.
///
/// The sensoLocation.
///
public IEnumerable GetGroups(SensorLocation sensorLocation)
{
var list = new List();
list.AddRange(SensorGroups);
return list;
}
public void Dispose()
{
DataEventPublisher.OnAfterChange -= DataEventPublisher_OnAfterChange;
}
private void DataEventPublisher_OnDataListModified(object sender, PublishEventArgs e)
{
if (sender is List)
{
var dma = (DataListModifiedArgs) e;
if (dma.Action == ListModifyAction.Delete)
{
UpdatePickSensorsForGroups();
foreach (Group sensorGroup in SensorGroups)
{
sensorGroup.Remove((Sensor) e.Objects[0]);
// Next lines just to force an update of the group table content
string ms = sensorGroup.SelectionAsString;
sensorGroup.SelectionAsString = ms;
}
}
}
if (sender is List)
{
var dma = (DataListModifiedArgs) e;
if (dma.Action == ListModifyAction.Delete)
{
var delList = new List();
foreach (SensorLocation sensorLocation in SensorLocations)
{
if (sensorLocation.Group == (Group) e.Objects[0])
{
delList.Add(sensorLocation);
}
}
foreach (SensorLocation delLocation in delList)
{
SensorLocations.Remove(delLocation);
}
DataEventPublisher.DataListModified(SensorLocations, null);
}
}
}
private void DataEventPublisher_OnAfterChange(object sender, PublishEventArgs e)
{
if (sender is Sensor || sender is Group)
{
UpdatePickSensorsForGroups();
}
}
}