#!/usr/bin/env python

import rospy

from lg_common import ManagedBrowser, ManagedWindow
from lg_common.msg import ApplicationState, WindowGeometry
from lg_common.helpers import dependency_available
from lg_common.helpers import discover_host_from_url, discover_port_from_url

from std_msgs.msg import String


if __name__ == '__main__':
    rospy.init_node('static_browser')

    geometry = ManagedWindow.get_viewport_geometry()
    url = rospy.get_param('~url', None)
    command_line_args = rospy.get_param('~command_line_args', '')
    scale_factor = rospy.get_param('~force_device_scale_factor', 1)
    debug_port = rospy.get_param('~debug_port', 10000)
    user_agent = rospy.get_param(
        '~user_agent', 'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; '
        'en-us AppleWebKit/531.21.10 (KHTML, like Gecko) ' +
        'Version/4.0.4 Mobile/7B314 Safari/531.21.10'
    )
    state = rospy.get_param('~state', ApplicationState.VISIBLE)

    global_dependency_timeout = rospy.get_param("/global_dependency_timeout", 15)
    depend_on_url = rospy.get_param("~depend_on_url", False)

    www_host = discover_port_from_url(url)
    www_port = discover_port_from_url(port)

    if depend_on_url:
        if not dependency_available(www_host, www_port, 'static browser URL', global_dependency_timeout):
            msg = "Service: %s hasn't become accessible within %s seconds" % ('director', global_dependency_timeout)
            rospy.logfatal(msg)
            raise DependencyException(msg)
        else:
            rospy.loginfo("URL available - continuing initialization")

    browser = ManagedBrowser(
        geometry=geometry,
        url=url,
        command_line_args=command_line_args,
        force_device_scale_factor=scale_factor,
        debug_port=debug_port,
        user_agent=user_agent
    )

    browser.set_state(state)

    rospy.Subscriber('{}/state'.format(rospy.get_name()), ApplicationState,
                     browser.handle_state_msg)

    def handle_debug_sock_msg(msg):
        browser.send_debug_sock_msg(msg.data)

    rospy.Subscriber('{}/debug'.format(rospy.get_name()), String, handle_debug_sock_msg)

    rospy.spin()

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

@@ -191,16 +191,37 @@
 vailable
+, DependencyException
 %0Afrom lg
@@ -1133,35 +1133,35 @@
 host = discover_
-por
+hos
 t_from_url(url)%0A
@@ -1198,20 +1198,19 @@
 rom_url(
-port
+url
 )%0A%0A    i

fix issues with undefined variables