# -*- coding: utf-8 -*-
"""图 7-13　Jira 软件界面 —— 教材第 172 页

逐项还原「图内要素」：
· 外层浏览器窗口；顶部 Atlassian 官网导航（A 标志 + ATLASSIAN、Products/Teams/
  Why Atlassian?/Resources/Enterprise、右侧搜索、九宫格、SY 头像 + 被右缘截断的姓名）；
· 主视觉：两行大标题「The new Jira:／from teams to dreams」（teams 加粗）、
  黑色胶囊按钮 Get started、一排 6 个产品类别（Software、Product management、
  Marketing、Project management、Design、IT），Project management 被矩形框圈出；
· 底部嵌入 Jira 应用界面（下缘被图框裁切）：顶栏（Jira、Your work…Apps、Create、
  Search or ask anything、铃铛/问号/头像）、左侧窄栏（Overviews、Website、Email、
  All projects）、主区项目名 The Next Big Thing 与 13 个标签页（Board 选中）。
"""
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from matplotlib.patches import Polygon  # noqa: E402
from _draw import setup, canvas, save  # noqa: E402
from _jira import (rect, T, line, ellipse, caret, grid_icon, jira_logo,  # noqa: E402
                   magnifier, bell, INK, NAV, ACC, FILL, LINE, GREY, GRID, SOFT,
                   FRAME, DARK, BLACK)

OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'assets',
                   '图7-13-Jira软件界面.png')


def product_icon(ax, kind, cx, cy, s=1.0, c=DARK):
    """6 个产品类别的小图标（简化线稿）。"""
    if kind == 'software':
        rect(ax, cx - 2.2 * s, cy + 1.7 * s, 4.4 * s, 2.9 * s, fc='none', ec=c, lw=1.1, r=0.2, z=6)
        line(ax, cx - 2.2 * s, cy + 0.1 * s, cx + 2.2 * s, cy + 0.1 * s, color=c, lw=1.0, z=6)
        line(ax, cx - 0.9 * s, cy - 1.2 * s, cx + 0.9 * s, cy - 1.2 * s, color=c, lw=1.1, z=6)
        line(ax, cx, cy + 0.1 * s, cx, cy - 1.2 * s, color=c, lw=1.0, z=6)
    elif kind == 'pm':
        ellipse(ax, cx, cy, 1.55 * s, fc='none', ec=c, lw=1.1, z=6)
        for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0),
                       (0.72, 0.72), (-0.72, -0.72), (0.72, -0.72), (-0.72, 0.72)]:
            line(ax, cx + dx * 1.55 * s * 0.9, cy + dy * 1.55 * s * 0.9,
                 cx + dx * 2.05 * s, cy + dy * 2.05 * s, color=c, lw=1.0, z=6)
    elif kind == 'mkt':
        ax.add_patch(Polygon([[cx - 1.9 * s, cy + 1.3 * s], [cx + 0.3 * s, cy + 1.3 * s],
                              [cx + 1.9 * s, cy + 2.2 * s], [cx + 1.9 * s, cy - 2.2 * s],
                              [cx + 0.3 * s, cy - 1.3 * s], [cx - 1.9 * s, cy - 1.3 * s]],
                             closed=True, fc='none', ec=c, lw=1.1, zorder=6))
        line(ax, cx - 1.9 * s, cy - 1.3 * s, cx - 1.9 * s, cy + 1.3 * s, color=c, lw=1.1, z=6)
    elif kind == 'proj':
        for k, hgt in enumerate([3.4, 2.4, 1.6]):
            rect(ax, cx - 2.4 * s + k * 1.7 * s, cy + 1.9 * s, 1.3 * s, hgt * s,
                 fc=c, ec=c, lw=0.3, r=0.15, z=6)
    elif kind == 'design':
        ax.add_patch(Polygon([[cx - 2.0 * s, cy - 1.7 * s], [cx + 0.6 * s, cy + 2.1 * s],
                              [cx + 1.9 * s, cy + 0.7 * s], [cx - 0.6 * s, cy - 1.9 * s]],
                             closed=True, fc='none', ec=c, lw=1.0, zorder=6))
        line(ax, cx - 1.6 * s, cy - 1.5 * s, cx + 1.5 * s, cy + 0.5 * s, color=c, lw=0.8, z=6)
    else:  # IT / cloud
        ellipse(ax, cx - 1.0 * s, cy - 0.3 * s, 1.3 * s, fc='none', ec=c, lw=1.0, z=6)
        ellipse(ax, cx + 1.1 * s, cy - 0.2 * s, 1.2 * s, fc='none', ec=c, lw=1.0, z=6)
        ellipse(ax, cx + 0.1 * s, cy + 0.9 * s, 1.3 * s, fc='none', ec=c, lw=1.0, z=6)
        rect(ax, cx - 2.0 * s, cy + 0.1 * s, 3.6 * s, 1.1 * s, fc='white', ec='none', lw=0, z=5)


def atlassian_nav(ax, top=97.3, bot=92.3):
    rect(ax, 0.8, top, 98.4, top - bot, fc='white', ec='none', lw=0, z=2)
    cy = (top + bot) / 2
    ax.add_patch(Polygon([[3.0, cy - 1.35], [4.5, cy + 1.55], [6.0, cy - 1.35]],
                         closed=True, fc='none', ec=INK, lw=1.4, zorder=6))
    line(ax, 3.9, cy - 0.2, 5.1, cy - 0.2, color=INK, lw=1.3, z=6)
    T(ax, 6.7, cy, 'ATLASSIAN', fs=8.0, c=INK, bold=True, z=6)
    for name, x in [('Products', 18.0), ('Teams', 28.0), ('Why Atlassian?', 34.8),
                    ('Resources', 51.2), ('Enterprise', 62.4)]:
        T(ax, x, cy, name, fs=6.2, c=INK, z=6)
        if name != 'Enterprise':
            caret(ax, x + len(name) * 0.62 + 0.6, cy - 0.1, s=0.5, c=INK)
    magnifier(ax, 83.2, cy, r=0.55)
    grid_icon(ax, 86.6, cy, s=0.30, c=DARK)
    ellipse(ax, 89.6, cy, 1.05, fc='#4a5568', z=6)
    T(ax, 89.6, cy - 0.05, 'SY', fs=4.4, c='white', ha='center', bold=True, z=7)
    T(ax, 91.2, cy, 'Song Yinying…', fs=6.0, c=INK, z=6)


def hero(ax):
    T(ax, 50, 87.2, 'The new Jira:', fs=26, c=INK, ha='center', z=6)
    T(ax, 34.6, 82.2, 'from', fs=26, c=INK, z=6)
    T(ax, 41.0, 82.2, 'teams', fs=26, c=INK, bold=True, z=6)
    T(ax, 49.6, 82.2, 'to dreams', fs=26, c=INK, z=6)

    rect(ax, 43.4, 78.0, 13.2, 3.3, fc=BLACK, ec=BLACK, lw=1.0, r=1.65, z=7)
    T(ax, 50, 76.35, 'Get started', fs=7.4, c='white', ha='center', bold=True, z=8)

    items = [('software', 'Software', 16.5), ('pm', 'Product management', 30.5),
             ('mkt', 'Marketing', 44.5), ('proj', 'Project management', 59.0),
             ('design', 'Design', 73.5), ('it', 'IT', 87.0)]
    for kind, label, cx in items:
        product_icon(ax, kind, cx, 71.4, s=0.92)
        T(ax, cx, 67.8, label, fs=5.6, c=INK, ha='center', z=6)
    rect(ax, 52.4, 73.4, 13.2, 7.4, fc='none', ec=BLACK, lw=1.8, z=9)


def embedded_jira(ax, top=63.6, bot=47.2):
    x0, x1 = 3.0, 97.0
    rect(ax, x0, top, x1 - x0, top - bot, fc='white', ec=FRAME, lw=1.0, z=3)
    nh = 3.4
    rect(ax, x0, top, x1 - x0, nh, fc=SOFT, ec='none', lw=0, z=4)
    line(ax, x0, top - nh, x1, top - nh, color=FRAME, lw=0.7, z=5)
    cy = top - nh / 2
    grid_icon(ax, x0 + 1.6, cy, s=0.24, c=DARK)
    jira_logo(ax, x0 + 2.8, cy, fs=5.8)
    menus = [('Your work', 5.2), ('Projects', 4.2), ('Filters', 3.4),
             ('Dashboards', 5.4), ('People', 3.6), ('Apps', 2.8)]
    x = x0 + 7.8
    for name, w in menus:
        T(ax, x, cy, name, fs=4.3, c=DARK, z=6)
        caret(ax, x + w + 0.1, cy - 0.05, s=0.32)
        x += w + 2.0
    rect(ax, x - 0.8, cy + 0.85, 4.0, 1.8, fc=BLACK, ec=BLACK, lw=0.5, r=0.3, z=6)
    T(ax, x + 1.2, cy, 'Create', fs=4.2, c='white', ha='center', z=7)

    rect(ax, 77.6, cy + 0.9, 13.8, 1.8, fc='white', ec=FRAME, lw=0.6, r=0.3, z=5)
    magnifier(ax, 78.9, cy, r=0.32, c=GREY)
    T(ax, 79.9, cy, 'Search or ask anything', fs=4.0, c=GREY, z=6)
    bell(ax, 93.6, cy, s=0.55)
    ellipse(ax, 95.4, cy, 0.5, fc='none', ec=DARK, lw=0.6, z=6)
    T(ax, 95.4, cy - 0.05, '?', fs=3.6, c=DARK, ha='center', z=7)
    ellipse(ax, 96.8, cy, 0.55, fc='#4a5568', z=6)

    # 左侧窄栏
    sx0, sx1 = x0, x0 + 11.6
    rect(ax, sx0, top - nh, sx1 - sx0, (top - nh) - bot, fc=SOFT, ec='none', lw=0, z=4)
    line(ax, sx1, bot, sx1, top - nh, color=GRID, lw=0.7, z=5)
    T(ax, sx0 + 0.9, top - nh - 2.2, 'Overviews', fs=4.4, c=DARK, bold=True, z=6)
    T(ax, sx0 + 1.8, top - nh - 4.4, '▼ Website', fs=4.2, c=DARK, z=6)
    T(ax, sx0 + 1.8, top - nh - 6.4, '▼ Email', fs=4.2, c=DARK, z=6)
    T(ax, sx0 + 0.9, top - nh - 8.8, '▣  All projects', fs=4.3, c=DARK, z=6)

    # 主区：项目名 + 标签页
    mx = sx1 + 1.8
    rect(ax, mx, top - nh - 0.15, 1.7, 1.7, fc='#2c5282', ec='none', lw=0, r=0.3, z=6)
    T(ax, mx + 2.6, top - nh - 1.0, 'The Next Big Thing', fs=6.0, c=INK, bold=True, z=6)
    tabs = ['Summary', 'Board', 'List', 'Calendar', 'Timeline', 'Approvals', 'Forms',
            'Pages', 'Issues', 'Reports', 'Shortcuts', 'Apps', 'Project settings']
    ty = top - nh - 4.6
    tx = mx
    for t in tabs:
        w = len(t) * 0.56 + 1.0
        if t == 'Board':
            T(ax, tx, ty, t, fs=4.3, c=NAV, bold=True, z=6)
            line(ax, tx - 0.2, ty - 0.95, tx + w - 0.6, ty - 0.95, color=NAV, lw=1.2, z=6)
        else:
            T(ax, tx, ty, t, fs=4.3, c=DARK, z=6)
        if t in ('Shortcuts', 'Apps'):
            caret(ax, tx + len(t) * 0.56 + 0.2, ty - 0.05, s=0.3)
        tx += w
    line(ax, mx - 1.8, ty - 1.35, x1 - 1.2, ty - 1.35, color=GRID, lw=0.6, z=5)


def main():
    setup()
    fig, ax = canvas(13.8, 8.0, xlim=(0, 100), ylim=(40, 100))
    ax.set_position([0, 0, 1, 1])

    rect(ax, 0.8, 97.4, 98.4, 52.8, fc='white', ec=FRAME, lw=1.4, z=1)
    atlassian_nav(ax)
    hero(ax)
    embedded_jira(ax)

    T(ax, 50, 42.6, '图 7-13　Jira 软件界面', fs=10.5, c='#4a5568', ha='center', z=8)
    save(fig, OUT)


if __name__ == '__main__':
    main()
