#coding=utf-8

class AtController < ApplicationController
  respond_to :json

  def show
    @logger = Logger.new(Rails.root.join('log', 'at.log').to_s)
    users = find_at_users(params[:type], params[:id])
    @users = users
    @users = users.uniq { |u| u.id }.delete_if { |u| u.id == User.current.id }.sort{|x,y| to_pinyin(x.show_name) <=> to_pinyin(y.show_name)} if users

    #加上all
    if @users.size > 0
      allUser = Struct.new(:id, :name).new
      allUser.id = @users.map{|u| u.id}.join(",")
      allUser.name = "all"
      @users.insert(0, allUser)
    end
    @users
  end

  private
  def to_pinyin(s)
    Pinyin.t(s).downcase
  end

  def find_at_users(type, id)
    @logger.info("#{type}, #{id}")
    case type
      when "Issue"
        find_issue(id)
      when 'Project'
        find_project(id)
      when 'Course'
        find_course(id)
      when 'Activity', 'CourseActivity', 'ForgeActivity','UserActivity', 'OrgActivity','PrincipalActivity'
        find_activity(id, type)
      when 'Attachment'
        find_attachment(id)
      when 'Message'
        find_message(id)
      when 'HomeworkCommon'
        find_homework(id)
      when 'Topic'
        find_topic(id)
      when 'JournalsForMessage'
        find_journals_for_message(id)
      when 'Principal'
        find_principal(id)
      when 'All'
        nil
      else
        nil
    end
  end

  def find_topic(id)

  end

  def find_issue(id)
    #1. issues list persons
    #2. project persons
    issue = Issue.find(id)
    journals = issue.journals
    at_persons = journals.map(&:user) + issue.project.users
    at_persons.uniq { |u| u.id }.delete_if { |u| u.id == User.current.id }
  end

  def find_project(id)
    return [] if id.to_i<0
    at_persons = Project.find(id).users
    at_persons.delete_if { |u| u.id == User.current.id }
  end

  def find_course(id)
    at_persons = Course.find(id).users
    at_persons.delete_if { |u| u.id == User.current.id }
  end

  def find_activity(id, type)

    ## 基本上是本类型中的 加上所属类型的用户
    case type
      when 'Activity'
        activity = Activity.find(id)
        (find_at_users(activity.act_type, activity.act_id) ||[]) +
            (find_at_users(activity.activity_container_type, activity.activity_container_id) || [])
      when 'CourseActivity'
        activity = CourseActivity.find(id)
        (find_at_users(activity.course_act_type, activity.course_act_id) || []) + (find_course(activity.course.id) || [])
      when 'ForgeActivity'
        activity = ForgeActivity.find(id)
        (find_at_users(activity.forge_act_type, activity.forge_act_id) ||[]) +
            (find_project(activity.project_id) || [])
      when 'UserActivity'
        activity = UserActivity.find(id)
        (find_at_users(activity.act_type, activity.act_id) || []) +
            (find_at_users(activity.container_type, activity.container_id) || [])
      when 'OrgActivity'
        activity = OrgActivity.find(id)
        (find_at_users(activity.org_act_type, activity.org_act_id) || []) +
            (find_at_users(activity.container_type, activity.container_id) || [])
      when 'PrincipalActivity'
        activity = PrincipalActivity.find(id)
        find_at_users(activity.principal_act_type, activity.principal_act_id)
      else
        nil
    end
  end

  #作业应该是关联课程,取课程的用户列表
  def find_homework(id)
    homework = HomeworkCommon.find(id)
    find_course(homework.course_id)
  end

  def find_attachment(id)
    attachment = Attachment.find(id)
    find_at_users(attachment.container_type, attachment.container_id)
  end

  #Message
  def find_message(id)
    message = Message.find(id)
    at_persons =  message.board.messages.map(&:author)
    (at_persons || []) + (find_project(message.board.project_id)||[])
  end

  #News
  def find_news(id)
    find_project(News.find(id).project_id)
  end

  #JournalsForMessage
  def find_journals_for_message(id)
    jounrnal = JournalsForMessage.find(id)
    if jounrnal.jour_type == 'Principal'
      [jounrnal.user]  + (JournalsForMessage.where(m_reply_id: id).map(&:user) || [])
    else
      find_at_users(jounrnal.jour_type, jounrnal.jour_id)
    end
  end

  #Poll
  def find_poll(id)
  end

  #Journal
  def find_journal(id)
  end

  #Document
  def find_document(id)
    find_project(Document.find(id).project_id)
  end

  #ProjectCreateInfo
  def find_project_create_info(id)

  end

  #Principal
  def find_principal(id)
  end

  #BlogComment
  def find_blog_comment(id)
    blog = BlogComment.find(id).blog
    blog.users
  end

end