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

use super::Food;
use super::diff::FoodDiff;
use crate::military::army::Army;
use crate::military::army::personnel::ArmyPersonnel;
use crate::military::squad::Squad;
use crate::military::unit::UnitChunkSize;
use derive_more::Display;
use nil_util::ConstDeref;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::iter::Sum;
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(Copy, Debug, Display, Deserialize, Serialize, ConstDeref)]
#[derive_const(Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
pub struct Maintenance(Food);

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

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

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

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

const impl From<Maintenance> for f64 {
  fn from(value: Maintenance) -> Self {
    f64::from(value.0)
  }
}

impl<'a> Sum<&'a Squad> for Maintenance {
  fn sum<I>(iter: I) -> Self
  where
    I: Iterator<Item = &'a Squad>,
  {
    iter.fold(Maintenance::default(), |mut acc, squad| {
      acc += squad.maintenance();
      acc
    })
  }
}

impl<'a> Sum<&'a ArmyPersonnel> for Maintenance {
  fn sum<I>(iter: I) -> Self
  where
    I: Iterator<Item = &'a ArmyPersonnel>,
  {
    iter.flat_map(ArmyPersonnel::iter).sum()
  }
}

impl<'a> Sum<&'a Army> for Maintenance {
  fn sum<I>(iter: I) -> Self
  where
    I: Iterator<Item = &'a Army>,
  {
    iter.flat_map(Army::iter).sum()
  }
}

const impl Add for Maintenance {
  type Output = Self;

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

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

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

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

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

const impl Sub for Maintenance {
  type Output = Self;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

const 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)
  }
}

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

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

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

const 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(Copy, Debug, Display, Deserialize, Serialize, ConstDeref)]
#[derive_const(Clone, PartialEq, PartialOrd)]
#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
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))
  }
}

const impl From<MaintenanceRatio> for f64 {
  fn from(value: MaintenanceRatio) -> Self {
    value.0
  }
}

#[derive(Debug, Deserialize, Serialize)]
#[derive_const(Clone)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
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 const fn is_sustainable(&self) -> bool {
    self.production >= self.maintenance
  }
}

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

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