"""Story screens composed from the existing modern generator's UI primitives."""


def build(g, indent):
    info = lambda name: g.info(name, namespace='WCS.Story')
    state = lambda name, value: g.is_prop(name, value, namespace='WCS.Story')
    i = indent + '    '

    def lives(y=112):
        markers = []
        for index in range(3):
            present = 'Integer.IsGreater({},{})'.format(g.prop('Lives', 'WCS.Story'), index)
            markers += [g.image(g.CIRCLE_TEX, index * 24, 10, 16, 16, i, color=g.GOLD, visible=present),
                        g.image(g.CIRCLE_STROKE_TEX, index * 24, 10, 16, 16, i, color=g.DIM, visible='!' + present)]
        return g.group(markers + [g.label(g.localized(35406), 84, 0, 120, 36, i, font='font25_title', color=g.SUB),
                        g.label(info('Lives') + ' / 3', 210, 0, 90, 36, i, font='font25_title', color=g.GOLD,
                                align='right')], g.PAD + 700, y, 300, 36, indent)

    def chapters():
        j = i + '    '
        rows = [g.panel(0, 0, 304, 744, j, tone='solid'),
                g.label(g.localized(35427), 28, 22, 248, 32, j, font='font20_title', color=g.DIM),
                g.label(info('Progress'), 28, 58, 248, 56, j, font='font40_title', color=g.GOLD)]
        for number in range(1, 6):
            y = 142 + (number - 1) * 112
            current = state('Chapter.{}.State'.format(number), 'current')
            done = state('Chapter.{}.State'.format(number), 'done')
            rows += [g.image(g.BTN_TEX, 14, y, 276, 98, j, color='22E8B860', border=g.R_BTN, visible=current),
                     g.image(g.BTN_STROKE_TEX, 14, y, 276, 98, j, color=g.GOLD, border=g.R_BTN, visible=current),
                     g.label(str(number), 28, y + 8, 36, 40, j, font='font25_title', color=g.DIM, align='center'),
                     g.label(g.localized(35409 + number), 76, y + 8, 204, 40, j, font='font25_title',
                             color=g.TXT),
                     g.label(info('Chapter.{}.Progress'.format(number)), 76, y + 48, 190, 36, j,
                             font='font25', color=g.SUB),
                     g.image(g.CIRCLE_TEX, 32, y + 58, 18, 18, j, color=g.GREEN, visible=done)]
        return g.group(rows, g.CARD_W - g.PAD - 304, 112, 304, 744, i, animations=g.stagger(200))

    rules = []
    for number, heading, body, value in (
            ('01', 35430, 35431, info('QuestionCount')),
            ('02', 35432, 35433, '3'),
            ('03', 35434, 35435, 'A B C D')):
        rules.append(g.rule_card(number, g.localized(heading), g.localized(body),
                                 g.PAD + (int(number) - 1) * 360, i,
                                 [g.label(value, 28, 108, 284, 80, i + '    ', font='font_MainMenu', color=g.GOLD)]))
    rules += [g.headline(g.localized(35442), 108, i),
              g.button(9781, g.localized(35437), g.PAD, g.START_ROW_Y, g.START_PRIMARY_W, 84, i,
                       (None, None, None, 9782), primary=True),
              g.button(9782, g.localized(35423), g.PAD + g.START_PRIMARY_W + g.START_GAP, g.START_ROW_Y,
                       g.START_BACK_W, 84, i, (None, None, 9781, None))]
    result = [g.group(rules, 0, 0, g.CARD_W, g.CARD_H, indent, visible=g.on_screen('rules'),
                      animations=g.screen_animations())]

    result.append(g.group([
        g.headline(g.localized(35408), 200, i), g.panel(g.PAD, 336, 1000, 160, i),
        g.dots(g.PAD + 40, 402, i),
        g.label(g.localized(35408), g.PAD + 178, 352, 770, 128, i, font='font32', color=g.TXT),
        g.button(9783, g.localized(35423), g.PAD, 560, 400, 84, i, (None, None, None, None))],
        0, 0, g.CARD_W, g.CARD_H, indent, visible=g.on_screen('loading'), animations=g.screen_animations()))

    question = [g.label(info('SceneLabel'), g.PAD, 112, 650, 36, i, font='font25_title', color=g.TXT), lives(),
                g.moderator_card(g.PAD, 164, 1000, 204, i, info('Narration'), ids=(9850, 9851),
                                 links=(9851, 9851, 9790, 9791),
                                 caption=(g.localized(35405), 18, 34, 'font25_title', g.GOLD), font='font27',
                                 text_top=58, text_bottom=22),
                g.textbox(info('Question'), g.PAD, 392, 1000, 172, i, font='font40_title', color=g.TXT,
                          autoscroll=True),
                chapters()]
    for index, letter in enumerate('ABCD'):
        links = ((9851, 9792, None, 9791), (9851, 9793, 9790, None),
                 (9790, None, None, 9793), (9791, None, 9792, None))[index]
        question.append(g.answer_tile(9790 + index, letter, g.PAD + (506 if index % 2 else 0),
                                      g.ANSWER_Y1 if index < 2 else g.ANSWER_Y2, links, i,
                                      namespace='WCS.Story'))
    result.append(g.group(question, 0, 0, g.CARD_W, g.CARD_H, indent, visible=g.on_screen('question'),
                          animations=g.screen_animations()))

    correct = state('Outcome', 'correct')
    wrong = state('Outcome', 'wrong')
    outcomes = [g.label(info('SceneLabel'), g.PAD, 112, 650, 36, i, font='font25_title', color=g.TXT), lives(),
                g.headline(info('Headline'), 154, i), chapters(),
                g.group([g.answer_card('right', info('RightLetter'), info('RightText'), g.localized(35441),
                                       g.PAD, 248, 1000, 160, i)], 0, 0, g.CARD_W, g.CARD_H, i, visible=correct),
                g.group([g.answer_card('right', info('RightLetter'), info('RightText'), g.localized(35441),
                                       g.PAD, 248, 494, 160, i, font='font27'),
                         g.answer_card('wrong', info('YourLetter'), info('YourText'), g.localized(35440),
                                       g.PAD + 506, 248, 494, 160, i, font='font27')],
                        0, 0, g.CARD_W, g.CARD_H, i, visible=wrong),
                g.moderator_card(g.PAD, 430, 1000, 250, i, info('Aftermath'), ids=(9852, 9853),
                                 links=(9853, 9853, 9794, 9794),
                                 caption=(g.localized(35405), 18, 34, 'font25_title', g.GOLD), font='font27',
                                 text_top=58, text_bottom=22),
                g.label(g.localized(35426) + ': ' + info('Score') + '     •     ' +
                        g.localized(35427) + ': ' + info('Progress'), g.PAD, 696, 1000, 44, i,
                        font='font27', color=g.SUB),
                g.button(9794, info('PrimaryLabel'), g.PAD, g.START_ROW_Y, 440, 84, i,
                         (9853, None, None, ((g.on_screen('feedback'), None),
                                            (g.on_screen('win', 'gameover'), 9796))), primary=True),
                g.group([g.button(9796, g.localized(35438), g.PAD + 460, g.START_ROW_Y, 320, 84, i,
                                   (9853, None, 9794, 9797)),
                         g.button(9797, g.localized(35439), g.PAD + 800, g.START_ROW_Y, 200, 84, i,
                                   (9853, None, 9796, None))],
                        0, 0, g.CARD_W, g.CARD_H, i, visible=g.on_screen('win', 'gameover'))]
    result.append(g.group(outcomes, 0, 0, g.CARD_W, g.CARD_H, indent,
                          visible=g.on_screen('feedback', 'win', 'gameover'), animations=g.screen_animations()))
    result.append(g.group([
        g.headline(g.localized(35401), 108, i),
        g.group([g.panel(0, 0, 1000, 280, i + '    ', tone='red'),
                 g.textbox(info('Error'), 32, 26, 936, 228, i + '    ', font='font32', color=g.TXT,
                           autoscroll=True)], g.PAD, 240, 1000, 280, i),
        g.button(9784, g.localized(35422), g.PAD, 560, 480, 84, i, (None, None, None, 9785), primary=True),
        g.button(9785, g.localized(35423), g.PAD + 500, 560, 360, 84, i, (None, None, 9784, None))],
        0, 0, g.CARD_W, g.CARD_H, indent, visible=g.on_screen('error'), animations=g.screen_animations()))
    return result
