#coding=utf-8

module Mobile
  module Apis
    class Homeworks < Grape::API

      def self.get_service
        HomeworkService.new
      end

      resources :homeworks do
        desc "作业详情"
        params do
          requires :id, type: Integer, desc: "作业ID"
        end
        route_param  :id do
          get do
            homework = Homeworks.get_service.show_homework params
            present :data, homework, with: Mobile::Entities::Homework
            present :status, 0
          end
        end

        desc "我的作品列表"
        params do
          requires :token, type: String
        end
        get ':user_id/homework_attachs' do
          ue = Homeworks.get_service.my_homework_list params,current_user.nil? ? User.find(2):current_user
          present :data, ue,with: Mobile::Entities::Course
          present :status, 0
        end

        # desc "启动匿评"
        # params do
        #   requires :token, type: String
        # end
        # post ':id/start_anonymous_comment' do
        #   statue = Homeworks.get_service.start_anonymous_comment params,current_user.nil? ? User.find(2):current_user
        #   messages = ""
        #   case statue
        #     when 1
        #       messages = "启动成功"
        #     when 2
        #       messages = "启动失败，作业总数大于等于2份时才能启动匿评"
        #     when 3
        #       messages = "已开启匿评，请务重复开启"
        #   end
        #   present :data,messages
        #   present :status, statue
        # end
        #
        # desc "关闭匿评"
        # params do
        #   requires :token, type: String
        # end
        # post ':id/stop_anonymous_comment' do
        #   Homeworks.get_service.stop_anonymous_comment params,current_user.nil? ? User.find(2):current_user
        #   present :status, 0
        # end

        desc "匿评作品详情"
        params do
          requires :token, type: String
        end
        get ':homework_id/anonymous_works_show' do
          works,par = Homeworks.get_service.anonymous_works_show params.merge(:id => params[:homework_id]),current_user.nil? ? User.find(2):current_user
          present :data, works ,with: Mobile::Entities::HomeworkAttach
          present :otherdata,par,with: Mobile::Entities::AnonymousWorksParams
          present :status, 0
        end

        desc "作品打分"
        params do
          requires :token, type: String
          requires :is_teacher, type: String,desc: '是否为教师（匿评作品详情返回的结果中可获取此参数的值）'
          requires :is_anonymous_comments, type: String, desc: '是否为匿评（匿评作品详情返回的结果中可获取此参数的值）'
          optional :stars_value, type: Integer,desc: '用户给出的评分'
          optional :cur_page,type: Integer,desc: '匿评作品详情返回的结果中可获取此参数的值'
          optional :cur_type, type: Integer,desc: '匿评作品详情返回的结果中可获取此参数的值'
          optional :user_message, type: String, desc: '用户评论'
        end

        post ':homework_id/scoring' do
          cs_params = {
              new_form: params.reject{|k,v| [:token,:is_teacher,:is_anonymous_comments,:stars_value,:cur_page,:cur_type,:homework_id].include?(k)},
              token: params[:token],
              is_teacher: params[:is_teacher],
              is_anonymous_comments: params[:is_anonymous_comments],
              stars_value: params[:stars_value],
              cur_page: params[:cur_page],
              cur_type: params[:cur_type],
              homework_id: params[:homework_id]
          }
          Homeworks.get_service.add_score_and_jour cs_params,current_user
          present :status, 0
        end

        desc '创建作业'
        params do
          requires :token,type:String
          requires :work_name,type:String,desc:'作业名称'
          requires :work_desc,type:String,desc:'作业描述'
          requires :work_deadline,type:String,desc:'截止日期'
          requires :is_blind_appr,type:Integer,desc:'是否匿评'
          requires :blind_appr_num,type:Integer,desc:'匿评分配数'
          requires :course_id,type:Integer,desc: '课程id'
        end
        post 'create_home_work' do
          Homeworks.get_service.create_home_work params,current_user
          present :status, 0
        end

        desc '开启匿评'
        params do
          requires :token,type:String
          requires :course_id,type:Integer,desc:'课程id'
          requires :homework_id,type:Integer,desc:'作业id'
        end
        post ':homework_id/start_anonymous_comment' do
          hs = Homeworks.get_service
          status = hs.start_anonymous_comment params,current_user
            messages = ""
            case status[:status]
              when 1
                messages = "启动成功"
              when 2
                messages = "启动失败，作业总数大于等于2份时才能启动匿评"
              when 3
                messages = "已开启匿评，请务重复开启"
              when 4
                messages = "没有开启匿评的权限"
              when 5
                messages = "截止日期之前不可启动匿评"
            end
          present :data,messages
          present :status,0
        end

        desc '关闭匿评'
        params do
          requires :token,type:String
          requires :course_id,type:Integer,desc:'课程id'
          requires :homework_id,type:Integer,desc:'作业id'
        end
        post ':homework_id/stop_anonymous_comment' do
          hs = Homeworks.get_service
          hs.stop_anonymous_comment params,current_user
          message = "成功关闭"
          present :data, message
          present :status,0
        end

      end
    end
  end
end
