# 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 DefineSphMeshSurface 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

_DEFINESPHMESHSURFACE_CARD0 = (
    FieldSchema("sid", int, 0, 10, None),
    FieldSchema("type", int, 10, 10, 0),
    FieldSchema("sphpid", int, 20, 10, None),
    FieldSchema("sphxid", int, 30, 10, None),
    FieldSchema("nsid", int, 40, 10, None),
    FieldSchema("space", float, 50, 10, 0.0),
    FieldSchema("iout", int, 60, 10, 0),
)

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

class DefineSphMeshSurface(KeywordBase):
    """DYNA DEFINE_SPH_MESH_SURFACE keyword"""

    keyword = "DEFINE"
    subkeyword = "SPH_MESH_SURFACE"
    option_specs = [
        OptionSpec("TITLE", -1, 1),
    ]
    _link_fields = {
        "sphxid": LinkType.SECTION,
        "nsid": LinkType.SET_NODE,
        "sphpid": LinkType.PART,
    }

    def __init__(self, **kwargs):
        """Initialize the DefineSphMeshSurface class."""
        super().__init__(**kwargs)
        kwargs["parent"] = self
        self._cards = [
            Card.from_field_schemas_with_defaults(
                _DEFINESPHMESHSURFACE_CARD0,
                **kwargs,
            ),            OptionCardSet(
                option_spec = DefineSphMeshSurface.option_specs[0],
                cards = [
                    Card.from_field_schemas_with_defaults(
                        _DEFINESPHMESHSURFACE_OPTION0_CARD0,
                        **kwargs,
                    ),
                ],
                **kwargs
            ),
        ]
    @property
    def sid(self) -> typing.Optional[int]:
        """Get or set the Part or part set ID for the region of the mesh upon which the SPH elements will be placed.
        """ # nopep8
        return self._cards[0].get_value("sid")

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

    @property
    def type(self) -> int:
        """Get or set the SID type:
        EQ.0:	part set ID
        EQ.1 : part ID.
        """ # nopep8
        return self._cards[0].get_value("type")

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

    @property
    def sphpid(self) -> typing.Optional[int]:
        """Get or set the Part ID for generated SPH elements.
        """ # nopep8
        return self._cards[0].get_value("sphpid")

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

    @property
    def sphxid(self) -> typing.Optional[int]:
        """Get or set the Section ID for generated SPH elements
        """ # nopep8
        return self._cards[0].get_value("sphxid")

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

    @property
    def nsid(self) -> typing.Optional[int]:
        """Get or set the If defined, this card creates a node set with ID NSID (see *SET_NODE) for the nodes generated by this card.
        """ # nopep8
        return self._cards[0].get_value("nsid")

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

    @property
    def space(self) -> float:
        """Get or set the Maximum space between SPH elements.
        """ # nopep8
        return self._cards[0].get_value("space")

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

    @property
    def iout(self) -> int:
        """Get or set the Keyword file output:
        EQ.0:	no output(default)
        EQ.1 : output generated nodes, SPH elementsand node set to a keyword file with SPH_surface_ prefix.
        """ # nopep8
        return self._cards[0].get_value("iout")

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

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

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

        if value:
            self.activate_option("TITLE")

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

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

    @property
    def nsid_link(self) -> typing.Optional[KeywordBase]:
        """Get the SET_NODE_* keyword for nsid."""
        return self._get_set_link("NODE", self.nsid)

    @nsid_link.setter
    def nsid_link(self, value: KeywordBase) -> None:
        """Set the SET_NODE_* keyword for nsid."""
        self.nsid = value.sid

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

