module Mobile
  module Entities
    class HomeworkAttach < Grape::Entity
      include Redmine::I18n
      def self.homework_attach_expose(field)
        expose field do |f,opt|
          if f.is_a?(Hash) && f.key?(field)
            f[field]
          elsif f.is_a?(::HomeworkAttach)
            if f.respond_to?(field)
              if field == :created_at
                format_time(f.send(:created_at))
              else
                f.send(field)
              end
            else
               case field
                 when :homework_times
                   f.bid.courses.first.homeworks.index(f.bid) + 1 unless (f.bid.nil? || f.bid.courses.nil? || f.bid.courses.first.nil?)
                 when :comment_status
                   f.bid.comment_status
               end
            end
          end
        end
      end

      homework_attach_expose :id
      homework_attach_expose :name
      homework_attach_expose :homework_times
      homework_attach_expose :description
      homework_attach_expose :created_at
      #comment_status 0:所属作业尚未开启匿评，1：匿评中 2：匿评结束
      homework_attach_expose :comment_status
      expose :attachments,using: Mobile::Entities::Attachment do |f, opt|
        if f.respond_to?(:attachments)
          f.send(:attachments)
        end
      end
    end
  end
end