module CompetitionsHelper
  def chart_school_str user_ids
    chart_school_name = ""
    chart_school_name = School.where(id: UserExtension.where(user_id: user_ids).pluck(:school_id).uniq).pluck(:name).join("、")
  end

  # 耗时:小时、分、秒  00:00:00
  # 小于1秒钟则不显示
  def com_spend_time time
    hour = time / (60*60)
    min = time % (60*60) / 60
    sec = time % (60*60) % 60
    hour_str = "00"
    min_str = "00"
    sec_str = "00"
    if hour >= 1 && hour < 10
      hour_str = "0#{hour}"
    elsif hour >= 10
      hour_str = "#{hour}"
    end

    if min >= 1 && min < 10
      min_str = "0#{min}"
    elsif min >= 10
      min_str = "#{min}"
    end

    if sec >= 1 && sec < 10
      sec_str = "0#{sec}"
    elsif sec >= 10
      sec_str = "#{sec}"
    end

    time = "#{hour_str} : #{min_str} : #{sec_str}"
    return time
  end

  def chart_stages competition
    stages = []
    statistic_stages = competition.competition_stages.where("score_rate > 0")

    unless statistic_stages.size == 1
      end_time = competition.max_stage_end_time || competition.end_time
      if end_time && end_time < Time.now
        stages << {id: nil, name: statistic_stages.size > 1 ? "总排行榜" : "排行榜", rate: 1.0, start_time: competition.start_time, end_time: competition.end_time}
      end
    end

    statistic_stages.each do |stage|
      if (stage.max_end_time && stage.max_end_time < Time.now) || stage.max_end_time.nil?
        stages << {id: stage.id, name: "#{stage.name}排行榜", rate: stage.score_rate, start_time: stage.min_start_time, end_time: stage.max_end_time}
      end
    end

    # stages = stages.sort { |a, b| b[:end_time] <=> a[:end_time] } if stages.size > 0
    return stages
  end
end