#coding=utf-8

class AtMessage < ActiveRecord::Base
  include ApplicationHelper
  belongs_to :user
  belongs_to :sender, class_name: "User", foreign_key: "sender_id"
  attr_accessible :at_message, :container, :viewed, :user_id, :sender_id
  belongs_to :at_message, polymorphic: true
  belongs_to :container, polymorphic: true

  has_many :message_alls, :class_name => 'MessageAll',:as =>:message, :dependent => :destroy
  validates :user_id, :sender_id, :at_message_id, :at_message_type, presence: true

  after_create :add_user_message, :send_wechat_message

  scope :unviewed, ->(type, id){
    where(at_message_type: type, at_message_id:id, viewed: false)
  }

  def viewed!
    update_attribute :viewed, true
  end

  def at_valid?
    return true if at_message_type == 'Issue'
    return true if 'Journal' == at_message_type
    return true if 'JournalsForMessage' == at_message_type
    return true if 'Message' == at_message_type
    false
  end

  def add_user_message
    if MessageAll.where(message_type: self.class.name,message_id: self.id).empty?
      self.message_alls << MessageAll.new(:user_id => self.user_id)
    end
  end

  #@的时候发微信模版消息通知被@的人
  def send_wechat_message
    shield_type = ""
    container_id = 0
    status = 0
    type = ""
    detail_id = 0
    detail_title = ""

    if defined? at_message.notes
      detail_content = strip_html at_message.notes,30
    elsif defined? at_message.content
      detail_content = strip_html at_message.content,30
    elsif defined? at_message.description
      detail_content = strip_html at_message.description,30
    end

    user = self.user

    topic = get_root_parent at_message

    case at_message_type
      when "Issue"
        #新建issue
        shield_type = "Project"
        container_id = at_message.project.id
        type = "issues"
        detail_id = topic.id
        detail_title = at_message.subject
      when "Journal"
        #issue回复
        topic = get_root_parent at_message.journalized
        shield_type = "Project"
        container_id = at_message.journalized.project.id
        type = "issues"
        detail_id = topic.id
        detail_title = at_message.journalized.subject
      when 'Message'
          if at_message.course
            shield_type = "Course"
            container_id = at_message.course.id
            type = "course_discussion"
            detail_id = topic.id
            detail_title = at_message.subject
          elsif at_message.project
            shield_type = "Project"
            container_id = at_message.project.id
            type = "project_discussion"
            detail_id = topic.id
            detail_title = at_message.subject
          else
            status = -1
          end
      when 'JournalsForMessage'
        if at_message.jour && defined? at_message.jour.course
          #作业回复
          shield_type = "Course"
          container_id = at_message.jour.course.id
          type = "homework"
          detail_id = at_message.jour.id
          detail_title = at_message.jour.name
        else
          type = "journal_for_message"
          detail_id = topic.id
          detail_title = topic.notes.nil? ? "" : topic.notes
        end
      else
        status = -1
    end

    count = 0

    detail_title = detail_title.gsub(/RE: /, '')

    if container_id != 0
      count = ShieldWechatMessage.where("container_type='User' and container_id=#{user.id} and shield_type='#{shield_type}' and shield_id=#{container_id}").count
    end
    if count == 0 && status == 0
      message_title = self.sender.show_name+"@了您"
      ws = WechatService.new
      ws.at_notice user.id, type, detail_id, message_title, detail_title, format_time(Time.now), detail_content, "点击查看详情。",0
    end

  end

  def subject
    case at_message_type
      when "Issue"
        "新建问题: " + at_message.subject
      when "Journal"
        "问题留言: " + at_message.journalized.subject
      when 'Message'
        if(at_message.topic?)
          "发布新帖: "
        else
          "回复帖子: "
        end +  at_message.subject
      when 'JournalsForMessage'
        if at_message.jour_type == 'Principal'
          "留言: 在#{at_message.at_user.show_name}主页中留言"
        else
          "作业: #{at_message.jour.name} 中留言"
        end
      else
        logger.error "error type: #{at_message_type}"
    end
  end

  def description
    case at_message_type
      when "Issue"
        at_message.description
      when "Journal"
        at_message.notes
      when 'Message'
        at_message.content
      when "JournalsForMessage"
        at_message.notes
      else
        logger.error "error type: #{at_message_type}"
    end
  end

  def author
    case at_message_type
      when "Issue"
        at_message.author
      when "Journal"
        at_message.user
      when 'Message'
        at_message.author
      when 'JournalsForMessage'
        at_message.user
      else
        logger.error "error type: #{at_message_type}"
    end
  end

  def url
    case at_message_type
      when "Issue"
        {controller: :issues, action: :show, id: at_message}
      when "Journal"
        {controller: :issues, action: :show, id: at_message.journalized}
      when 'Message'
        {controller: :boards, action: :show, project_id: at_message.board.project, id: at_message.board}
      when 'JournalsForMessage'
        if at_message.jour_type == 'Principal'
          {controller: :users, action: :user_messages, id: at_message.at_user}
        elsif at_message.jour_type == 'HomeworkCommon'
          {controller: :homework_common, action: :index, course: at_message.jour.course_id}
        elsif at_message.jour_type == 'Course'
          {controller: :courses, action: :feedback, id: at_message.jour_id}
        end

      else
        logger.error "error type: #{at_message_type}"
    end

  end

end
