;================================================================
; mahjong/mahjong_display 麻将游戏 显示部分（前端渲染）
; mahjong_text.erb 文本形式
;================================================================


;;;打印完整的整个牌山
;;;- (option)int x_ray 是否启用透视
;;;@return 没有返回值
@MahjongPrintTilesWall(x_ray=0)
    #DIM x_ray
    #DIM CONST TileColor, 9 = 0xd32f2f, 0xf57c00, 0xfbc02d, 0x388e3c, 0x7b1fa2, 0x303f9f, 0x0097a7, 0xffa000, 0xc2185b
    #DIM DYNAMIC i
    FOR i, 0, 136
        SELECTCASE i
            CASE IS < 48
                SETCOLOR TileColor:(i / 4 % 4)
                FONTSTYLE 1p2
            CASE 48, 50
                SETCOLOR TileColor:0
                FONTSTYLE 1p2
            CASE 49
                SETCOLOR TileColor:1
                FONTSTYLE 1p2
            CASE 51
                SETCOLOR TileColor:2
                FONTSTYLE 1p2
            CASE 52
                SETCOLOR TileColor:3
                FONTSTYLE 1p2
            CASE IS < 122  ; 山牌
                IF i == 53
                    PRINTL
                    SETCOLOR TileColor:4
                    SIF i < MJ_TILES_CURSOR
                        FONTSTYLE 1p2
                ELSE
                    IF i >= 121 - MJ_KAN_COUNT
                        SETCOLOR TileColor:5
                        SIF i > 121 - MJ_KAN_COUNT
                            FONTSTYLE 1p2
                    ELSE
                        SETCOLOR TileColor:4
                    ENDIF
                    SIF i < MJ_TILES_CURSOR
                        FONTSTYLE GETSTYLE()|1p2
                ENDIF
            CASE IS < 132  ; 宝牌
                SIF i == 122
                    PRINTL
                SETCOLOR i % 2 == 0 ? TileColor:6 # TileColor:7
                FONTSTYLE i % 2 == 0 ? 1p0 # 1p1
                SIF i < 124 + MJ_KAN_COUNT * 2
                    FONTSTYLE GETSTYLE()|1p3
            CASEELSE  ; 岭上牌
                SETCOLOR TileColor:8
                SIF i > 135 - MJ_KAN_COUNT
                    FONTSTYLE 1p2
        ENDSELECT
        PRINTS x_ray || (i >= 122 && i <= 122 + MJ_KAN_COUNT * 2 && i % 2 == 0) ? mj_humanize(mj_to_code(MJ_TILES_WALL:i)) # "□□"
        RESETCOLOR
        FONTREGULAR
        PRINTS " "
    NEXT
    PRINTL
    RETURN


;;;打印某家的手牌
;;;- int seat 绝对位置(东家/南家/西家/北家)
;;;- (option)int x_ray 是否启用透视
;;;@return 没有返回值
@MahjongPrintOthersHand(seat, x_ray=0)
    #DIM seat
    #DIM x_ray
    #DIM CONST SeatColor, 4 = 0xd32f2f, 0xf57c00, 0xfbc02d, 0x388e3c
    #DIM DYNAMIC i
    #DIMS DYNAMIC the_tile
    SETCOLOR SeatColor:seat
    PRINTFORM %"副露占位符 ", 20%
    FOR i, 0, VARSIZE("MJ_HAND", 1)
        the_tile '= mj_to_code(MJ_HAND:seat:i)
        SIF the_tile != ""
            PRINTS (x_ray ? mj_humanize(the_tile) # UNICODE(0x25a1) * 2) + " "
    NEXT
    RESETCOLOR
    PRINTL
    RETURN


;;;打印某家的牌河
;;;（舍张历史记录）
;;;- int seat 绝对位置(东家/南家/西家/北家)
;;;@return 没有返回值
@MahjongPrintDiscardPile(seat)
    #DIM seat
    #DIM DYNAMIC line_flag
    #DIM DYNAMIC i
    #DIMS DYNAMIC tile_code
    #DIMS DYNAMIC html
    line_flag = LINECOUNT
    FOR i, 0, VARSIZE("MJ_DISCARD_PILE", 1)
        SIF MJ_DISCARD_PILE:seat:i == ""
            BREAK
        SIF !ISNUMERIC(MJ_DISCARD_PILE:seat:i)
            CONTINUE
        tile_code '= mj_to_code(TOINT(MJ_DISCARD_PILE:seat:i))
        IF MJ_DISCARD_PILE:seat:(i + 1) == "" && seat == MJ_LAST_DISCARD_SEAT
            html += get_text_html(mj_humanize(tile_code), 1p1, "Cyan")
        ELSE
            html += get_text_html(mj_humanize(tile_code))
        ENDIF
        html += " "
    NEXT
    HTML_PRINT @"<div width='{CLIENTWIDTH()}px' height='200'>" + html + "</div>"
    PRINTL
    RETURN


;;;打印别家状况
;;;- int relative_seat 相对位置(自家/下家/对家/上家)
;;;@return 没有返回值
@MahjongPrintOthersStatus(relative_seat)
    #DIM relative_seat
    #DIM CONST SeatColor, 4 = 0xd32f2f, 0xf57c00, 0xfbc02d, 0x388e3c
    #DIM DYNAMIC seat
    seat = mj_get_seat_from_aspect(relative_seat)
    SETCOLOR SeatColor:seat
    PRINTSL " " + (seat == MJ_CURRENT_PLAYER ? UNICODE(0x273f) # "  ") + " " + MJ_ENUM_SEAT:seat + "家 " + TOSTR(MJ_PLAYER_POINTS:seat)
    RESETCOLOR
    CALL MahjongPrintOthersHand(seat, MJ_CHEAT_X_RAY)
    CALL MahjongPrintDiscardPile(seat)
    RETURN


;;;打印自家手牌
;;;@return 没有返回值
@MahjongPrintSelfHand()
    #DIM DYNAMIC playing
    #DIMS DYNAMIC melds, 4
    #DIM DYNAMIC i
    #DIMS DYNAMIC html
    #DIMS DYNAMIC the_tile
    playing = MJ_PLAYER_SEAT == MJ_CURRENT_PLAYER && mj_count_hand(MJ_PLAYER_SEAT) % 3 == 2
    SPLIT mj_get_melds(MJ_PLAYER_SEAT), ",", melds
    FOR i, 0, RESULT
        SIF melds:i != ""
            html += "TODO副露" + melds:i
    NEXT
    FOR i, 0, VARSIZE("MJ_HAND", 1)
        the_tile '= mj_to_code(MJ_HAND:MJ_PLAYER_SEAT:i)
        IF the_tile != ""
        {
            html += " " + get_text_html(
                mj_humanize(the_tile),
                playing ? 1p0 # 0,
                playing ? "Cyan" # "",
                "",
                playing ? TOSTR(0xb100|i) # ""
            )
        }
        ENDIF
    NEXT
    HTML_PRINT "<p align='center'>" + html + "</p>"
    PRINTL
    RETURN


;;;打印可用按钮
;;;@return 没有返回值
@MahjongPrintButtons
    #DIM DYNAMIC playing
    #DIM DYNAMIC options
    #DIM DYNAMIC allow_pass
    #DIMS DYNAMIC tips
    #DIMS DYNAMIC html
    playing = MJ_PLAYER_SEAT == MJ_CURRENT_PLAYER && mj_count_hand(MJ_PLAYER_SEAT) % 3 == 2
    options = mj_get_options(MJ_PLAYER_SEAT)
    tips '= "没有提示"
    IF options & MJ_ENUM_WIN
        html += get_text_html(playing ? "[自摸]" # "[ 和 ]", 1p0, "Cyan", "", TOSTR(0xb200|MJ_ENUM_WIN)) + get_space_html()
        allow_pass = !playing
    ENDIF
    SIF playing && (options & MJ_ENUM_RIICHI)
        html += get_text_html("[立直]", 1p0, "Cyan", "", TOSTR(0xb200|MJ_ENUM_RIICHI)) + get_space_html()
    IF options & MJ_ENUM_KAN
        html += get_text_html("[ 杠 ]", 1p0, "Cyan", "", TOSTR(0xb200|MJ_ENUM_KAN)) + get_space_html()
        allow_pass = !playing
    ENDIF
    IF !playing && (options & MJ_ENUM_PON)
        html += get_text_html("[ 碰 ]", 1p0, "Cyan", "", TOSTR(0xb200|MJ_ENUM_PON)) + get_space_html()
        allow_pass = 1
    ENDIF
    IF !playing && (options & MJ_ENUM_CHII)
        html += get_text_html("[ 吃 ]", 1p0, "Cyan", "", TOSTR(0xb200|MJ_ENUM_CHII)) + get_space_html()
        allow_pass = 1
    ENDIF
    SIF allow_pass
        html += get_text_html("[跳过]", 1p0, "Cyan", "", TOSTR(0xb200|MJ_ENUM_SKIP)) + get_space_html()
    SIF playing && (options & MJ_ENUM_DRAW)
        html += get_text_html("[流局]", 1p0, "Cyan", "", TOSTR(0xb200|MJ_ENUM_DRAW)) + get_space_html()
    html += get_space_html(5)
    html += get_text_html("提示", 1p1, "#0097a7", tips)
    html += get_space_html()
    HTML_PRINT @"<p align='right'>%html%</p>"
    RETURN


;;;打印当前麻将桌面状况
;;;@return 没有返回值
@MahjongPrintDesk()
    #DIM CONST SeatColor, 4 = 0xd32f2f, 0xf57c00, 0xfbc02d, 0x388e3c
    #DIMS DYNAMIC html
    #DIM DYNAMIC i
    CALL PrintLine
    html '= @"<b>%MJ_ENUM_SEAT:MJ_STARTING_WIND%{MJ_ROUND_INDEX}局</b>"
    html += @"  {MJ_HONBA_COUNT}本场  <i>立直棒×{MJ_RIICHI_COUNT}</i>"
    HTML_PRINT "<p align='center'>" + html + "</p>"
    PRINTL
    CALL MahjongPrintTilesWall(MJ_CHEAT_X_RAY)
    PRINTL
    html '= @"余牌数量 {mj_count_wall()}"
    HTML_PRINT "<p align='center'>" + html + "</p>"
    FOR i, 1, 4
        PRINTL
        CALL MahjongPrintOthersStatus(i)
    NEXT
    PRINTL
    CALL MahjongPrintDiscardPile(MJ_PLAYER_SEAT)
    html '= " " + (MJ_PLAYER_SEAT == MJ_CURRENT_PLAYER ? UNICODE(0x273f) # "  ") + " "
    html += @"%MJ_ENUM_SEAT:MJ_PLAYER_SEAT%家 {MJ_PLAYER_POINTS:MJ_PLAYER_SEAT}"
    html '= @"<font color='#%TOSTR(SeatColor:MJ_PLAYER_SEAT, "x6")%'>%html%</font>"
    HTML_PRINT @"<div ypos='100' width='{CLIENTWIDTH()}px' height='100'>%html%</div>"
    MJ_LINE_FLAG = LINECOUNT
    CALL MahjongPrintButtons
    PRINTL
    CALL MahjongPrintSelfHand
    CALL PrintLine
    CALL PrintLog(MJ_LOG_TEXT, "tips")
    RETURN
