# encoding: utf-8

class Bulletin
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::MultiParameterAttributes
  include Impressionist::Impressionable

  is_impressionable :counter_cache => { :column_name => :view_count }

  has_one :title, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy
  has_one :subtitle, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy
  has_one :text, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy
  has_and_belongs_to_many :tags, :class_name => "AnnouncementTag"

  field :postdate , :type => DateTime
  field :deadline , :type => DateTime
  # field :url
  field :create_user_id
  field :update_user_id, :class_name => "User"
  
  field :is_top, :type => Boolean, :default => false
  field :is_hot, :type => Boolean, :default => false
  field :is_hidden, :type => Boolean, :default => false
  field :is_checked, :type => Boolean, :default => false
  field :is_pending, :type => Boolean, :default => true 
  field :is_rejected, :type => Boolean, :default => false 

  field :view_count, :type => Integer, :default => 0
  
  field :not_checked_reason
  
  field :public, :type => Boolean, :default => true
  
  scope :can_display,where(is_checked: true)
  
  mount_uploader :image, ImageUploader
  
  belongs_to :bulletin_category
#  belongs_to :unit_list_for_anc
  
  # embeds_many :bulletin_links, :cascade_callbacks => true
  # embeds_many :bulletin_files, :cascade_callbacks => true

  has_many :bulletin_links, :autosave => true, :dependent => :destroy
  has_many :bulletin_files, :autosave => true, :dependent => :destroy
  
  accepts_nested_attributes_for :bulletin_files, :allow_destroy => true
  accepts_nested_attributes_for :bulletin_links, :allow_destroy => true
  
  validates_presence_of :title
  
  before_save :set_key, :update_status,:check_deadline
  
  after_save :save_bulletin_links
  after_save :save_bulletin_files
  

  def self.search( search = nil, category_id = nil )
    if category_id.to_s.size > 0 and search.to_s.size > 0
      key = /#{search}/
      find(:all, :conditions => {title: key, bulletin_category_id: category_id}).desc( :is_top, :postdate )
    elsif category_id.to_s.size > 0 and search.to_s.size < 1
      find(:all, :conditions => {bulletin_category_id: category_id}).desc( :is_top, :postdate )
    elsif search.to_s.size > 0 and category_id.to_s.size < 1
      key = /#{search}/
      find(:all, :conditions => {title: key}).desc( :is_top, :postdate )
    else
      find(:all).desc( :is_top, :postdate)
    end
  end


  def self.widget_datas( category_id = nil )
  
    date_now = Time.now
 
   # find(:all, :conditions => {:postdate => {"$lte" => Date.today}, deadline: nil} ).desc( :is_top, :postdate)
   # where( :postdate.lte => date_now ).where( :deadline => nil ).desc(:is_top, :postdate)
   # any_of({ :title => "test" },{:deadline => nil, :title => "123"})
   
   if category_id.to_s.size > 0
     find(:all, :conditions => {bulletin_category_id: category_id}).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate)
   else
     any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate)
   end
   
  end

  def is_expired?
    Date.today > self.deadline ? true : false  rescue false
    #some dates might sat as nil so rescue false
  end

  def is_top?
    self.is_top
  end
  
  def is_hot?
    self.is_hot
  end
  
  def is_hidden?
    self.is_hidden
  end
  
  def is_checked?
    self.is_checked
  end
  
  def is_pending?
    self.is_pending
  end
  
  def is_rejected?
    self.is_rejected
  end
  
  
  def save_bulletin_links
	self.bulletin_links.each do |t|
	  if t.should_destroy
		t.destroy
	  end
    end
  end
  
  def save_bulletin_files
	self.bulletin_files.each do |t|
	  if t.should_destroy
		t.destroy
	  end
    end
  end
  
  
  def title
    @title ||= I18nVariable.first(:conditions => {:key => 'title', :language_value_id => self.id, :language_value_type => self.class}) rescue nil
  end

  def subtitle
    @subtitle ||= I18nVariable.first(:conditions => {:key => 'subtitle', :language_value_id => self.id, :language_value_type => self.class}) rescue nil
  end
  
  def text
    @text ||= I18nVariable.first(:conditions => {:key => 'text', :language_value_id => self.id, :language_value_type => self.class}) rescue nil
  end

  def self.filter(bulletins)
    bulletins.each do |bulletin|
      p "#{bulletin.id}/#{bulletin.is_top}/#{bulletin.is_hot}/#{bulletin.is_hidden}"
    end
    return nil
  end

  def sorted_tags
    tags.order_by(I18n.locale, :asc)
  end
  
  protected
  
  def check_deadline
    if(!self.deadline.nil? and (self.deadline<= self.postdate ))
      self.deadline = nil   
    end

  end

  def set_key
    if title && title.new_record?
      title.key = 'title'
    end
    if subtitle && subtitle.new_record?
      subtitle.key = 'subtitle'
    end
    if text && text.new_record?
      text.key = 'text'
    end
  end

  def update_status
    if !self.is_pending && !self.is_checked
      self.is_pending = false
      self.is_rejected = true
      self.is_checked = false
    elsif self.is_checked
      self.is_pending = false
      self.is_rejected = false
      self.is_checked = true
    end
  end
  
  
end