#!/usr/bin/env python3
# Software License Agreement (BSD)
#
# @author    Luis Camero <lcamero@clearpathrobotics.com>
# @copyright (c) 2025, Clearpath Robotics, Inc., All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright notice,
#   this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
# * Neither the name of Clearpath Robotics nor the names of its contributors
#   may be used to endorse or promote products derived from this software
#   without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

import argparse
import rclpy
import time

from lifecycle_msgs.msg import Transition
from lifecycle_msgs.srv import ChangeState


def main(args=None):
    parser = argparse.ArgumentParser()
    parser.add_argument('--namespace', type=str, default='')
    parser.add_argument('--node', type=str, default='node_name')
    parser.add_argument('--auto_configure', type=bool, default=True)
    parser.add_argument('--auto_activate', type=bool, default=True)
    parser.add_argument('--timeout', type=float, default=5.0)
    parser.add_argument('--transition_attempts', type=int, default=3)
    arg, _ = parser.parse_known_args(args=args)

    print(arg)

    rclpy.init()
    node = rclpy.create_node(f'activate_{arg.node}')

    if arg.namespace == '/':
        cli = node.create_client(
            ChangeState,
            f'{arg.namespace}'
            f'{arg.node}/change_state')
    else:
        cli = node.create_client(
            ChangeState,
            f'/{arg.namespace}/'
            f'{arg.node}/change_state')

    available = False
    for i in range(arg.transition_attempts):
        if cli.wait_for_service(timeout_sec=arg.timeout):
            available = True
            break
        node.get_logger().error(
            f'Lifecycle service not available. Trying again in {arg.timeout}...')
    if not available:
        node.get_logger().error(
            f'Lifecycle service not available after {arg.transition_attempts}')
        return

    req = ChangeState.Request()
    configured = False
    activated = False

    if arg.auto_configure:
        req.transition.id = Transition.TRANSITION_CONFIGURE
        for i in range(arg.transition_attempts):
            node.get_logger().info(
                f'Attempt {i+1}, requesting lifecycle node transition to configured')
            future = cli.call_async(req)
            rclpy.spin_until_future_complete(node, future, timeout_sec=arg.timeout)
            if future.result() and future.result().success:
                node.get_logger().info('Lifecycle node configured successfully.')
                configured = True
                break
            else:
                node.get_logger().warn(f'Configuration attempt {i+1} failed. Retrying...')
                time.sleep(arg.timeout)

    if not configured:
        node.get_logger().error(
            f'Failed to configure the lifecycle node after {arg.transition_attempts} attempts')

    if configured and arg.auto_activate:
        req.transition.id = Transition.TRANSITION_ACTIVATE
        for i in range(arg.transition_attempts):
            node.get_logger().info(
                f'Attempt {i+1}, requesting lifecycle node transition to activated')
            future = cli.call_async(req)
            rclpy.spin_until_future_complete(node, future, timeout_sec=arg.timeout)
            if future.result() and future.result().success:
                node.get_logger().info('Lifecycle node activated successfully.')
                activated = True
                break
            else:
                node.get_logger().warn(f'Activation attempt {i+1} failed. Retrying...')
                time.sleep(arg.timeout)

    if configured and not activated:
        node.get_logger().error(
            f'Failed to activate the lifecycle node after {arg.transition_attempts} attempts')

    node.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()

