# Copyright (C) 2021 - 2024 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""Module providing the DefineAdaptiveSolidToDesId class."""
import typing
from ansys.dyna.core.lib.card import Card, Field, Flag
from ansys.dyna.core.lib.field_schema import FieldSchema
from ansys.dyna.core.lib.option_card import OptionCardSet, OptionSpec
from ansys.dyna.core.lib.keyword_base import KeywordBase
from ansys.dyna.core.lib.keyword_base import LinkType

_DEFINEADAPTIVESOLIDTODESID_CARD0 = (
    FieldSchema("did", int, 0, 10, None),
    FieldSchema("heading", str, 10, 70, None),
)

_DEFINEADAPTIVESOLIDTODESID_CARD1 = (
    FieldSchema("ipid", int, 0, 10, None),
    FieldSchema("itype", int, 10, 10, 0),
    FieldSchema("nq", int, 20, 10, 1),
    FieldSchema("ipdes", int, 30, 10, None),
    FieldSchema("isdes", int, 40, 10, None),
    FieldSchema("rsf", float, 50, 10, 1.0),
    FieldSchema("outdes", int, 60, 10, 0),
    FieldSchema("ibond", int, 70, 10, 0),
)

_DEFINEADAPTIVESOLIDTODESID_CARD2 = (
    FieldSchema("pbn", float, 0, 10, None),
    FieldSchema("pbs", float, 10, 10, None),
    FieldSchema("pbn_s", float, 20, 10, None),
    FieldSchema("pbs_s", float, 30, 10, None),
    FieldSchema("sfa", float, 40, 10, 1.0),
    FieldSchema("alpha", float, 50, 10, 0.0),
)

_DEFINEADAPTIVESOLIDTODESID_OPTION0_CARD0 = (
    FieldSchema("title", str, 0, 80, None),
)

class DefineAdaptiveSolidToDesId(KeywordBase):
    """DYNA DEFINE_ADAPTIVE_SOLID_TO_DES_ID keyword"""

    keyword = "DEFINE"
    subkeyword = "ADAPTIVE_SOLID_TO_DES_ID"
    option_specs = [
        OptionSpec("TITLE", -1, 1),
    ]
    _link_fields = {
        "isdes": LinkType.SECTION,
        "ipdes": LinkType.PART,
    }

    def __init__(self, **kwargs):
        """Initialize the DefineAdaptiveSolidToDesId class."""
        super().__init__(**kwargs)
        kwargs["parent"] = self
        self._cards = [
            Card.from_field_schemas_with_defaults(
                _DEFINEADAPTIVESOLIDTODESID_CARD0,
                **kwargs,
            ),            Card.from_field_schemas_with_defaults(
                _DEFINEADAPTIVESOLIDTODESID_CARD1,
                **kwargs,
            ),            Card.from_field_schemas_with_defaults(
                _DEFINEADAPTIVESOLIDTODESID_CARD2,
                **kwargs,
            ),            OptionCardSet(
                option_spec = DefineAdaptiveSolidToDesId.option_specs[0],
                cards = [
                    Card.from_field_schemas_with_defaults(
                        _DEFINEADAPTIVESOLIDTODESID_OPTION0_CARD0,
                        **kwargs,
                    ),
                ],
                **kwargs
            ),
        ]
    @property
    def did(self) -> typing.Optional[int]:
        """Get or set the Definition ID. This must be a unique number..
        """ # nopep8
        return self._cards[0].get_value("did")

    @did.setter
    def did(self, value: int) -> None:
        """Set the did property."""
        self._cards[0].set_value("did", value)

    @property
    def heading(self) -> typing.Optional[str]:
        """Get or set the Definition descriptor. It is suggested that unique descriptions be	used.
        """ # nopep8
        return self._cards[0].get_value("heading")

    @heading.setter
    def heading(self, value: str) -> None:
        """Set the heading property."""
        self._cards[0].set_value("heading", value)

    @property
    def ipid(self) -> typing.Optional[int]:
        """Get or set the ID of the solid part or part set to transform.
        """ # nopep8
        return self._cards[1].get_value("ipid")

    @ipid.setter
    def ipid(self, value: int) -> None:
        """Set the ipid property."""
        self._cards[1].set_value("ipid", value)

    @property
    def itype(self) -> int:
        """Get or set the Set type of the IPID
        EQ. 0: Part ID
        NE. 0: Part set ID
        """ # nopep8
        return self._cards[1].get_value("itype")

    @itype.setter
    def itype(self, value: int) -> None:
        """Set the itype property."""
        if value not in [0, 1, None]:
            raise Exception("""itype must be `None` or one of {0,1}.""")
        self._cards[1].set_value("itype", value)

    @property
    def nq(self) -> int:
        """Get or set the Adaptive option for hexahedral elements. For tetrahedral and
        pentahedral elements, see Remark 1:
        EQ.1: Adapt one solid element to one discrete element,
        EQ.2: Adapt one solid element to 8 discrete elements,
        EQ.3: Adapt one solid element to 27 discrete elements.
        """ # nopep8
        return self._cards[1].get_value("nq")

    @nq.setter
    def nq(self, value: int) -> None:
        """Set the nq property."""
        if value not in [1, 2, 3, None]:
            raise Exception("""nq must be `None` or one of {1,2,3}.""")
        self._cards[1].set_value("nq", value)

    @property
    def ipdes(self) -> typing.Optional[int]:
        """Get or set the Part ID for newly generated discrete elements, See Remark 2.
        """ # nopep8
        return self._cards[1].get_value("ipdes")

    @ipdes.setter
    def ipdes(self, value: int) -> None:
        """Set the ipdes property."""
        self._cards[1].set_value("ipdes", value)

    @property
    def isdes(self) -> typing.Optional[int]:
        """Get or set the Section ID for discrete elements, See Remark 2.
        """ # nopep8
        return self._cards[1].get_value("isdes")

    @isdes.setter
    def isdes(self, value: int) -> None:
        """Set the isdes property."""
        self._cards[1].set_value("isdes", value)

    @property
    def rsf(self) -> float:
        """Get or set the DES radius scale down factor, which is the ratio of the radius of the generated DES to the calculated radius based on volume consistency.
        """ # nopep8
        return self._cards[1].get_value("rsf")

    @rsf.setter
    def rsf(self, value: float) -> None:
        """Set the rsf property."""
        self._cards[1].set_value("rsf", value)

    @property
    def outdes(self) -> int:
        """Get or set the Allow user output generated discrete element nodes and DES properties toa keyword file.
        EQ.0:	No output. (Default)
        EQ.1:	Write data under filename, desvfill.inc.
        """ # nopep8
        return self._cards[1].get_value("outdes")

    @outdes.setter
    def outdes(self, value: int) -> None:
        """Set the outdes property."""
        if value not in [0, 1, None]:
            raise Exception("""outdes must be `None` or one of {0,1}.""")
        self._cards[1].set_value("outdes", value)

    @property
    def ibond(self) -> int:
        """Get or set the Allow user define bonds between DES generated from the same solid element.
        EQ.0:	No bonds. (Default)
        EQ.1:	Bonds generated, need to define Card 2.
        """ # nopep8
        return self._cards[1].get_value("ibond")

    @ibond.setter
    def ibond(self, value: int) -> None:
        """Set the ibond property."""
        if value not in [0, 1, None]:
            raise Exception("""ibond must be `None` or one of {0,1}.""")
        self._cards[1].set_value("ibond", value)

    @property
    def pbn(self) -> typing.Optional[float]:
        """Get or set the Parallel-bond modulus [Pa]. See Remark 1 in *DEFINE_DE_BOND.
        """ # nopep8
        return self._cards[2].get_value("pbn")

    @pbn.setter
    def pbn(self, value: float) -> None:
        """Set the pbn property."""
        self._cards[2].set_value("pbn", value)

    @property
    def pbs(self) -> typing.Optional[float]:
        """Get or set the Parallel-bond stiffness ratio. Shear stiffness/normal stiffness. See Remark 2 in *DEFINE_DE_BOND
        """ # nopep8
        return self._cards[2].get_value("pbs")

    @pbs.setter
    def pbs(self, value: float) -> None:
        """Set the pbs property."""
        self._cards[2].set_value("pbs", value)

    @property
    def pbn_s(self) -> typing.Optional[float]:
        """Get or set the Parallel-bond maximum normal stress. A zero value defines an infinite maximum normal stress
        """ # nopep8
        return self._cards[2].get_value("pbn_s")

    @pbn_s.setter
    def pbn_s(self, value: float) -> None:
        """Set the pbn_s property."""
        self._cards[2].set_value("pbn_s", value)

    @property
    def pbs_s(self) -> typing.Optional[float]:
        """Get or set the Parallel-bond maximum shear stress. A zero value defines an infinite maximum shear stress.
        """ # nopep8
        return self._cards[2].get_value("pbs_s")

    @pbs_s.setter
    def pbs_s(self, value: float) -> None:
        """Set the pbs_s property."""
        self._cards[2].set_value("pbs_s", value)

    @property
    def sfa(self) -> float:
        """Get or set the Bond radius multiplier. Default is 1.
        """ # nopep8
        return self._cards[2].get_value("sfa")

    @sfa.setter
    def sfa(self, value: float) -> None:
        """Set the sfa property."""
        self._cards[2].set_value("sfa", value)

    @property
    def alpha(self) -> float:
        """Get or set the Numerical damping.
        """ # nopep8
        return self._cards[2].get_value("alpha")

    @alpha.setter
    def alpha(self, value: float) -> None:
        """Set the alpha property."""
        self._cards[2].set_value("alpha", value)

    @property
    def title(self) -> typing.Optional[str]:
        """Get or set the Additional title line
        """ # nopep8
        return self._cards[3].cards[0].get_value("title")

    @title.setter
    def title(self, value: str) -> None:
        """Set the title property."""
        self._cards[3].cards[0].set_value("title", value)

        if value:
            self.activate_option("TITLE")

    @property
    def isdes_link(self) -> typing.Optional[KeywordBase]:
        """Get the SECTION_* keyword for isdes."""
        if self.deck is None:
            return None
        for kwd in self.deck.get_kwds_by_type("SECTION"):
            if kwd.secid == self.isdes:
                return kwd
        return None

    @isdes_link.setter
    def isdes_link(self, value: KeywordBase) -> None:
        """Set the SECTION_* keyword for isdes."""
        self.isdes = value.secid

    @property
    def ipdes_link(self) -> typing.Optional[KeywordBase]:
        """Get the PART keyword containing the given ipdes."""
        return self._get_link_by_attr("PART", "pid", self.ipdes, "parts")

