from orderly_evm_connector.lib.utils import check_required_parameters, get_timestamp
from orderly_evm_connector.lib.utils import get_withdraw_settle_verifyingcontract


def get_settle_pnl_nonce(self):
    """[Private] Get Settle PnL nonce

    Limit: 10 requests per 1 second

    GET /v1/settle_nonce

    Retrieve a nonce used for requesting a withdrawal on Orderly Network. Each nonce can only be used once.

    https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-settle-pnl-nonce
    """
    return self._sign_request("GET", "/v1/settle_nonce")


def request_pnl_settlement(
    self,
    brokerId: str,
    chainId: int,
    settleNonce: int,
    userAddress: str,
    # verifyingContract: str,
):
    """[Private] Request PnL Settlement

    Limit: 1 requests per 1 second

    POST /v1/settle_pnl

    Args:
    message(JSON): Message object containing the message that is signed by the wallet owner
    message.brokerId(string): Broker ID
    message.chainId(int): Chain ID of registering chain (within those that are supported by the Network)
    message.settleNonce(int): once from Get settle PnL Nonce
    message.timestamp(timestamp): current timestamp in UNIX milliseconds
    signature(string): The signature generated by signing the message object via EIP-712
    userAddress(string): The address of the wallet signing the message object via EIP-712

    https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/request-pnl-settlement
    """

    check_required_parameters(
        [
            [brokerId, "brokerId"],
            [chainId, "chainId"],
            [settleNonce, "settleNonce"],
            [userAddress, "userAdress"],
            # [verifyingContract, "verifyingContract"],
        ]
    )
    verifyingContract = get_withdraw_settle_verifyingcontract(self.orderly_testnet)
    _message = {
        "brokerId": brokerId,
        "chainId": chainId,
        "settleNonce": settleNonce,
        "userAddress": userAddress,
        "timestamp": int(get_timestamp()),
    }
    message = {
        "domain": {
            "name": "Orderly",
            "version": "1",
            "chainId": chainId,
            "verifyingContract": verifyingContract,
        },
        "message": _message,
        "primaryType": "SettlePnl",
        "types": {
            "EIP712Domain": [
                {"name": "name", "type": "string"},
                {"name": "version", "type": "string"},
                {"name": "chainId", "type": "uint256"},
                {"name": "verifyingContract", "type": "address"},
            ],
            "SettlePnl": [
                {"name": "brokerId", "type": "string"},
                {"name": "chainId", "type": "uint256"},
                {"name": "settleNonce", "type": "uint64"},
                {"name": "timestamp", "type": "uint64"},
            ],
        },
    }
    print(message)
    _signature = self.get_wallet_signature(message=message)
    payload = {
        "message": _message,
        "signature": _signature,
        "userAddress": userAddress,
        "verifyingContract": verifyingContract,
    }
    return self._sign_request("POST", "/v1/settle_pnl", payload=payload)


def get_pnl_settlement_history(
    self, start_t: int = None, end_t: int = None, page: int = None, size: int = None
):
    """[Private] Get PnL settlement history

    Limit: 20 requests per 1 secsond

    GET /v1/pnl_settlement/history

    Retrieve the historical PnL settlement history of the account.

    Optional Args:
        start_t(timestamp)
        end_t(timestamp)
        page(int)
        size(int)
    https://orderly.network/docs/build-on-evm/evm-api/restful-api/private/get-pnl-settlement-history
    """
    payload = {"start_t": start_t, "end_t": end_t, "page": page, "size": size}
    return self._sign_request("GET", "/v1/pnl_settlement/history", payload=payload)
