#coding=utf-8
module Mobile
  module Apis
    class Resources < Grape::API

      resource :resources do

        desc '获取所有课件'
        params do
          requires :token, type: String
          requires :page, type: Integer
        end
        post do
          authenticate!
          page = params[:page] ? params[:page] : 0

          rs = ResourcesService.new
          # data = current_user.course_attachments
          data = rs.all_course_attachments current_user
          all_count = data.count
          data = data.limit(10).offset(page * 10)
          count = data.count

          present :data, data, with: Mobile::Entities::Attachment,user: current_user
          present :all_count, all_count
          present :count, count
          present :page, page
          present :status, 0

        end



        desc '获取所有作业'
        params do
          requires :token, type: String
          requires :page, type: Integer
        end
        post 'homeworks' do
          authenticate!

          page = params[:page] ? params[:page] : 0

          rs = ResourcesService.new
          homeworks = rs.all_homework_commons current_user

          all_count = homeworks.count
          homeworks = homeworks.limit(10).offset(page * 10)
          count = homeworks.count

          present :data, homeworks, with: Mobile::Entities::Homework,user: current_user
          present :all_count, all_count
          present :count, count
          present :page, page
          present :status, 0

        end

        desc '获取所有测验'
        params do
          requires :token, type: String
          requires :page, type: Integer
        end
        post 'exercises' do
          authenticate!

          page = params[:page] ? params[:page] : 0

          rs = ResourcesService.new
          exercises = rs.all_exercises current_user
          all_count = exercises.count
          exercises = exercises.limit(10).offset(page * 10)
          count = exercises.count

          present :data, exercises, with: Mobile::Entities::Exercise,user: current_user
          present :all_count, all_count
          present :count, count
          present :page, page
          present :status, 0
        end


        desc '发送资源'
        params do
          requires :token, type: String
          requires :course_ids, type: Array[Integer]
          requires :send_id, type: Integer
          requires :send_type, type: Integer
        end
        post 'send' do
          authenticate!

          rs = ResourcesService.new

          ori = nil
          flag = false
          save_message = nil

          case params[:send_type]
            when 1
              ori, flag, save_message = rs.send_resource_to_course(current_user,params)
            when 2
              ori, flag, save_message = rs.send_homework_to_course(current_user,params)
            when 3
              ori, flag, save_message = rs.send_exercise_to_course(current_user,params)
          end

          if flag
            present :status, 0
          else
            {status: -1, message: save_message.first}
          end
        end

      end

    end
  end
end