using System;
using System.Collections.Generic;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
namespace ThreadPilot.Models
{
///
/// Condition types for profile triggers
///
public enum ProfileConditionType
{
SystemLoad,
TimeOfDay,
PowerState,
ProcessCount,
MemoryUsage,
CpuTemperature,
BatteryLevel,
NetworkActivity,
UserIdle,
Custom
}
///
/// Comparison operators for conditions
///
public enum ComparisonOperator
{
Equals,
NotEquals,
GreaterThan,
LessThan,
GreaterThanOrEqual,
LessThanOrEqual,
Contains,
NotContains,
Between,
NotBetween
}
///
/// Logical operators for combining conditions
///
public enum LogicalOperator
{
And,
Or,
Not
}
///
/// System state information for condition evaluation
///
public class SystemState
{
public double CpuUsage { get; set; }
public double MemoryUsage { get; set; }
public int ProcessCount { get; set; }
public DateTime CurrentTime { get; set; } = DateTime.Now;
public bool IsOnBattery { get; set; }
public int BatteryLevel { get; set; }
public double CpuTemperature { get; set; }
public bool IsUserIdle { get; set; }
public TimeSpan UserIdleTime { get; set; }
public double NetworkActivity { get; set; }
public Dictionary CustomProperties { get; set; } = new();
}
///
/// Individual condition for profile evaluation
///
public partial class ProfileCondition : ObservableObject
{
[ObservableProperty]
private string name = string.Empty;
[ObservableProperty]
private ProfileConditionType conditionType;
[ObservableProperty]
private ComparisonOperator comparisonOperator;
[ObservableProperty]
private object? value;
[ObservableProperty]
private object? secondaryValue; // For Between/NotBetween operations
[ObservableProperty]
private bool isEnabled = true;
[ObservableProperty]
private string description = string.Empty;
///
/// Evaluate this condition against the current system state
///
public bool Evaluate(ProcessModel process, SystemState systemState)
{
if (!IsEnabled) return true; // Disabled conditions are considered true
try
{
var actualValue = GetActualValue(process, systemState);
return CompareValues(actualValue, Value, SecondaryValue, ComparisonOperator);
}
catch (Exception)
{
return false; // Failed conditions are considered false
}
}
private object? GetActualValue(ProcessModel process, SystemState systemState)
{
return ConditionType switch
{
ProfileConditionType.SystemLoad => systemState.CpuUsage,
ProfileConditionType.TimeOfDay => systemState.CurrentTime.TimeOfDay.TotalHours,
ProfileConditionType.PowerState => systemState.IsOnBattery,
ProfileConditionType.ProcessCount => systemState.ProcessCount,
ProfileConditionType.MemoryUsage => systemState.MemoryUsage,
ProfileConditionType.CpuTemperature => systemState.CpuTemperature,
ProfileConditionType.BatteryLevel => systemState.BatteryLevel,
ProfileConditionType.NetworkActivity => systemState.NetworkActivity,
ProfileConditionType.UserIdle => systemState.IsUserIdle,
ProfileConditionType.Custom => systemState.CustomProperties.GetValueOrDefault(Name),
_ => null
};
}
private static bool CompareValues(object? actual, object? expected, object? secondary, ComparisonOperator op)
{
if (actual == null || expected == null) return false;
return op switch
{
ComparisonOperator.Equals => actual.Equals(expected),
ComparisonOperator.NotEquals => !actual.Equals(expected),
ComparisonOperator.GreaterThan => Comparer