use std::{fmt, ops::{Add, Sub, Neg}};
use rand::{Rng, thread_rng};
use crate::Rotation;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Delta {
    pub dx: i32,
    pub dy: i32,
}
impl Delta {
    pub const ZERO: Self = Self::new(0, 0);
    pub const LEFT:  Self = Self::new(-1,  0);
    pub const UP:    Self = Self::new( 0, -1);
    pub const RIGHT: Self = Self::new( 1,  0);
    pub const DOWN:  Self = Self::new( 0,  1);
    pub const fn new(dx: i32, dy: i32) -> Self {
        Self { dx, dy }
    }
    pub fn random_cardinal_with(rng: &mut impl Rng) -> Self {
        Rotation::random_cardinal_with(rng) * Self::RIGHT
    }
    pub fn random_cardinal() -> Self {
        Self::random_cardinal_with(&mut thread_rng())
    }
}
impl fmt::Display for Delta {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "({}, {})", self.dx, self.dy)
    }
}
impl Add for Delta {
    type Output = Delta;
    fn add(self, rhs: Self) -> Self {
        Self::new(self.dx + rhs.dx, self.dy + rhs.dy)
    }
}
impl Sub for Delta {
    type Output = Delta;
    fn sub(self, rhs: Self) -> Self {
        Self::new(self.dx - rhs.dx, self.dy - rhs.dy)
    }
}
impl Neg for Delta {
    type Output = Delta;
    fn neg(self) -> Self {
        Self::new(-self.dx, -self.dy)
    }
}