git_brcal/src/BreCalClient/ShipcallControlModel.cs
2023-10-04 07:34:16 +02:00

154 lines
4.2 KiB
C#

// Copyright (c) 2023 schick Informatik
// Description: Container model for shipcall related info
//
using BreCalClient.misc.Model;
using System;
using System.Collections.Generic;
namespace BreCalClient
{
/// <summary>
/// Container model to aggregate separate models for the Shipcall control data binding
/// </summary>
public class ShipcallControlModel
{
#region Enumerations
public enum TrafficLightMode
{
OFF,
GREEN,
YELLOW,
RED,
RED_YELLOW,
ALL
};
[Flags]
public enum StatusFlags
{
RED,
GREEN,
YELLOW,
BLINK_1,
BLINK_2
};
#endregion
#region Properties
public Shipcall? Shipcall { get; set; }
public Ship? Ship { get; set; }
public string? Berth { get; set; }
internal Dictionary<Extensions.ParticipantType, Participant> AssignedParticipants { get; } = new();
public List<Times> Times { get; set; } = new();
public TrafficLightMode LightMode
{
get
{
if (IsFlagSet(StatusFlags.RED))
{
if (IsFlagSet((StatusFlags)StatusFlags.YELLOW))
{
if (IsFlagSet(StatusFlags.GREEN))
{
return TrafficLightMode.ALL;
}
return TrafficLightMode.RED_YELLOW;
}
return TrafficLightMode.RED;
}
if (IsFlagSet(StatusFlags.YELLOW))
return TrafficLightMode.YELLOW;
if (IsFlagSet(StatusFlags.GREEN))
return TrafficLightMode.GREEN;
return TrafficLightMode.OFF;
}
}
#endregion
#region public methods
public void AssignParticipants(List<Participant> participants)
{
this.AssignedParticipants.Clear();
if (Shipcall != null)
{
foreach (int participantId in Shipcall.Participants)
{
Participant? participant = participants.Find((x) => x.Id == participantId);
if (participant != null)
{
foreach(Extensions.ParticipantType participantType in Enum.GetValues(typeof(Extensions.ParticipantType)))
{
if(participant.IsTypeFlagSet(participantType))
AssignedParticipants[participantType] = participant;
}
}
}
}
}
internal Times? GetTimesForParticipantType(Extensions.ParticipantType type)
{
if (AssignedParticipants.ContainsKey(type)) {
int participantId = AssignedParticipants[type].Id;
foreach (Times times in this.Times)
{
if ((times.ParticipantId == participantId) && (times.ParticipantType == (int) type))
return times;
}
}
return null;
}
public bool ContainsRemarkText(string text)
{
if(Shipcall != null)
{
foreach(Times times in this.Times)
{
if (times.Remarks == null) continue;
if(times.Remarks.Contains(text)) return true;
}
}
return false;
}
#endregion
#region helper
internal Participant? GetParticipantForType(Extensions.ParticipantType participantType)
{
foreach(Participant p in AssignedParticipants.Values)
{
if (p.IsTypeFlagSet(participantType))
return p;
}
return null;
}
private bool IsFlagSet(StatusFlags flag)
{
if(this.Shipcall == null) return false;
return (this.Shipcall.Flags & (int) flag) != 0;
}
#endregion
}
}