// Copyright (C) Call of Nil contributors
// SPDX-License-Identifier: AGPL-3.0-only

use super::Food;
use super::diff::FoodDiff;
use crate::military::unit::UnitChunkSize;
use derive_more::{Deref, Display, Into};
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::num::NonZeroU32;
use std::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign};

/// Maintenance tax of an entity.
///
/// Its value is equivalent to a percentage of the [base cost].
///
/// [base cost]: crate::resources::cost::Cost
#[derive(
  Clone,
  Copy,
  Debug,
  Default,
  Deref,
  Display,
  Into,
  Deserialize,
  Serialize,
  PartialEq,
  Eq,
  PartialOrd,
  Ord,
)]
#[into(u32, f64, Food)]
pub struct Maintenance(Food);

impl Maintenance {
  #[inline]
  pub const fn new(value: u32) -> Self {
    Self(Food::new(value))
  }
}

impl From<u32> for Maintenance {
  fn from(value: u32) -> Self {
    Self::new(value)
  }
}

impl From<f64> for Maintenance {
  fn from(value: f64) -> Self {
    debug_assert!(value.is_finite());
    Self::new(value as u32)
  }
}

impl Add for Maintenance {
  type Output = Self;

  fn add(self, rhs: Self) -> Self {
    Self(self.0 + rhs.0)
  }
}

impl Add<Food> for Maintenance {
  type Output = Self;

  fn add(self, rhs: Food) -> Self {
    Self(self.0 + rhs)
  }
}

impl AddAssign for Maintenance {
  fn add_assign(&mut self, rhs: Self) {
    *self = Self(self.0 + rhs.0);
  }
}

impl AddAssign<Food> for Maintenance {
  fn add_assign(&mut self, rhs: Food) {
    *self = Self(self.0 + rhs);
  }
}

impl Sub for Maintenance {
  type Output = Self;

  fn sub(self, rhs: Self) -> Self {
    Self(self.0 - rhs.0)
  }
}

impl Sub<Food> for Maintenance {
  type Output = Self;

  fn sub(self, rhs: Food) -> Self {
    Self(self.0 - rhs)
  }
}

impl Sub<Maintenance> for Food {
  type Output = Self;

  fn sub(self, rhs: Maintenance) -> Self {
    self - rhs.0
  }
}

impl Sub<Maintenance> for FoodDiff {
  type Output = Self;

  fn sub(self, rhs: Maintenance) -> Self {
    self - rhs.0
  }
}

impl SubAssign for Maintenance {
  fn sub_assign(&mut self, rhs: Self) {
    *self = Self(self.0 - rhs.0);
  }
}

impl SubAssign<Food> for Maintenance {
  fn sub_assign(&mut self, rhs: Food) {
    *self = Self(self.0 - rhs);
  }
}

impl SubAssign<Maintenance> for Food {
  fn sub_assign(&mut self, rhs: Maintenance) {
    *self = *self - rhs.0;
  }
}

impl SubAssign<Maintenance> for FoodDiff {
  fn sub_assign(&mut self, rhs: Maintenance) {
    *self = *self - rhs.0;
  }
}

impl Mul<u32> for Maintenance {
  type Output = Maintenance;

  fn mul(self, rhs: u32) -> Self::Output {
    Self(self.0 * rhs)
  }
}

impl Mul<NonZeroU32> for Maintenance {
  type Output = Maintenance;

  fn mul(self, rhs: NonZeroU32) -> Self::Output {
    Self(self.0 * rhs.get())
  }
}

impl Div<UnitChunkSize> for Maintenance {
  type Output = Maintenance;

  fn div(self, rhs: UnitChunkSize) -> Self::Output {
    let maintenance = f64::from(self);
    let chunk_size = f64::from(rhs);
    Self::from(maintenance / chunk_size)
  }
}

impl PartialEq<Food> for Maintenance {
  fn eq(&self, other: &Food) -> bool {
    self.0.eq(other)
  }
}

impl PartialEq<Maintenance> for Food {
  fn eq(&self, other: &Maintenance) -> bool {
    self.eq(&other.0)
  }
}

impl PartialOrd<Food> for Maintenance {
  fn partial_cmp(&self, other: &Food) -> Option<Ordering> {
    self.0.partial_cmp(other)
  }
}

impl PartialOrd<Maintenance> for Food {
  fn partial_cmp(&self, other: &Maintenance) -> Option<Ordering> {
    self.partial_cmp(&other.0)
  }
}

/// Proportion of the base cost that should be used as a maintenance tax.
#[derive(Clone, Copy, Debug, Deref, Into, Deserialize, Serialize)]
pub struct MaintenanceRatio(f64);

impl MaintenanceRatio {
  #[inline]
  pub const fn new(ratio: f64) -> Self {
    debug_assert!(ratio.is_finite());
    debug_assert!(!ratio.is_subnormal());
    Self(ratio.clamp(0.0, 1.0))
  }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MaintenanceBalance {
  pub maintenance: Maintenance,
  pub production: Food,
}

impl MaintenanceBalance {
  /// Checks if the food production is enough to cover the maintenance costs.
  #[inline]
  pub fn is_sustainable(&self) -> bool {
    self.production >= self.maintenance
  }
}

impl Add<Maintenance> for MaintenanceBalance {
  type Output = Self;

  fn add(self, rhs: Maintenance) -> Self::Output {
    Self {
      maintenance: self.maintenance + rhs,
      production: self.production,
    }
  }
}
