from __future__ import annotations
from typing import Dict, Any
import dataclasses
from .client_extensions import ClientExtensions
from .stop_loss_order_request_time_in_force import StopLossOrderRequestTimeInForce
from .stop_loss_order_request_time_in_force import (
    check_stop_loss_order_request_time_in_force,
)
from .stop_loss_order_request_trigger_condition import (
    StopLossOrderRequestTriggerCondition,
)
from .stop_loss_order_request_trigger_condition import (
    check_stop_loss_order_request_trigger_condition,
)
from .stop_loss_order_request_type import StopLossOrderRequestType
from .stop_loss_order_request_type import check_stop_loss_order_request_type
from typing import Optional, Type, TypeVar

T = TypeVar("T", bound="StopLossOrderRequest")


@dataclasses.dataclass
class StopLossOrderRequest:
    """A StopLossOrderRequest specifies the parameters that may be set when creating a Stop Loss Order. Only one of the
    price and distance fields may be specified.

        Attributes:
            type (Optional[StopLossOrderRequestType]): The type of the Order to Create. Must be set to "STOP_LOSS" when
                creating a Stop Loss Order.
            trade_id (Optional[str]): The ID of the Trade to close when the price threshold is breached.
            client_trade_id (Optional[str]): The client ID of the Trade to be closed when the price threshold is
                breached.
            price (Optional[str]): The price threshold specified for the Stop Loss Order. If the guaranteed flag is
                false, the associated Trade will be closed by a market price that is equal to or worse than this threshold. If
                the flag is true the associated Trade will be closed at this price.
            distance (Optional[str]): Specifies the distance (in price units) from the Account's current price to use as
                the Stop Loss Order price. If the Trade is short the Instrument's bid price is used, and for long Trades the ask
                is used.
            time_in_force (Optional[StopLossOrderRequestTimeInForce]): The time-in-force requested for the StopLoss
                Order. Restricted to "GTC", "GFD" and "GTD" for StopLoss Orders.
            gtd_time (Optional[str]): The date/time when the StopLoss Order will be cancelled if its timeInForce is
                "GTD".
            trigger_condition (Optional[StopLossOrderRequestTriggerCondition]): Specification of which price component
                should be used when determining if an Order should be triggered and filled. This allows Orders to be triggered
                based on the bid, ask, mid, default (ask for buy, bid for sell) or inverse (ask for sell, bid for buy) price
                depending on the desired behaviour. Orders are always filled using their default price component.
                This feature is only provided through the REST API. Clients who choose to specify a non-default trigger
                condition will not see it reflected in any of OANDA's proprietary or partner trading platforms, their
                transaction history or their account statements. OANDA platforms always assume that an Order's trigger condition
                is set to the default value when indicating the distance from an Order's trigger price, and will always provide
                the default trigger condition when creating or modifying an Order.
                A special restriction applies when creating a guaranteed Stop Loss Order. In this case the TriggerCondition
                value must either be "DEFAULT", or the "natural" trigger side "DEFAULT" results in. So for a Stop Loss Order for
                a long trade valid values are "DEFAULT" and "BID", and for short trades "DEFAULT" and "ASK" are valid.
            guaranteed (Optional[bool]): Flag indicating that the Stop Loss Order is guaranteed. The default value
                depends on the GuaranteedStopLossOrderMode of the account, if it is REQUIRED, the default will be true, for
                DISABLED or ENABLED the default is false.
            client_extensions (Optional[ClientExtensions]): A ClientExtensions object allows a client to attach a
                clientID, tag and comment to Orders and Trades in their Account.  Do not set, modify, or delete this field if
                your account is associated with MT4."""

    type: Optional[StopLossOrderRequestType]
    trade_id: Optional[str]
    client_trade_id: Optional[str]
    price: Optional[str]
    distance: Optional[str]
    time_in_force: Optional[StopLossOrderRequestTimeInForce]
    gtd_time: Optional[str]
    trigger_condition: Optional[StopLossOrderRequestTriggerCondition]
    guaranteed: Optional[bool]
    client_extensions: Optional["ClientExtensions"]

    @classmethod
    def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
        from .client_extensions import ClientExtensions

        d = src_dict.copy()
        _type = d.pop("type", None)
        type: Optional[StopLossOrderRequestType]
        if _type is None:
            type = None
        else:
            type = check_stop_loss_order_request_type(_type)
        trade_id = d.pop("tradeID", None)
        client_trade_id = d.pop("clientTradeID", None)
        price = d.pop("price", None)
        distance = d.pop("distance", None)
        _time_in_force = d.pop("timeInForce", None)
        time_in_force: Optional[StopLossOrderRequestTimeInForce]
        if _time_in_force is None:
            time_in_force = None
        else:
            time_in_force = check_stop_loss_order_request_time_in_force(_time_in_force)
        gtd_time = d.pop("gtdTime", None)
        _trigger_condition = d.pop("triggerCondition", None)
        trigger_condition: Optional[StopLossOrderRequestTriggerCondition]
        if _trigger_condition is None:
            trigger_condition = None
        else:
            trigger_condition = check_stop_loss_order_request_trigger_condition(
                _trigger_condition
            )
        guaranteed = d.pop("guaranteed", None)
        _client_extensions = d.pop("clientExtensions", None)
        client_extensions: Optional[ClientExtensions]
        if _client_extensions is None:
            client_extensions = None
        else:
            client_extensions = ClientExtensions.from_dict(_client_extensions)
        stop_loss_order_request = cls(
            type=type,
            trade_id=trade_id,
            client_trade_id=client_trade_id,
            price=price,
            distance=distance,
            time_in_force=time_in_force,
            gtd_time=gtd_time,
            trigger_condition=trigger_condition,
            guaranteed=guaranteed,
            client_extensions=client_extensions,
        )
        stop_loss_order_request.additional_properties = d
        return stop_loss_order_request

    def to_dict(self) -> Dict[str, Any]:
        """Convert the dataclass instance to a dictionary."""
        return dataclasses.asdict(self)
