import base64
import os

from hoshino import Service

from .caculate import *
from ..plugin_utils.base_util import get_img_cq

sv = Service('uma_endurance')
with open(os.path.join(os.path.dirname(__file__), f'{sv.name}_help.png'), 'rb') as f:
    base64_data = base64.b64encode(f.read())
    s = base64_data.decode()
sv.help = f'![](data:image/jpeg;base64,{s})'


# å¸®åŠ©ç•Œé�¢
@sv.on_fullmatch("é©¬å¨˜è€�åŠ›å¸®åŠ©")
async def sv_help(bot, ev):
    img_path = os.path.join(os.path.dirname(__file__), f'{sv.name}_help.png')
    sv_help_ = await get_img_cq(img_path)
    await bot.send(ev, sv_help_)


@sv.on_rex(r'''
ç®—è€�åŠ›\r?
å±�æ€§(:|ï¼š)(\d{1,4}) (\d{1,4}) (\d{1,4}) (\d{1,4}) (\d{1,4})\r?
é€‚åº”æ€§(:|ï¼š)(\S{2})-(\S) (\S{1,2})-(\S) (\d{4})-(\S)\r?
å¹²åŠ²(:|ï¼š)(\S{2,3}) çŠ¶å†µ(:|ï¼š)(\S{1,2})\r?
å›ºå›�(:|ï¼š)(\d{1,2}) æ™®å›�(:|ï¼š)(\d{1,2}) é‡‘å›�(:|ï¼š)(\d{1,2})'''.strip())
async def get_endurance(bot, ev):
    # è�·å�–å�„ä¸ªæ•°æ�®
    # é€Ÿåº¦ä¸Šé™�
    speed_limit = int(ev['match'].group(2))
    # è€�åŠ›
    endurance_tmp = int(ev['match'].group(3))
    # åŠ›é‡�
    power = int(ev['match'].group(4))
    # æ ¹æ€§
    determination = int(ev['match'].group(5))
    # æ™ºåŠ›
    intelligence = int(ev['match'].group(6))
    # è·‘æ³•ï¼šé€ƒé©¬ã€�å…ˆé©¬ã€�å·®é©¬ã€�è¿½é©¬
    run_type = ev['match'].group(8)
    # è·‘æ³•é€‚åº”æ€§ï¼šS, A, B, C, D, E, F, G
    run_adaptability = ev['match'].group(9).upper()
    # åœºåœ°ç±»å�‹ï¼šèŠ�ã€�æ³¥åœ°
    site_type = ev['match'].group(10)
    # åœºåœ°é€‚åº”æ€§ï¼šS, A, B, C, D, E, F, G
    site_adaptability = ev['match'].group(11).upper()
    # è·‘é�“é•¿åº¦ï¼š1000, 1200, 1400, 1600, 1800, 2000, 2200, 2400, 2500, 3000, 3200, 3400, 3600
    track_length = int(ev['match'].group(12))
    # è·‘é�“é•¿åº¦é€‚åº”æ€§ï¼šS, A, B, C, D, E, F, G
    track_adaptability = ev['match'].group(13).upper()
    # å¹²åŠ²ï¼šç»�å¥½è°ƒã€�å¥½è°ƒã€�æ™®é€šã€�ä¸�è°ƒã€�ç»�ä¸�æ�‰
    feeling = ev['match'].group(15)
    # åœºå†µï¼šè‰¯ã€�ç¨�é‡�ã€�é‡�ã€�ä¸�è‰¯
    situation = ev['match'].group(17)
    # å›ºæœ‰å›�ä½“ç­‰çº§
    stable_recover_level = int(ev['match'].group(19))
    # æ™®é€šå›�ä½“ä¸ªæ•°
    common_recover_num = int(ev['match'].group(21))
    # é‡‘å›�ä½“ä¸ªæ•°
    upper_recover_num = int(ev['match'].group(23))

    # å¼€å§‹è®¡ç®—
    # é€Ÿåº¦ä¸Šé™�è¡¥æ­£
    speed_limit_patch = await judge_speed(speed_limit, site_type, feeling, situation)
    # ä½“åŠ›è¡¥æ­£
    hp_bonus = await judge_hp_bonus(run_type)
    # åŠ›é‡�è¡¥æ­£
    power_patch = await judge_power(power, site_type, feeling, situation)
    # æ ¹æ€§è¡¥æ­£
    determination_patch = await judge_determination(determination, feeling)
    # æ™ºåŠ›è¡¥æ­£
    intelligence_patch = await judge_intelligence(intelligence, feeling, run_adaptability)
    # åŸºå‡†é€Ÿåº¦
    speed_standard_patch = await speed_standard(track_length)
    # ç»ˆç›˜ä½“åŠ›æ¶ˆè€—æ¯”
    end_endurance_bonus = 1 + 200 / ((600 * determination_patch) ** 0.5)
    # åº�ç›˜ä½“åŠ›éœ€æ±‚
    endurance_begin, uniform_speed_begin = await calcu_begin_endurance(speed_standard_patch, run_type,
                                                                       intelligence_patch, power_patch,
                                                                       site_adaptability, track_length,
                                                                       track_adaptability, site_type, situation)
    # ä¸­ç›˜ä½“åŠ›éœ€æ±‚
    endurance_middle, uniform_speed_middle = await calcu_middle_endurance(uniform_speed_begin, speed_standard_patch,
                                                                          run_type,
                                                                          intelligence_patch, power_patch,
                                                                          site_adaptability, track_length,
                                                                          track_adaptability, site_type, situation)
    # ç»ˆç›˜ä½“åŠ›éœ€æ±‚
    endurance_end = await calcu_end_endurance(speed_limit_patch, uniform_speed_middle, speed_standard_patch, run_type,
                                              intelligence_patch, power_patch, site_adaptability, track_length,
                                              track_adaptability, site_type, situation, end_endurance_bonus)
    # ç�†è®ºæ€»ä½“åŠ›éœ€æ±‚
    hp = endurance_begin + endurance_middle + endurance_end
    # ç�†è®ºæ€»è€�åŠ›éœ€æ±‚
    endurance = await calcu_endurance(endurance_begin, endurance_middle, endurance_end, track_length, run_type)
    # ç�†è®º
    # ç�†è®ºä½“åŠ›
    theoretical_hp = await theoretical_endurance(track_length, endurance_tmp * feeling_bonus[feeling], run_type)
    # å›�ä½“æŠ€èƒ½æŠ˜ç®—è€�åŠ›
    stable_recover_end, common_recover_end, upper_recover_end = await calcu_skill_end(stable_recover_level,
                                                                                      theoretical_hp, run_type)
    # å›�ä½“æŠ€èƒ½æŠ˜ç®—ä½“åŠ›
    stable_recover, common_recover_single, common_recover, upper_recover_single, upper_recover = await calcu_skill(
        stable_recover_level, common_recover_num, upper_recover_num, theoretical_hp)
    # ç®—ä¸ŠæŠ€èƒ½å��çš„æ€»ä½“åŠ›
    end_hp = theoretical_hp + stable_recover + common_recover + upper_recover
    # ç®—ä¸ŠæŠ€èƒ½å��çš„æ€»è€�åŠ›
    end_endurance = await get_end_endurance(end_hp, track_length, run_type)
    # ç»“è®º
    if end_endurance > endurance:
        conclusion = f'ğŸ�‰æ�­å–œæ‚¨ï¼Œæ‚¨çš„é©¬å¨˜å�¯ä»¥æ­£å¸¸è·‘å®Œï¼�ç”šè‡³å¯Œä½™äº†{round(end_endurance - endurance, 1)}è€�åŠ›'
    else:
        conclusion = f'ğŸ˜±å¾ˆæŠ±æ­‰ï¼Œæ‚¨çš„é©¬å¨˜æ— æ³•æ­£å¸¸è·‘å®Œï¼Œè¿˜éœ€è¦�è¡¥å……{round(endurance - end_endurance, 1)}è€�åŠ›ï¼Œè¯·æŒ‰ç…§æŠ€èƒ½å›�è€�é‡�è‡ªè¡Œå®‰æ�’ã€‚'

    msg = f'''
é€Ÿåº¦ä¸Šé™�ï¼š{speed_limit}
è€�åŠ›ï¼š{endurance_tmp}
åŠ›é‡�ï¼š{power}
æ ¹æ€§ï¼š{determination}
æ™ºåŠ›ï¼š{intelligence}
è·‘æ³•ï¼š{run_type} - {run_adaptability}
è·‘åœºï¼š{site_type} - {site_adaptability}
è·‘é�“ï¼š{track_length}ç±³ - {track_adaptability}
å¤©æ°”ï¼š{situation} | å¹²åŠ²ï¼š{feeling}

åº�ç›˜ä½“åŠ›éœ€æ±‚ï¼š{round(endurance_begin, 1)}
ä¸­ç›˜ä½“åŠ›éœ€æ±‚ï¼š{round(endurance_middle, 1)}
ç»ˆç›˜ä½“åŠ›éœ€æ±‚ï¼š{round(endurance_end, 1)}

æœ¬æ¬¡æ�ºå¸¦äº†ï¼š
    - {stable_recover_level}çº§çš„å›ºæœ‰å›�ä½“
    - {common_recover_num}ä¸ªæ™®é€šå›�ä½“
    - {upper_recover_num}ä¸ªé‡‘å›�ä½“

å…¶ä¸­ï¼š
-æ¯�ä¸ªæ™®é€šå›�ä½“å›�å¤�{round(common_recover_single, 1)}ä½“åŠ›
    - æŠ˜å�ˆè€�åŠ›ï¼š{round(common_recover_end, 1)}
-æ¯�ä¸ªé‡‘å›�ä½“å›�å¤�{round(upper_recover_single, 1)}ä½“åŠ›
    - æŠ˜å�ˆè€�åŠ›ï¼š{round(upper_recover_end, 1)}
-å›ºæœ‰ä½“åŠ›å›�å¤�{round(stable_recover, 1)}ä½“åŠ›
    - æŠ˜å�ˆè€�åŠ›ï¼š{round(stable_recover_end, 1)}

æ­£å¸¸è·‘å®Œéœ€è¦�ï¼š{round(hp, 1)}ä½“åŠ›
    - æŠ˜å�ˆè€�åŠ›ï¼š{round(endurance, 1)}
å½“å‰�é©¬å¨˜å®�é™…ï¼š{round(end_hp, 1)}ä½“åŠ›
    - æŠ˜å�ˆè€�åŠ›ï¼š{round(end_endurance, 1)}
å…¶ä¸­æŠ€èƒ½å›�å¤�äº†ï¼š{round(stable_recover + common_recover + upper_recover, 1)}ä½“åŠ›
    - æŠ˜å�ˆè€�åŠ›ï¼š{round(end_endurance - endurance_tmp, 1)}

ç»“è®ºï¼š
    {conclusion}

æ³¨ï¼šæ­¤æ•°æ�®å�–è‡ªæ ¹æ€§ä¸‹å�¡æ”¹ç‰ˆå‰�çš„æ•°æ�®
å®�é™…éœ€æ±‚æ¯”è®¡ç®—å™¨ç»“æ�œè¦�é«˜ä¸�å°‘ï¼Œå°¤å…¶æ˜¯å¤§èµ›
æœ¬è®¡ç®—å™¨ä»…ä¸ºèƒ½åˆšå¥½æ­£å¸¸è·‘å®Œçš„è€�åŠ›æœ€ä½�ä¸‹é™�
æ•°å€¼ä¸ºé��å¸¸ç�†æƒ³çš„æƒ…å†µï¼Œæ²¡æœ‰åŠ é€ŸæŠ€èƒ½ï¼Œå› æ­¤ä»…ä¾›å�‚è€ƒ
'''.strip()
    await bot.send(ev, msg)
