from browser import window

from reeborg_common import *

__id = "en"

_usage_ = '''
<h2 id="%s">Basic commands</h2>
<p>Note: In Javascript, <code>true</code> and <code>false</code> are all written in lower cases.
In Python, <code>True</code> and <code>False</code> start with an upper case letter.
To avoid having to
specify both possibilities each time, "true" and "false" will be used instead below.</p>
<p> In addition to understanding Javascript or Python, Reeborg, and all the other robots,
know the following instructions:</p>
<ul>
    <li><code>at_goal()</code>: condition which is "true" if Reeborg has reached "home"</li>
    <li><code>build_wall()</code>: adds a wall right in front of where Reeborg is</li>
    <li><code>done()</code>: instructs the program to stop (end) at that point.</li>
    <li><code>front_is_clear()</code>: "true" if no wall blocking Reeborg's way</li>
    <li><code>has_token()</code>: "true" if Reeborg carries at least one token</li>
    <li><code>is_facing_north()</code>: "true" if Reeborg is facing North
        (towards the top of the screen)</li>
    <li><code>move()</code></li>
    <li><code>put()</code> or <code>put("object")</code> where object is one of
      <code>"token"</code>,
      <code>"triangle"</code>, <code>"square"</code> or <code>"star"</code></li>
    <li><code>right_is_clear()</code>: "true" if no wall is to the immediate right of Reeborg</li>
    <li><code>repeat(f, n)</code>: executes ``f()`` n times</li>
    <li><code>object_here()</code>: "false" if there is no object at Reeborg's location, otherwise
    it returns the name of the object, <code>"triangle"</code>, <code>"square"</code> or
    <code>"star"</code>, or code>"token"</code>. </li>
    <li><code>take()</code> or <code>take("object")</code> where object is one of
        <code>"token"</code>, <code>"triangle"</code>, <code>"square"</code> or
        <code>"star"</code></li>
    <li><code>token_here()</code>: "true" if there is a token at Reeborg's location</li>
    <li><code>turn_left()</code></li>
</ul>
<p>To create additional robots, one can use <code>UsedRobot()</code> preceeded by
<code>new</code> if using Javascript.</p>
<p>In addition, the following commands are available and are not tied to
any robot in particular.</p>
<ul>
    <li><code>from my_lib import *</code>: for <b>Python</b>, includes the code found in the
         Library tab</li>
    <li><code>inspect(object)</code>: shows result in Reeborg's Diary</li>
    <li><code>pause()</code>; can be called with an argument in milliseconds which indicates the
    time after which it will automatically resume.</li>
    <li><code>sound("true")</code>: turns sound on. Can be turned off by calling with no argument
        or with "false". Off by default or if delay between actions is less than 250ms.</li>
    <li><code>think(number)</code>: sets the amount of time in milliseconds between each robot
         action.</li>
    <li><code>write(string)</code>: write a string in Reeborg's Diary <b>only</b> if world is
         visible - otherwise, use <code>print</code>.</li>
    <li><code>select_world(string)</code>: selects a given world.</li>
        <li><code>select_challenge(string)</code>: selects a given (hidden) world.</li>
    <li><code>print(string)</code>: only valid with Python.  Somewhat equivalent to
         <code>write</code>.</li>
</ul>
''' % ("basic-commands-" + __id)

RUR = window.RUR
RUR.ui.add_help(_usage_, __id, "English",
            "Instead of my_lib, token, star, triangle, and square, use: ")

# RUR._x_ defined in commands.js
at_goal = RUR._at_goal_
build_wall = RUR._build_wall_
front_is_clear = RUR._front_is_clear_
has_token = RUR._has_token_
is_facing_north = RUR._is_facing_north_
move = RUR._move_
put = RUR._put_
token_here = RUR._token_here_
right_is_clear = RUR._right_is_clear_
object_here = RUR._object_here_
take = RUR._take_
turn_left = RUR._turn_left_
repeat = RUR._repeat_
set_max_steps = RUR._set_max_steps_
# defined in rur_utils.js
inspect = RUR.inspect
view_source = RUR.view_source
# defined in control.js
write = RUR.control.write
done = RUR.control.done
sound = RUR.control.sound
think = RUR.control.think
pause = RUR.control.pause
# defined in ui.js
select_world = RUR.ui.select_world
select_challenge = RUR.ui.select_challenge

# The following is the only language specific function which can only be used in monde.html,
# not when imported from world.html
try:
    verify = RUR.verify
except AttributeError:
    pass


class UsedRobot(object):
    def __init__(self, x=1, y=1, orientation='e', tokens=0):
        robot = RUR.robot.create_robot(x, y, orientation, tokens)
        self.body = robot
        RUR.world.add_robot(self.body)

    def move(self):
        RUR.control.move(self.body)

    def at_goal(self):
        return RUR.control.at_goal(self.body)

    def build_wall(self):
        RUR.control.build_wall(self.body)

    def front_is_clear(self, no_frame=False):
        return RUR.control.front_is_clear(self.body, no_frame=no_frame)

    def has_token(self):
        return RUR.control.has_token(self.body)

    def is_facing_north(self):
        return RUR.control.is_facing_north(self.body)

    def put(self, arg="token"):
        RUR.control.put(self.body, arg)

    def token_here(self):
        return RUR.control.token_here(self.body)

    def right_is_clear(self):
        return RUR.control.right_is_clear(self.body)

    def object_here(self):
        return RUR.control.object_here(self.body)

    def take(self, arg="token"):
        RUR.control.take(self.body, arg)

    def turn_left(self, no_frame=False):
        RUR.control.turn_left(self.body, no_frame=no_frame)
