// Access to values: on a `PropertyInfo` for `AnyValue`s, with all checks, and on an object for typed values.

///|
/// Fail when the property belongs to another registry than the object.
fn PropertyInfo::check_object(
  self : PropertyInfo,
  object : DependencyObject,
) -> Unit raise DependencyError {
  if self.registry_id != object.registry.id {
    raise RegistryMismatch(
      "`\{self}` belongs to another registry than the `\{object.owner_type}` object",
    )
  }
}

///|
/// Fail when a change with `key` is not allowed. A property that is not read-only has no key.
fn PropertyInfo::check_key(
  self : PropertyInfo,
  key : PropertyKeyInfo?,
) -> Unit raise DependencyError {
  match key {
    Some(key) if key.property != self =>
      raise ReadOnly("the key of `\{key.property}` cannot change `\{self}`")
    None if self.read_only => raise ReadOnly("`\{self}` is read-only")
    _ => ()
  }
}

///|
/// Fail when `value` has another key than the property, or when the validation rejects it.
fn PropertyInfo::check_value(
  self : PropertyInfo,
  value : AnyValue,
) -> Unit raise DependencyError {
  if value.key_id != self.value_key_id {
    raise InvalidValue(
      "a `\{value.key_id}` value cannot be set on `\{self}`, which has `\{self.value_key_id}` values",
    )
  }
  if !(self.validate)(value) {
    raise InvalidValue(
      "\{(self.show)(value)} is not a valid value of `\{self}`",
    )
  }
}

///|
/// The effective value on `object`, as `DependencyObject::get_value` gives it.
pub fn PropertyInfo::get_value(
  self : PropertyInfo,
  object : DependencyObject,
) -> AnyValue raise DependencyError {
  self.check_object(object)
  match object.stored(self) {
    Some(value) => value.effective()
    None => self.metadata(object.owner_type).default_value
  }
}

///|
/// The current value or the local value on `object`, as `DependencyObject::read_local_value` gives it.
pub fn PropertyInfo::read_local_value(
  self : PropertyInfo,
  object : DependencyObject,
) -> AnyValue? raise DependencyError {
  self.check_object(object)
  object.stored(self).bind(value => value.local_value())
}

///|
/// Set the local value on `object`, as `DependencyObject::set_value` does. The value key of the property has to have
/// wrapped `value`.
pub fn PropertyInfo::set_value(
  self : PropertyInfo,
  object : DependencyObject,
  value : AnyValue,
) -> Unit raise {
  self.check_object(object)
  self.check_key(None)
  object.change(self, Set(value))
}

///|
/// Set the current value on `object`, as `DependencyObject::set_current_value` does.
pub fn PropertyInfo::set_current_value(
  self : PropertyInfo,
  object : DependencyObject,
  value : AnyValue,
) -> Unit raise {
  self.check_object(object)
  self.check_key(None)
  object.change(self, SetCurrent(value))
}

///|
/// Remove the local value from `object`, as `DependencyObject::clear_value` does.
pub fn PropertyInfo::clear_value(
  self : PropertyInfo,
  object : DependencyObject,
) -> Unit raise {
  self.check_object(object)
  self.check_key(None)
  object.change(self, Clear)
}

///|
/// Coerce the value on `object` again, as `DependencyObject::coerce_value` does.
pub fn PropertyInfo::coerce_value(
  self : PropertyInfo,
  object : DependencyObject,
) -> Unit raise {
  self.check_object(object)
  object.change(self, Coerce)
}

///|
/// Set the local value of a read-only property on `object`. `key` has to be the key of this property.
pub fn PropertyInfo::set_with_key(
  self : PropertyInfo,
  object : DependencyObject,
  key : PropertyKeyInfo,
  value : AnyValue,
) -> Unit raise {
  self.check_object(object)
  self.check_key(Some(key))
  object.change(self, Set(value))
}

///|
/// Remove the local value of a read-only property from `object`. `key` has to be the key of this property.
pub fn PropertyInfo::clear_with_key(
  self : PropertyInfo,
  object : DependencyObject,
  key : PropertyKeyInfo,
) -> Unit raise {
  self.check_object(object)
  self.check_key(Some(key))
  object.change(self, Clear)
}

///|
/// The effective value: the local value, or else the inherited value, or else the default value for the type of the object,
/// then the current value instead when there is one, and then the result of the coercion. Every property of the registry has
/// a value on every object.
pub fn[T] DependencyObject::get_value(
  self : DependencyObject,
  property : DependencyProperty[T],
) -> T raise DependencyError {
  property.key.value_of(property.info.get_value(self))
}

///|
/// The current value when there is one, or else the local value. Both are without the coercion.
pub fn[T] DependencyObject::read_local_value(
  self : DependencyObject,
  property : DependencyProperty[T],
) -> T? raise DependencyError {
  property.info
  .read_local_value(self)
  .map(value => property.key.value_of(value))
}

///|
/// Set the local value, which discards a current value. An invalid value is an error and changes nothing.
pub fn[T] DependencyObject::set_value(
  self : DependencyObject,
  property : DependencyProperty[T],
  value : T,
) -> Unit raise {
  property.info.set_value(self, property.key.wrap(value))
}

///|
/// Change the effective value until the next `set_value` or `clear_value`, or until the object inherits another value and has
/// no local value. A control does this for its own changes, such as those by a click. An invalid value is an error and changes
/// nothing.
pub fn[T] DependencyObject::set_current_value(
  self : DependencyObject,
  property : DependencyProperty[T],
  value : T,
) -> Unit raise {
  property.info.set_current_value(self, property.key.wrap(value))
}

///|
/// Remove the local value and the current value, so that the object inherits again. A default value that becomes effective is
/// not coerced, and an inherited value is.
pub fn[T] DependencyObject::clear_value(
  self : DependencyObject,
  property : DependencyProperty[T],
) -> Unit raise {
  property.info.clear_value(self)
}

///|
/// Set the local value of a read-only property.
pub fn[T] DependencyObject::set_with_key(
  self : DependencyObject,
  key : DependencyPropertyKey[T],
  value : T,
) -> Unit raise {
  let property = key.property
  property.info.set_with_key(self, key.info(), property.key.wrap(value))
}

///|
/// Remove the local value of a read-only property.
pub fn[T] DependencyObject::clear_with_key(
  self : DependencyObject,
  key : DependencyPropertyKey[T],
) -> Unit raise {
  key.property.info.clear_with_key(self, key.info())
}

///|
/// Coerce the value again, after a change of something that the coercion reads. This coerces a default value too.
pub fn[T] DependencyObject::coerce_value(
  self : DependencyObject,
  property : DependencyProperty[T],
) -> Unit raise {
  property.info.coerce_value(self)
}

///|
/// The properties for which `read_local_value` has a value, with that value, in the order of their registration. The result
/// is a copy.
pub fn DependencyObject::local_values(
  self : DependencyObject,
) -> Array[(PropertyInfo, AnyValue)] {
  let values = []
  for value in self.values {
    if value.local_value() is Some(found) {
      values.push((value.property, found))
    }
  }
  values
}