using System;
using System.ComponentModel;
namespace TestProject
{
/// <summary>
/// 구매 항목
/// </summary>
public class PurchaseItem : INotifyPropertyChanged, IEditableObject
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Event
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 속성 변경시 - PropertyChanged
/// <summary>
/// 속성 변경시
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Struct
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 항목 데이터 - ItemData
/// <summary>
/// 항목 데이터
/// </summary>
private struct ItemData
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Internal
#region Field
/// <summary>
/// 설명
/// </summary>
internal string Description;
/// <summary>
/// 제안 만기일
/// </summary>
internal DateTime OfferExpires;
/// <summary>
/// 가격
/// </summary>
internal double Price;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Internal
#region 새 항목 구하기 - GetNewItem()
/// <summary>
/// 새 항목 구하기
/// </summary>
/// <returns>새 항목</returns>
internal static ItemData GetNewItem()
{
ItemData data = new ItemData
{
Description = "New item",
Price = 0,
OfferExpires = DateTime.Now + new TimeSpan(7, 0, 0, 0)
};
return data;
}
#endregion
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 복사 데이터
/// </summary>
private ItemData copyData = ItemData.GetNewItem();
/// <summary>
/// 현대 데이터
/// </summary>
private ItemData currentData = ItemData.GetNewItem();
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 설명 - Description
/// <summary>
/// 설명
/// </summary>
public string Description
{
get
{
return this.currentData.Description;
}
set
{
if(this.currentData.Description != value)
{
this.currentData.Description = value;
FirePropertyChangedEvent("Description");
}
}
}
#endregion
#region 가격 - Price
/// <summary>
/// 가격
/// </summary>
public double Price
{
get
{
return this.currentData.Price;
}
set
{
if(this.currentData.Price != value)
{
this.currentData.Price = value;
FirePropertyChangedEvent("Price");
}
}
}
#endregion
#region 제안 만기일 - OfferExpires
/// <summary>
/// 제안 만기일
/// </summary>
public DateTime OfferExpires
{
get
{
return this.currentData.OfferExpires;
}
set
{
if(value != this.currentData.OfferExpires)
{
this.currentData.OfferExpires = value;
FirePropertyChangedEvent("OfferExpires");
}
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - PurchaseItem()
/// <summary>
/// 생성자
/// </summary>
public PurchaseItem()
{
}
#endregion
#region 생성자 - PurchaseItem(description, price, offerExpires)
/// <summary>
/// 생성자
/// </summary>
/// <param name="description">설명</param>
/// <param name="price">가격</param>
/// <param name="offerExpires">제안 만기일</param>
public PurchaseItem(string description, double price, DateTime offerExpires)
{
Description = description;
Price = price;
OfferExpires = offerExpires;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 편집 시작하기 - BeginEdit()
/// <summary>
/// 편집 시작하기
/// </summary>
public void BeginEdit()
{
this.copyData = this.currentData;
}
#endregion
#region 편집 취소하기 - CancelEdit()
/// <summary>
/// 편집 취소하기
/// </summary>
public void CancelEdit()
{
this.currentData = this.copyData;
FirePropertyChangedEvent("");
}
#endregion
#region 편집 종료하기 - EndEdit()
/// <summary>
/// 편집 종료하기
/// </summary>
public void EndEdit()
{
this.copyData = ItemData.GetNewItem();
}
#endregion
#region 문자열 구하기 - ToString()
/// <summary>
/// 문자열 구하기
/// </summary>
/// <returns>문자열</returns>
public override string ToString() => $"{Description}, {Price:c}, {OfferExpires:D}";
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Protected
#region 속성 변경시 이벤트 발생시키기 - FirePropertyChangedEvent(propertyName)
/// <summary>
/// 속성 변경시 이벤트 발생시키기
/// </summary>
/// <param name="propertyName">속성명</param>
private void FirePropertyChangedEvent(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}