use crate::times::{now, Duration, Timestamp};
use crate::types::NFTOwnable;
#[derive(candid::CandidType, serde::Deserialize, Debug, Default, Clone)]
pub struct ForbiddenDuration {
start: Timestamp,
end: Timestamp,
}
#[derive(candid::CandidType, serde::Deserialize, Debug, Default, Clone)]
pub struct NftTicket {
activity_start: Timestamp, activity_end: Timestamp, transfer_forbidden: Vec<ForbiddenDuration>, }
#[derive(candid::CandidType, serde::Deserialize, Debug)]
pub enum NftTicketStatus {
NoBody(Duration), InvalidToken, Forbidden(Duration), Owner(Duration, NFTOwnable), Anonymous(Duration, NFTOwnable), }
impl NftTicket {
pub fn can_transfer(&self) -> bool {
let now = now();
for ForbiddenDuration { start, end } in self.transfer_forbidden.iter() {
if start <= &now && &now < end {
return false;
}
}
true
}
pub fn ticket_status(&self) -> NftTicketStatus {
let now = now();
if now < self.activity_start {
return NftTicketStatus::NoBody(self.activity_start - now); } else if now < self.activity_end {
return NftTicketStatus::Owner(self.activity_end - now, NFTOwnable::None);
} else {
return NftTicketStatus::Anonymous(now - self.activity_end, NFTOwnable::None);
}
}
pub fn set_activity_start(&mut self, start: Timestamp) {
self.activity_start = start;
}
pub fn set_activity_end(&mut self, end: Timestamp) {
self.activity_end = end;
}
pub fn set_transfer_forbidden(&mut self, forbidden: Vec<ForbiddenDuration>) {
self.transfer_forbidden = forbidden;
}
pub fn get_activity(&self) -> (Timestamp, Timestamp) {
(self.activity_start, self.activity_end)
}
pub fn get_transfer_forbidden(&self) -> Vec<ForbiddenDuration> {
self.transfer_forbidden.clone()
}
}