<%
    # Note that this file is not a valid *.html file!
    # It is intended to be a bottlepy - style template
    # used for the scripting part of TheOnionBox!

    VERSION_STATUS_COLORS = {"new": "blue", "new in series": "blue", "obsolete": "red", "recommended": "green",
                             "old": "red",  "unrecommended": "red",  "unknown": "cyan"}

    tor = get('tor')

    # This is Copy/Paste from ARM!

    # see dir-spec.txt for description of FLAGS
    FLAG_LABELS = {"Authority": "default",  "BadExit": "danger",     "BadDirectory": "danger",    "Exit": "warning",
                   "Fast": "primary",      "Guard": "primary",     "HSDir": "info",       "Named": "primary",
                   "Stable": "primary",      "Running": "success",  "Unnamed": "warning",     "Valid": "success",
                   "V2Dir": "default",       "V3Dir": "default"}

    FLAG_COLORS = {"Authority": "black", "BadExit": "red", "BadDirectory": "red", "Exit": "orange",
                   "Fast": "blue",      "Guard": "blue",     "HSDir": "deepskyblue",       "Named": "blue",
                   "Stable": "blue",      "Running": "green",  "Unnamed": "red",     "Valid": "green",
                   "V2Dir": "black",       "V3Dir": "black", "Tor Communication Error": "red", 'none': 'cyan',
                    "unknown": "black"}

    def flags(is_bridge = False):
        flags = ['unknown']
        try:
            if is_bridge is True:
                oo_details = get('oo_details', None)
                if oo_details is not None:
                    # get_flags() only works for relays, not for bridges
                    flags = oo_details('flags') or ['unknown']
                end
            else:
                flags = tor.flags or ['unknown']
            end
        except Exception as exc:
            pass
        end

        fs = ''
        for flag in flags:
            if fs is not '':
                fs += ' | '
            end
            fs += '<span style="color: '+ FLAG_COLORS[flag] + ';">' + flag + '</span>'
        end

        if (len(flags) == 1) and (flags[0] == 'none'):
            a_o = get('accounting_on')
            if a_o == True:
                a_s = get('accounting_stats')
                if a_s.status != 'awake':
                    fs += ' | could be due to '
                    fs += '<a href="#accounting">Hibernation Status</a>'
                    fs += ' <span style="color: green;">' + a_s.status + '</span>'
                end
            end
        end
        return fs
    end

%>

<%
    bv = get('boxVersion')
    tvt = bv.Tor

    tvs = tor.version_short
%>


{{!header_row('Tor', 'Local Status', 'stats')}}
{{!standard_row('Version', tor.get_version())}}

<div class = "row">
    <div class="{{box_datum_grid}}">
        Version Flag
    </div>
    <div class="{{box_value_grid}} box_value_margin">
        % tvc = tor.version_current
        % if tvc in VERSION_STATUS_COLORS:
            <span style="color: {{VERSION_STATUS_COLORS[tvc]}};">{{tvc}}</span>
        % else:
            <span>{{tvc}}</span>
        % end
        % if tvt.is_latest_stable(tvs) is True:
            <span> | latest stable</span>
        % end
    </div>
    <div class="{{box_right_grid}}"></div>
</div>


% if tvt.is_latest_stable(tvs) is False:

        <div class = "row">
            <div class="{{box_datum_grid}}">
                Latest Stable Version
            </div>
            <div class="{{box_value_grid}} box_value_margin">
                <a href="https://dist.torproject.org/" target="_blank">{{tvt.stable}}</a>
            </div>
            <div class="{{box_right_grid}}"></div>
        </div>
<%
    end
    if tvt.is_latest_unstable(tvs) is False:
%>
        <div class = "row">
            <div class="{{box_datum_grid}}">
                Latest Unstable Version
            </div>
            <div class="{{box_value_grid}} box_value_margin">
                <a href="https://dist.torproject.org/" target="_blank">{{tvt.unstable}}</a>
            </div>
            <div class="{{box_right_grid}}"></div>
        </div>
%   end

{{!standard_row('PID', tor.get_pid())}}
{{!standard_row('User', tor.user)}}

<%
    node_mode = 'Client'
    if tor.get_conf('BridgeRelay', None) == '1':
        node_mode = 'Bridge'
    elif tor.get_conf('ORPort', None):
        node_mode = 'Relay'
    end
%>
{{!standard_row('Mode', node_mode)}}

<%
    _fngrprnt = None
    try:
        _fngrprnt = tor.fingerprint
    except:
        pass
    end
%>
% if node_mode != 'Client' and _fngrprnt is not None:
    {{!standard_row('Fingerprint', _fngrprnt)}}

    <div class="row">
        <div class="{{box_datum_grid}}">
            Flags
        </div>
        <div class="{{box_value_grid}} box_value_margin">
            {{!flags(node_mode == 'Bridge')}}
        </div>
        <div class="{{box_right_grid}}"></div>
    </div>
% end

<hr>