class UsersController < ApplicationController

  before_action :load_user, only: [:show, :homepage_info]
  before_action :check_user_exist, only: [:show, :homepage_info]

  def show;end

  def update
    @user = User.find params[:id]
    @user.update!(user_params)
    render_ok
  rescue Exception => e
    uid_logger_error(e.message)
    tip_exception(e.message)
    raise ActiveRecord::Rollback
  end

  # 贴吧获取用户信接口
  def get_user_info
    begin
      @user = current_user
      # TODO 等消息上线再打开注释
      #@tidding_count = unviewed_tiddings(current_user) if current_user.present?
      @course =
          if params[:course_id]
            Course.find params[:course_id]
          elsif params[:board_id]
            Board.find(params[:board_id]).course
          elsif params[:graduation_topic_id]
            GraduationTopic.find(params[:graduation_topic_id]).course
          elsif params[:graduation_group_id]
            GraduationGroup.find(params[:graduation_group_id]).course
          elsif params[:graduation_work_id]
            GraduationWork.find(params[:graduation_work_id]).course
          elsif params[:graduation_task_id]
            GraduationTask.find(params[:graduation_task_id]).course
          elsif params[:poll_id]
            Poll.find(params[:poll_id]).course
          elsif params[:attachment_id]
            Attachment.find(params[:attachment_id]).course
          end
      @course_identity = current_user.course_identity(@course) if @course
    rescue Exception => e
      missing_template
    end

  end

  def attachment_show
    file_name = params[:file_name]
    path = params[:path]
    send_file "#{path}/#{file_name}", :filename => "#{file_name}",
              :type => 'game',
              :disposition => 'attachment' #inline can open in browser
  end

  # Redo: 消息总数缓存
  def get_navigation_info
    @old_domain = edu_setting('old_edu_host')
    @user = current_user
    # 新消息数
    @new_message = @user.tidings.where("created_at > '#{@user.click_time}'").count > 0 || @user.private_messages.where("created_at > '#{@user.click_time}'").count > 0

    @user_url = "#{@old_domain}/users/#{@user.login}"
    @career = Career.where(status: true).order("created_at asc").pluck(:id, :name)
    ec_user = EcSchoolUser.where(:user_id => current_user.id).first
    @auth = ec_user ? "#{@old_domain}/ecs/department?school_id=#{ec_user.school_id}" : nil
  end

  # 用户回复功能
  def reply_message
    @message = JournalsForMessage.new(reply_message_params)
    @message.user_id = current_user.id
    @message.save!
    #normal_status("回复成功")
  end

  # 搜索用户具有管理员角色的项目
  def search_user_projects
    condition = '%%'
    condition = "%#{params[:search].strip}%".gsub(" ","") if !params[:search].blank?

    project_ids = Project.find_by_sql("SELECT p.id FROM projects p, members m, member_roles mr WHERE m.project_id = p.id
                        AND m.id=mr.member_id AND mr.role_id = 3 AND m.user_id=#{current_user.id} AND p.status != 9 and
                        p.name like '#{condition}'")
    @projects = Project.where(id: project_ids.pluck(:id))
  end

  # 个人主页信息
  def homepage_info;end

  def brief_introduction
    content = params[:content].to_s.strip
    if content.blank?
      render_error('内容不能为空')
      return
    end

    current_user.user_extension.update!(brief_introduction: content)

    render_ok
  end

  def attendance
    attendance = Users::AttendanceService.call(current_user)
    render_ok(grade: current_user.grade, next_gold: attendance.next_gold)
  rescue Users::AttendanceService::Error => ex
    render_error(ex.message)
  end

  private
  def load_user
    @user = User.find_by_login(params[:id]) || User.find_by(id: params[:id])
  end

  def user_params
    params.require(:user).permit(:nickname, :lastname, :show_realname,
                                  user_extension_attributes: [
                                  :gender, :location, :location_city,
                                  :occupation, :technical_title,
                                  :school_id, :department_id]
                                )
  end

  def reply_message_params
    normal_status(-1, "参数不对") if params[:journals_for_message][:jour_type].nil? || params[:journals_for_message][:jour_id].nil? ||
        params[:journals_for_message][:notes].nil? || params[:journals_for_message][:reply_id].nil?
    params.require(:journals_for_message).permit(:jour_type, :jour_id, :notes, :m_parent_id, :reply_id)
  end

  def check_user_exist
    return if @user.present?
    render_not_found
  end

end