"""Formula 9.4 from EN 1992-1-1:2004: Chapter 9 - Detailing of members and particular rules."""

import numpy as np

from blueprints.codes.eurocode.en_1992_1_1_2004 import EN_1992_1_1_2004
from blueprints.codes.formula import Formula
from blueprints.codes.latex_formula import LatexFormula
from blueprints.type_alias import DEG, DIMENSIONLESS, MM, MM2
from blueprints.validations import raise_if_greater_than_90, raise_if_negative


class Form9Dot4ShearReinforcementRatio(Formula):
    """Class representing the formula 9.4 for the calculation of the shear reinforcement ratio."""

    label = "9.4"
    source_document = EN_1992_1_1_2004

    def __init__(
        self,
        a_sw: MM2,
        s: MM,
        b_w: MM,
        alpha: DEG,
    ) -> None:
        r"""[$\rho_w$] Shear reinforcement ratio [$-$].

        EN 1992-1-1:2004 art.9.2.2(5) - Formula (9.4)

        Parameters
        ----------
        a_sw: MM2
            [$A_{sw}$] Area of shear reinforcement within length s [$mm^2$].
        s: MM
            [$s$] The spacing between shear reinforcement along the longitudinal axis of the element [$mm$].
        b_w: MM
            [$b_w$] The width of the web of the element [$mm$].
        alpha: DEG
            [$\alpha$] The angle between the shear reinforcement and the longitudinal axis of the beam (see 9.2.2(1)) [$deg$].
        """
        super().__init__()
        self.a_sw = a_sw
        self.s = s
        self.b_w = b_w
        self.alpha = alpha

    @staticmethod
    def _evaluate(
        a_sw: MM2,
        s: MM,
        b_w: MM,
        alpha: DEG,
    ) -> DIMENSIONLESS:
        """For more detailed documentation see the class docstring."""
        raise_if_negative(a_sw=a_sw, s=s, b_w=b_w, alpha=alpha)
        raise_if_greater_than_90(alpha=alpha)

        # Convert the angle from degrees to radians
        alpha_radians = np.deg2rad(alpha)

        return a_sw / (s * b_w * np.sin(alpha_radians))

    def latex(self, n: int = 2) -> LatexFormula:
        """Returns LatexFormula object for formula 9.4."""
        return LatexFormula(
            return_symbol=r"\rho_w",
            result=f"{self:.6f}",
            equation=r"\frac{A_{sw}}{s \cdot b_w \cdot sin(\alpha)}",
            numeric_equation=rf"\frac{{{self.a_sw:.{n}f}}}{{{self.s:.{n}f} \cdot {self.b_w:.{n}f} \cdot sin({self.alpha:.{n}f})}}",
            comparison_operator_label="=",
        )
