#!/usr/bin/env python
# -*- encoding: UTF-8 -*-

import qi
import argparse
import sys

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--ip", type=str, default="127.0.0.1",
                        help="Robot IP address")
    parser.add_argument("--port", type=int, default=9559,
                        help="Naoqi port number")

    args = parser.parse_args()

    session = qi.Session()
    try:
        session.connect(f"tcp://{args.ip}:{args.port}")
        print(f"Connected to NAO at {args.ip}")
    except RuntimeError:
        print(f"❌ Could not connect to Naoqi at {args.ip}:{args.port}")
        sys.exit(1)

    # Get the ALAutonomousLife service
    try:
        autonomous_life = session.service("ALAutonomousLife")
        
        current_state = autonomous_life.getState()
        print(f"Current Autonomous Life state: {current_state}")
        
        if current_state != "disabled":
            print("Disabling Autonomous Life...")
            autonomous_life.setState("disabled")
            print("✅ Autonomous Life is now DISABLED")
        else:
            print("✅ Autonomous Life was already disabled")
            
    except Exception as e:
        print(f"⚠️ Error: {e}")
        print("Note: Make sure the robot is awake and you have the right permissions.")

    print("\nYou can now run your teleoperation program safely.")

if __name__ == "__main__":
    main()
