def add_header_text_to_card(card, user_exercise):
    """
    Adds header text to a problem card based on
    A/B test buckets for the "intervention" set of experiments.
    """

    # use the same ab_test categories for all experiments
    core_categories = ["learning_dashboard", "exercises"]
    # the unique identifier for this user
    bingo_id = identity.identity()

    # get top level A/B test condition
    primary_condition = experiments.CoreMetrics.ab_test(
            "intervention top level",
            alternative_params={
                "no header": 4,
                "intervention": 1,
                "combination": 1},
            core_categories=core_categories)
    event_log.log_event('x.intervention.primary', primary_condition)

    if primary_condition == "no header":
        # student is in control condition
        card.growthHeader = ""
        intervention_logging(card, user_exercise)
        return

    # choose the frequency with which to insert a header
    # above exercises
    frequency_condition = experiments.CoreMetrics.ab_test(
            "intervention frequency",
            alternative_params={
                "0-10": 25,
                "10-25": 25,
                "25-75": 25,
                "75-100": 25},
            core_categories=core_categories)
    # by having continuous frequencies, we can smoothly plot effectiveness
    # vs. frequency later
    if frequency_condition == "0-10":
        frequency = hash_to_rand(bingo_id, 'freq') * 0.1
    elif frequency_condition == "10-25":
        frequency = hash_to_rand(bingo_id, 'freq') * 0.15 + 0.1
    elif frequency_condition == "25-75":
        frequency = hash_to_rand(bingo_id, 'freq') * 0.50 + 0.25
    elif frequency_condition == "75-100":
        frequency = hash_to_rand(bingo_id, 'freq') * 0.25 + 0.75
    event_log.log_event('x.intervention.frequency', frequency_condition)

    if frequency < random.random():
        # no header for this exercise
        card.growthHeader = ""
        event_log.log_event('x.intervention.frequency.skip', 'True')
        intervention_logging(card, user_exercise)
        return

    # choose what kind of psychological intervention to apply
    alternative_params = {
        "repeat experiment": 1,
        "whatwhyhow": 1,
        "individual explanation": 1}
    if primary_condition == "intervention":
        intervention_type_condition = experiments.CoreMetrics.ab_test(
                "intervention type",
                alternative_params=alternative_params,
                core_categories=core_categories)
    elif primary_condition == "combination":
        # in the combination condition, set the subexperiment at
        # random each time
        intervention_type_condition = random_weighted_choice(
                alternative_params)
    event_log.log_event('x.intervention.type', intervention_type_condition)

    if intervention_type_condition == 'repeat experiment':
        # repeat experiment means conditions which are duplicated from the
        # prior growth mindset experiment.
        # This is a nested ab test.
        # the positive statement had a small negative effect in the last
        # experiment, but it's good to have as a control, so we'll only
        # assign a small fraction of students to it this time
        alternative_params = {
            "growth mindset": 10,
            "growth mindset + link": 10,
            "science statement": 10,
            "positive statement": 1,
            }
        if primary_condition == "intervention":
            intervention_repeat_condition = experiments.CoreMetrics.ab_test(
                    "intervention repeat experiment",
                    alternative_params=alternative_params,
                    core_categories=core_categories)
        elif primary_condition == "combination":
            # in the combination condition, set the subexperiment at
            # random each time
            intervention_repeat_condition = random_weighted_choice(
                    alternative_params)
        event_log.log_event('x.intervention.repeat.type',
                intervention_repeat_condition)

        # set the message text for each condition
        if intervention_repeat_condition == "growth mindset":
            message_text = random.choice(growth_messages)
            card.growthHeader = "<p><em>" + message_text + "</em></p>"
        elif intervention_repeat_condition == "growth mindset + link":
            message_text = random.choice(growth_messages)
            message_text = (i18n._('<p><em>%(message)s</em>'
                                 '&nbsp&nbsp&nbsp<FONT SIZE="-5">'
                                 '<a href=/brainworkout_1 target="_blank">'
                                 'LEARN MORE</a>'
                                 '</FONT></p>', message=message_text))
        elif intervention_repeat_condition == "science statement":
            message_text = random.choice(science_messages)
            message_text = "<p><em>" + message_text + "</em></p>"
        elif intervention_repeat_condition == "positive statement":
            message_text = random.choice(positive_messages)
            message_text = "<p><em>" + message_text + "</em></p>"
    elif intervention_type_condition == 'whatwhyhow':
        # nearly identical to the whatwhyhow condition that was run in the
        # metacognitive experiment
        message_text = whatwhyhow_body
    elif intervention_type_condition == 'individual explanation':
        # more light weight explanation effect hints.
        # choose the target of address for the explanation.
        alternative_params = {
            "self": 1,
            "teacher": 1,
            "sal": 1,
            "friend": 1,
            }
        if primary_condition == "intervention":
            target_condition = experiments.CoreMetrics.ab_test(
                    "intervention repeat experiment",
                    alternative_params=alternative_params,
                    core_categories=core_categories)
        elif primary_condition == "combination":
            # in the combination condition, set the subexperiment at
            # random each time
            target_condition = random_weighted_choice(alternative_params)
        target_text = explanation_prompt_target_text[target_condition]
        question_text = random.choice(explanation_prompt_messages)
        message_text = target_text + question_text
        event_log.log_event('x.intervention.explanation.target',
                target_condition)
        event_log.log_event('x.intervention.explanation.question',
                question_text)

    # choose whether there's a dropdown
    dropdown_condition = experiments.CoreMetrics.ab_test(
            "intervention dropdown",
            alternative_params={
                "none": 1,
                "message": 1,
                "specific": 1
                },
            core_categories=core_categories)
    if intervention_type_condition == "whatwhyhow" and (
            dropdown_condition == 'none'):
        # whatwhyhow is a lot of text, and should always appear in a dropdown
        dropdown_condition = 'specific'
    event_log.log_event('x.intervention.dropdown', dropdown_condition)

    # choose the teaser text for the dropdown
    # TODO add bold+color to all dropdown text?
    if dropdown_condition == "message":
        dropdown_text = i18n._("[Click here for a message.]")
    elif dropdown_condition == "specific":
        if intervention_type_condition == 'repeat experiment':
            if intervention_repeat_condition == "science statement":
                dropdown_text = i18n._("[Click to learn a fun science fact.]")
            else:
                dropdown_text = i18n._(
                    "[Click to read a brief motivational message.]")
        elif intervention_type_condition == 'whatwhyhow':
            dropdown_text = i18n._('[Click to learn about the "<span class=' +
                '"hint_purple" style="font-weight: bold">What? Why? How?' +
                '</span>" strategy.]')
        elif intervention_type_condition == 'individual explanation':
            dropdown_text = i18n._("[Click for a learning strategy question.]")

    # combine the dropdown label and the message text
    if dropdown_condition == 'none':
        # just the message text
        card.growthHeader = message_text
    else:
        card.growthHeader = """
            <p>
                <a href="#" class="show-subhint"
                   data-subhint="intervention-learn-more"
                   data-hidden-text="Hide Information">
                    %s
                </a>
            </p>
            <div class="subhint" id="intervention-learn-more">
                %s
            </div>""" % (dropdown_text, message_text)

    intervention_logging(card, user_exercise)
"""
Code to take a problem card, and select and add motivational text to
it.  For use in the header text intervention A/B test.
"""

import event_log
import experiments
from intl import i18n
import intl.request
import random
from gae_bingo import identity
import hashlib


explanation_prompt_target_text = {
    "self": i18n._("While you are solving the problem, ask yourself: "),
    "teacher": i18n._("What would you tell your teacher if they asked you "
        "while you were solving the problem: "),
    "sal": i18n._("What would you say if Sal asked you while you were solving "
        "the problem: "),
    "friend": i18n._("While you are solving the problem, how would you answer "
        "another student who asked: "),
}

explanation_prompt_messages = [
    i18n._("What are you doing or thinking right now?"),
    i18n._("Why is what you are currently doing helpful?"),
    i18n._("Why is what you are currently doing useful for achieving your "
        "goal?"),
    i18n._("How well is your current approach to this problem working?"),
    i18n._("For each step, what does this step mean to you?"),