# 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 DefineBoxAdaptive 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
from ansys.dyna.core.keywords.keyword_classes.auto.define.define_curve import DefineCurve

_DEFINEBOXADAPTIVE_CARD0 = (
    FieldSchema("boxid", int, 0, 10, None),
    FieldSchema("xmn", float, 10, 10, 0.0),
    FieldSchema("xmx", float, 20, 10, 0.0),
    FieldSchema("ymn", float, 30, 10, 0.0),
    FieldSchema("ymx", float, 40, 10, 0.0),
    FieldSchema("zmn", float, 50, 10, 0.0),
    FieldSchema("zmx", float, 60, 10, 0.0),
)

_DEFINEBOXADAPTIVE_CARD1 = (
    FieldSchema("pid", int, 0, 10, 0),
    FieldSchema("level", int, 10, 10, 1),
    FieldSchema("lidx_ndid", int, 20, 10, 0, "lidx/ndid"),
    FieldSchema("lidy", int, 30, 10, 0),
    FieldSchema("lidz", int, 40, 10, 0),
    FieldSchema("brmin", float, 50, 10, 0.0),
    FieldSchema("brmax", float, 60, 10, 0.0),
)

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

class DefineBoxAdaptive(KeywordBase):
    """DYNA DEFINE_BOX_ADAPTIVE keyword"""

    keyword = "DEFINE"
    subkeyword = "BOX_ADAPTIVE"
    option_specs = [
        OptionSpec("TITLE", -1, 1),
    ]
    _link_fields = {
        "lidx_ndid": LinkType.DEFINE_CURVE,
        "lidy": LinkType.DEFINE_CURVE,
        "lidz": LinkType.DEFINE_CURVE,
        "pid": LinkType.PART,
    }

    def __init__(self, **kwargs):
        """Initialize the DefineBoxAdaptive class."""
        super().__init__(**kwargs)
        kwargs["parent"] = self
        self._cards = [
            Card.from_field_schemas_with_defaults(
                _DEFINEBOXADAPTIVE_CARD0,
                **kwargs,
            ),            Card.from_field_schemas_with_defaults(
                _DEFINEBOXADAPTIVE_CARD1,
                **kwargs,
            ),            OptionCardSet(
                option_spec = DefineBoxAdaptive.option_specs[0],
                cards = [
                    Card.from_field_schemas_with_defaults(
                        _DEFINEBOXADAPTIVE_OPTION0_CARD0,
                        **kwargs,
                    ),
                ],
                **kwargs
            ),
        ]
    @property
    def boxid(self) -> typing.Optional[int]:
        """Get or set the Box ID. Define unique numbers.
        """ # nopep8
        return self._cards[0].get_value("boxid")

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

    @property
    def xmn(self) -> float:
        """Get or set the Minimum x-coordinate.
        """ # nopep8
        return self._cards[0].get_value("xmn")

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

    @property
    def xmx(self) -> float:
        """Get or set the Maximum x-coordinate.
        """ # nopep8
        return self._cards[0].get_value("xmx")

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

    @property
    def ymn(self) -> float:
        """Get or set the Minimum y-coordinate.
        """ # nopep8
        return self._cards[0].get_value("ymn")

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

    @property
    def ymx(self) -> float:
        """Get or set the Maximum y-coordinate.
        """ # nopep8
        return self._cards[0].get_value("ymx")

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

    @property
    def zmn(self) -> float:
        """Get or set the Minimum z-coordinate.
        """ # nopep8
        return self._cards[0].get_value("zmn")

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

    @property
    def zmx(self) -> float:
        """Get or set the Maximum z-coordinate.
        """ # nopep8
        return self._cards[0].get_value("zmx")

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

    @property
    def pid(self) -> int:
        """Get or set the Part ID. If zero, all active elements within box are considered.
        """ # nopep8
        return self._cards[1].get_value("pid")

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

    @property
    def level(self) -> int:
        """Get or set the Maximum number of refinement levels for elements that are contained in the box. Values of 1, 2, 3, 4,... allow a maximum of 1, 4, 16, 64, ...  elements, respectively, to be created for each original element.
        """ # nopep8
        return self._cards[1].get_value("level")

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

    @property
    def lidx_ndid(self) -> int:
        """Get or set the Load curve ID/Node ID.
        GT.0: load curve ID. Define adaptive box movement (displacement
        vs. time) in global X axis.
        LT.0: absolute value is a node ID, whose movement will be
        followed by the moving adaptive box. The node ID can be
        on a moving rigid body.
        EQ.0: no movement.
        """ # nopep8
        return self._cards[1].get_value("lidx_ndid")

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

    @property
    def lidy(self) -> int:
        """Get or set the Load curve ID.
        GT.0: load curve ID. Define adaptive box movement (displacement
        vs. time) in global Y axis.
        EQ.0: no movement.
        """ # nopep8
        return self._cards[1].get_value("lidy")

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

    @property
    def lidz(self) -> int:
        """Get or set the Load curve ID.
        GT.0: load curve ID. Define adaptive box movement (displacement
        vs. time) in global Y axis.
        EQ.0: no movement.
        """ # nopep8
        return self._cards[1].get_value("lidz")

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

    @property
    def brmin(self) -> float:
        """Get or set the Minimum mesh size in 3D tetrahedron adaptivity.
        """ # nopep8
        return self._cards[1].get_value("brmin")

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

    @property
    def brmax(self) -> float:
        """Get or set the Maximum mesh size in 3D tetrahedron adaptivity.
        """ # nopep8
        return self._cards[1].get_value("brmax")

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

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

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

        if value:
            self.activate_option("TITLE")

    @property
    def lidx_ndid_link(self) -> typing.Optional[DefineCurve]:
        """Get the DefineCurve object for lidx_ndid."""
        if self.deck is None:
            return None
        for kwd in self.deck.get_kwds_by_full_type("DEFINE", "CURVE"):
            if kwd.lcid == self.lidx_ndid:
                return kwd
        return None

    @lidx_ndid_link.setter
    def lidx_ndid_link(self, value: DefineCurve) -> None:
        """Set the DefineCurve object for lidx_ndid."""
        self.lidx_ndid = value.lcid

    @property
    def lidy_link(self) -> typing.Optional[DefineCurve]:
        """Get the DefineCurve object for lidy."""
        if self.deck is None:
            return None
        for kwd in self.deck.get_kwds_by_full_type("DEFINE", "CURVE"):
            if kwd.lcid == self.lidy:
                return kwd
        return None

    @lidy_link.setter
    def lidy_link(self, value: DefineCurve) -> None:
        """Set the DefineCurve object for lidy."""
        self.lidy = value.lcid

    @property
    def lidz_link(self) -> typing.Optional[DefineCurve]:
        """Get the DefineCurve object for lidz."""
        if self.deck is None:
            return None
        for kwd in self.deck.get_kwds_by_full_type("DEFINE", "CURVE"):
            if kwd.lcid == self.lidz:
                return kwd
        return None

    @lidz_link.setter
    def lidz_link(self, value: DefineCurve) -> None:
        """Set the DefineCurve object for lidz."""
        self.lidz = value.lcid

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

