#coding=utf-8
module Mobile
  module Apis
    class Users < Grape::API
      resource :users do

        desc "注册用户"
        params do
          requires :login, type: String, desc: 'username'
          requires :mail, type: String, desc: 'mail'
          requires :password, type: String, desc: 'password'
        end
        post do
          us = UsersService.new
          user = us.register params.merge(:password_confirmation => params[:password],
                                          :should_confirmation_password => true)
          raise "该邮箱已经被注册过了" if user.new_record?
          
          present :data, user, with: Mobile::Entities::User
          present :status, 0
        end

        desc "显示用户"
        params do
          requires :id, type: Integer
        end
        route_param :id do
          get do
            us = UsersService.new
            ue = us.show_user params
            present :data, ue,with: Mobile::Entities::User
            present :status, 0
          end
        end

        desc "修改用户"
        params do
          requires :token, type: String
          #optional :file, type: File, desc: 'avatar'
          optional :occupation, type: String
          optional :brief_introduction, type: String
          optional :province, type: String
          optional :city, type: String
          optional :gender, type: Integer
        end
        put ':id' do
          authenticate!
          us = UsersService.new
          ue = us.edit_user params.merge(id: current_user.id)
          present :data, ue,with: Mobile::Entities::User
          present :status, 0
        end

        desc '获取用户课程'
        params do
          optional :token, type: String
        end

        get ':id/courses' do
          us = UsersService.new
          ue = us.user_courses_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
          requires :password, type:String , desc: '原密码'
          requires :new_password, type: String, desc: '新密码'
        end
        post 'password' do
          authenticate!
          us = UsersService.new
          user = us.change_password params.merge(current_user_id: current_user.id,
                                               new_password_confirmation: params[:new_password])
          present :data, user, with: Mobile::Entities::User
          present :status, 0
        end

        desc "用户搜索"
        params do
          requires :name, type: String, desc: '用户名关键字'
          requires :search_by, type: String,desc: '搜索依据：0 登录名，1 用户名，2 邮箱,3 登录名和姓名'
          optional :is_search_assitant,type:Integer,desc:'是否搜索注册用户来作为助教'
          optional :course_id,type:Integer,desc: '课程id，搜索注册用户不为该课程教师的其他用户'
          optional :user_id,type:Integer,desc:'用户id'
        end
        get 'search/search_user' do
          us = UsersService.new
          user = us.search_user params
          present :data, user, with: Mobile::Entities::User
          present :status, 0
        end

        desc "用户留言"
        params do
          requires :token, type: String
          requires :user_id, type: Integer,desc: '被留言的用户id'
          requires :page,type:Integer,desc:'请求数据的页码'
        end
        get ':user_id/messages' do
          us = UsersService.new
          jours = us.get_all_messages params
          present :data,jours,with:Mobile::Entities::Jours
          present :status,0
        end

        desc "回复用户留言"
        params do
          requires :token, type: String
          requires :user_id, type: Integer,desc: '被留言的用户id'
          requires :content,type:String,desc:'留言内容'
          requires :ref_user_id,type:Integer,desc:'被回复的用户id'
          requires :parent_id,type:Integer,desc:'留言父id'
          requires :ref_message_id,type:Integer,desc:'引用消息id'
          optional :type,type:Integer,desc:'回复类型'
          optional :course_id,type:Integer,desc:'课程id'
        end
        post ':user_id/reply_message' do
          us = UsersService.new
          jours = us.reply_user_messages params,current_user
          present :status,0
        end

        desc "给用户留言"
        params do
          requires :token, type: String
          requires :user_id, type: Integer,desc:'被留言的用户id'
          requires :content, type: String,desc:'留言内容'
        end
        post ':user_id/leave_message' do
          us = UsersService.new
          us.leave_message params,current_user
          present :data,0
        end

        desc "与我相关"
        params do
          requires :token, type: String
          requires :page,type:Integer,desc:'页码'
        end
        get ':user_id/all_my_dynamic' do
          us = UsersService.new
          my_jours = us.reply_my_messages params,current_user
          present :data,my_jours,with:Mobile::Entities::Jours
          present :status,0
        end
      end
    end
  end
end


