import datetime
import requests
from loguru import logger

# logger_file = '/root/develop/log/dotabot.log'
logger_file = 'dotabot.log'
logger.add(logger_file)

def convert_seconds_to_hms(total_seconds):
  hours, remainder = divmod(total_seconds, 3600)
  minutes, seconds = divmod(remainder, 60)
  return hours, minutes, seconds

def is_workday():
  return datetime.datetime.today().weekday() < 5

def is_game_time():
  # game time is workday 21:00 - 1:00, weekend 10:00 - 1:00
  if is_workday():
    return datetime.datetime.now().hour >= 21 or datetime.datetime.now().hour < 1
  else:
    return datetime.datetime.now().hour >= 10 or datetime.datetime.now().hour < 1


def heartbeat():
  resp = requests.get('https://up.tunpok.com/api/push/BDb4MJWDVh?status=up&msg=OK&ping=')
  if not resp.ok:
    logger.error('fail to heartbeat, resp status: %s' % resp.status_code)


def get_ranking(ranking_int):
  #  (10-15: Herald, 20-25: Guardian, 30-35: Crusader, 40-45: Archon, 50-55: Legend, 60-65: Ancient, 70-75: Divine, 80-85: Immortal).
  if not ranking_int:
    return ''
  stars = ranking_int % 10
  if ranking_int < 20:
    return '先锋 %s' % stars
  elif ranking_int < 30:
    return '卫士 %s' % stars
  elif ranking_int < 40:
    return '中军 %s' % stars
  elif ranking_int < 50:
    return '统帅 %s' % stars
  elif ranking_int < 60:
    return '传奇 %s' % stars
  elif ranking_int < 70:
    return '万古流芳 %s' % stars
  elif ranking_int < 80:
    return '超凡入圣 %s' % stars
  elif ranking_int < 90:
    return '不朽 %s' % stars
  else:
    return 'Unknown %s' % stars


def shorten_digits(num):
  # if num like 12345, return 1.2w
  # if num like 1234, return 1.2k
  # if num < 1000, return num
  # if result ends with 0, remove it
  if num >= 10000:
    return '%.1fw' % (num / 10000)
  elif num >= 1000:
    return '%.1fk' % (num / 1000)
  else:
    return str(num)
