/*
 *
 *    Copyright (c) 2020-2021 Project CHIP Authors
 *    All rights reserved.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

#pragma once

#include <app/CASEClientPool.h>
#include <app/OperationalSessionSetup.h>
#include <app/OperationalSessionSetupPool.h>
#include <lib/core/CHIPConfig.h>
#include <lib/core/CHIPCore.h>
#include <lib/support/Pool.h>
#include <platform/CHIPDeviceLayer.h>
#include <transport/SessionDelegate.h>
#include <transport/SessionManager.h>
#include <transport/SessionUpdateDelegate.h>

namespace chip {

struct CASESessionManagerConfig
{
    CASEClientInitParams sessionInitParams;
    CASEClientPoolDelegate * clientPool                    = nullptr;
    OperationalSessionSetupPoolDelegate * sessionSetupPool = nullptr;
};

/**
 * This class provides the following
 * 1. Manage a pool of operational device proxy objects for peer nodes that have active message exchange with the local node.
 * 2. The pool contains atmost one device proxy object for a given peer node.
 * 3. API to lookup an existing proxy object, or allocate a new one by triggering session establishment with the peer node.
 * 4. During session establishment, trigger node ID resolution (if needed), and update the DNS-SD cache (if resolution is
 * successful)
 */
class CASESessionManager : public OperationalSessionReleaseDelegate, public SessionUpdateDelegate
{
public:
    CASESessionManager() = default;
    virtual ~CASESessionManager()
    {
        if (mConfig.sessionInitParams.Validate() == CHIP_NO_ERROR)
        {
            mConfig.sessionInitParams.exchangeMgr->GetReliableMessageMgr()->RegisterSessionUpdateDelegate(nullptr);
        }
    }

    CHIP_ERROR Init(chip::System::Layer * systemLayer, const CASESessionManagerConfig & params);
    void Shutdown();

    /**
     * Find an existing session for the given node ID, or trigger a new session
     * request.
     *
     * The caller can optionally provide `onConnection` and `onFailure` callback
     * objects. If provided, these will be used to inform the caller about
     * successful or failed connection establishment.
     *
     * If the connection is already established, the `onConnection` callback
     * will be immediately called, before FindOrEstablishSession returns.
     *
     * The `onFailure` callback may be called before the FindOrEstablishSession
     * call returns, for error cases that are detected synchronously.
     *
     * attemptCount can be used to automatically retry multiple times if session
     * setup is not successful.
     */
    void FindOrEstablishSession(const ScopedNodeId & peerId, Callback::Callback<OnDeviceConnected> * onConnection,
                                Callback::Callback<OnDeviceConnectionFailure> * onFailure,
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
                                uint8_t attemptCount = 1, Callback::Callback<OnDeviceConnectionRetry> * onRetry = nullptr,
#endif // CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
                                TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kMRPPayload);

    /**
     * Find an existing session for the given node ID or trigger a new session request.
     *
     * The caller can optionally provide `onConnection` and `onSetupFailure`
     * callback objects. If provided, these will be used to inform the caller about successful or
     * failed connection establishment.
     *
     * If the connection is already established, the `onConnection` callback will be immediately called,
     * before `FindOrEstablishSession` returns.
     *
     * The `onSetupFailure` callback may be called before the `FindOrEstablishSession`
     * call returns, for error cases that are detected synchronously.
     *
     * The `attemptCount` parameter can be used to automatically retry multiple times if session setup is
     * not successful.
     *
     * @param peerId The node ID to find or establish a session with.
     * @param onConnection A callback to be called upon successful connection establishment.
     * @param onSetupFailure A callback to be called upon an extended device connection failure.
     * @param attemptCount The number of retry attempts if session setup fails (default is 1).
     * @param onRetry A callback to be called on a retry attempt (enabled by a config flag).
     * @param transportPayloadCapability An indicator of what payload types the session needs to be able to transport.
     */
    void FindOrEstablishSession(const ScopedNodeId & peerId, Callback::Callback<OnDeviceConnected> * onConnection,
                                Callback::Callback<OperationalSessionSetup::OnSetupFailure> * onSetupFailure,
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
                                uint8_t attemptCount = 1, Callback::Callback<OnDeviceConnectionRetry> * onRetry = nullptr,
#endif // CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
                                TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kMRPPayload);

    /**
     * Find an existing session for the given node ID or trigger a new session request.
     *
     * The caller can optionally provide `onConnection`
     * callback objects. If provided, these will be used to inform the caller about successful connection establishment.
     *
     * If the connection is already established, the `onConnection` callback will be immediately called,
     * before `FindOrEstablishSession` returns.
     *
     * The `attemptCount` parameter can be used to automatically retry multiple times if session setup is
     * not successful.
     *
     * This function allows passing 'nullptr' for the error handler to compile, which is useful in scenarios where error
     * handling is not needed.
     *
     * @param peerId The node ID to find or establish a session with.
     * @param onConnection A callback to be called upon successful connection establishment.
     * @param attemptCount The number of retry attempts if session setup fails (default is 1).
     * @param onRetry A callback to be called on a retry attempt (enabled by a config flag).
     * @param transportPayloadCapability An indicator of what payload types the session needs to be able to transport.
     */
    void FindOrEstablishSession(const ScopedNodeId & peerId, Callback::Callback<OnDeviceConnected> * onConnection, std::nullptr_t,
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
                                uint8_t attemptCount = 1, Callback::Callback<OnDeviceConnectionRetry> * onRetry = nullptr,
#endif // CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
                                TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kMRPPayload);

    void ReleaseSessionsForFabric(FabricIndex fabricIndex);

    void ReleaseAllSessions();

    /**
     * This API returns the address for the given node ID.
     * If the CASESessionManager is configured with a DNS-SD cache, the cache is looked up
     * for the node ID.
     * If the DNS-SD cache is not available, the CASESessionManager looks up the list for
     * an ongoing session with the peer node. If the session doesn't exist, the API will return
     * `CHIP_ERROR_NOT_CONNECTED` error.
     */
    CHIP_ERROR GetPeerAddress(const ScopedNodeId & peerId, Transport::PeerAddress & addr,
                              TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kMRPPayload);

    //////////// OperationalSessionReleaseDelegate Implementation ///////////////
    void ReleaseSession(OperationalSessionSetup * device) override;

    //////////// SessionUpdateDelegate Implementation ///////////////
    void UpdatePeerAddress(ScopedNodeId peerId) override;

private:
    OperationalSessionSetup * FindExistingSessionSetup(const ScopedNodeId & peerId, bool forAddressUpdate = false) const;

    Optional<SessionHandle> FindExistingSession(
        const ScopedNodeId & peerId,
        const TransportPayloadCapability transportPayloadCapability = TransportPayloadCapability::kMRPPayload) const;

    void FindOrEstablishSessionHelper(const ScopedNodeId & peerId, Callback::Callback<OnDeviceConnected> * onConnection,
                                      Callback::Callback<OnDeviceConnectionFailure> * onFailure,
                                      Callback::Callback<OperationalSessionSetup::OnSetupFailure> * onSetupFailure,
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
                                      uint8_t attemptCount, Callback::Callback<OnDeviceConnectionRetry> * onRetry,
#endif
                                      TransportPayloadCapability transportPayloadCapability);

    CASESessionManagerConfig mConfig;
};

} // namespace chip
