using SheetMe.Core.Catalog;
using SheetMe.Data.Stores;
namespace SheetMe.Designer.Services;
///
/// 지금 고른 환자 — 앱 전체가 하나를 공유한다.
///
/// 왜 전역인가. 환자는 미리보기 창에서 고르는데, 그 값이 필요한 곳은 거기만이 아니다 —
/// 쿼리 편집기의 '검증 실행'도 같은 환자로 돌려야 의미가 있다.
/// 창마다 따로 들고 있으면 같은 서식을 두 화면에서 서로 다른 환자로 보게 되고,
/// 그건 화면으로 구분할 수 없다.
///
/// 하나만 둔다. 여러 개를 허용하면 어느 것이 지금 것인지 알 수 없고,
/// 환자 정보가 걸린 문제에서 그건 개인정보 사고가 된다.
///
/// 앱이 닫힐 때까지 메모리에 남는다. 디스크에 쓰지 않는다 —
/// 환자 문맥은 로그에도 안 남기기로 한 값이다().
///
public static class PatientSession
{
#region Member Fields
/// 환자가 바뀌었다 — 열려 있는 화면들이 다시 그려야 한다
public static event EventHandler? Changed;
#endregion
#region Properties
/// 지금 고른 환자의 문맥 — 안 골랐으면 null
public static PatientContext? Current { get; private set; }
/// 사람에게 보여 줄 한 줄 — 창 머리에 그대로 쓴다
public static string Label { get; private set; } = string.Empty;
/// 고른 환자가 있는가
public static bool HasPatient => Current is not null;
#endregion
#region Methods
/// 환자를 붙이거나(문맥 전달) 뗀다(null 전달)
public static void Set(PatientContext? context, string label)
{
Current = context;
Label = context is null ? string.Empty : label;
Changed?.Invoke(null, EventArgs.Empty);
}
///
/// 치환 변수원 — 환자가 없으면 null 이고, 호출부는 그때 자리표시로 구문만 검사해야 한다.
///
public static IQueryVariableSource? Variables()
=> Current is { } context
? new PatientQueryVariableSource(context, UserSession.Current.UidCod)
: null;
#endregion
}