/*
  controller.cpp

  Copyright (c) 2021 Kevin Matz (kevin.matz@gmail.com)

  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.
*/

#include "controller.h"

namespace RDM {

/**
 * @brief Controller::Controller
 */
Controller::Controller()
  : next_transaction_(0)
{


}


/**
 * @brief Controller::~Controller
 */
Controller::~Controller()
{

}


/**
 * @brief Controller::findDevices
 * @param lowerBound
 * @param upperBound
 * @return
 */
bool Controller::findDevices(uint64_t lowerBound, uint64_t upperBound) const
{
  bool device_found = false;
  bool response_received = false;

  /// we have called to the lowest branch and are testing
  /// for a single device.
  if (lowerBound == upperBound) {
    for (int i = 0; i < 10; i++) {
        // response_received = sendDiscMute(lowerBound);
        if (response_received)
          break;
      }
    if ( response_received ) {
      // Add device found at ‘LowerBound’ to list.
    }
    return !(response_received);
  }

  /// look for a lower branch with active devices.
  for (int i = 0; i < 3; i++) {
    // response_received = sendDiscUniqueBranch(lowerBound, upperBound);
    if (response_received)
      break;
  }

  if ( response_received ) {
    device_found = true;

    // check for single good response.
    // !!! remove during FULL DEBUGGING VALIDATION begin
//    if ( response checksum passes )// possibly only one device in branch.
//    {
//      // shortcut to avoid branching all the way down.
//      deviceFound = quick_find(response uid, lowerBound, upperBound);
//    }
    // !!! remove during FULL DEBUGGING VALIDATION end

    /// choose next branches to test
    if (device_found == true ) { // Quick_Find indicates there are multiple devices in branch.
      uint64_t midPosition;
      midPosition = ((lowerBound & (0x0000800000000000-1)) +
                     (upperBound & (0x0000800000000000-1))) / 2
          + ((upperBound & 0x0000800000000000) ? 0x0000400000000000 : 0)
          + ((lowerBound & 0x0000800000000000) ? 0x0000400000000000 :0);
      //go into next branch fork.
      device_found = findDevices(midPosition + 1, upperBound);
      device_found |= findDevices (lowerBound, midPosition);
    }
  }
  return device_found;
}


/**
 * @brief Controller::quick_find
 * @param uid
 * @param lowerBound
 * @param upperBound
 * @return
 */
bool Controller::quick_find([[maybe_unused]] const UID& uid,
                            [[maybe_unused]] uint64_t lowerBound,
                            [[maybe_unused]] uint64_t upperBound) const
{
  /// The call to this sub-function should be removed during full discovery
  /// testing as it can mask other problems in the discovery implementation.

  /// This sub-function can speed up discovery when there is only one device in
  /// the current branch by eliminating the need to fully branch to the lowest
  /// level of the discovery tree.

  bool response_received = false;

  for (int i = 0; i < 10; i++) {
    // response_received = sendDiscMute(uid.uid);
    if (response_received)
      break;
    }

  if ( response_received ) {
    // Add device found at ‘LowerBound’ to list.
  }

  /// verify there is no other devices in this branch.
  for (int i = 0; i < 3; i++) {
    // response_received = sendDiscUniqueBranch(lowerBound, upperBound);
    if (response_received)
      break;
    }

//  if (response_received && checksum passed)
//    /// there is another single device response at this branch.
//    {
//      // return quick_find(response uid, lowerBound, upperBound);
//    }
//  else if (response_received && checksum failed)
//    /// there are possibly multiple devices... branch further
//    {
//      return true;
//    }
  return false;
}


/**
 * @brief Controller::rxDiscoveryResponse
 * @param message
 */
void Controller::rxDiscoveryResponse([[maybe_unused]] const MsgPtr message)
{

}


/**
 * @brief Controller::rxGetResponse
 * @param message
 */
void Controller::rxGetResponse([[maybe_unused]] const MsgPtr message)
{

}


/**
 * @brief Controller::rxSetResponse
 * @param message
 */
void Controller::rxSetResponse([[maybe_unused]] const MsgPtr message)
{

}


} // namespace RDM
