class NoticeComplaint < ActiveRecord::Base
  attr_accessible :notice_type, :notice_at, :received, :contact_phone, :contact_address, :content, :contact_name,
                  :contact_email, :contact_twitter, :contact_facebook, :anonymous, :status, :institution_ids,
                  :solution_comment, :alternate_officer

  # Non database attributtes
  attr_accessible :attachment
  
  # Paperclip
  has_attached_file :attachment
  
  # associations
  belongs_to :user
  has_and_belongs_to_many :institutions
  has_many :notice_complaint_records, dependent: :destroy
  accepts_nested_attributes_for :notice_complaint_records
     
  # validations
  validates :notice_at, :notice_type, :received, :anonymous, :content, presence: true
 
  # callbacks
  before_create :set_correlative


  # enumerated attributes
  enumerated_attribute :received, %w(system135 phone email attendance twitter facebook) do
    label system135: I18n.t('enum.notice_complaint.received.system135')
    label phone: I18n.t('enum.notice_complaint.received.phone')
    label email: I18n.t('enum.notice_complaint.received.email')
    label attendance: I18n.t('enum.notice_complaint.received.attendance')
    label twitter: I18n.t('enum.notice_complaint.received.twitter')
    label facebook: I18n.t('enum.notice_complaint.received.facebook')
  end

  enumerated_attribute :notice_type, %w(denouncement warning request complaint comment question) do
    label denouncement: I18n.t('enum.notice_complaint.notice_type.denouncement')
    label warning: I18n.t('enum.notice_complaint.notice_type.warning')
    label request: I18n.t('enum.notice_complaint.notice_type.request')
    label complaint: I18n.t('enum.notice_complaint.notice_type.complaint')
    label comment: I18n.t('enum.notice_complaint.notice_type.comment')
    label question: I18n.t('enum.notice_complaint.notice_type.question')
  end

  enumerated_attribute :status, %w(newest process finished) do
    label newest: I18n.t('enum.notice_complaint.status.newest')
    label process: I18n.t('enum.notice_complaint.status.process')
    label finished: I18n.t('enum.notice_complaint.status.finished')
  end

  enumerated_attribute :anonymous, %w(yes no) do
    label yes: I18n.t('enum.ofcia_complaint.anonymous.affirmative')
    label no: I18n.t('enum.ofcia_complaint.anonymous.negative')
  end

  # delegates
  delegate :name, :email, to: :user, allow_nil: true, prefix: true

  #def remaining_days
    #@remaining_days ||= correct? ? ( events.last.try(:event_start_with_business_days_added, CORRECT_BUSINESS_DAYS) - Date.current ).to_i : (delivery_deadline - Date.current).to_i rescue 0
  #end

  scope :newest, -> { where(status: :newest) }
  scope :process, -> { where(status: :process) }
  scope :finished, -> { where(status: :finished) }

  scope :denouncement, -> { where(notice_type: :denouncement) }
  scope :warning, -> { where(notice_type: :warning) }
  scope :request, -> { where(notice_type: :request) }
  scope :complaint, -> { where(notice_type: :complaint) }
  scope :comment, -> { where(notice_type: :comment) }

  def name
    self.content.truncate(50, omission: '...')
  end

  def check_status
    process_date = self.created_at
    business_days = CalendarEvent.select('kind,start_at').joins(:institution_calendar).where('institution_calendars.institution_id = ? AND calendar_events.start_at BETWEEN ? AND ?', self.institution_ids.first, process_date.beginning_of_day, Date.current.end_of_day).group_by{|o| o.start_at.to_date}
    cursor_day = process_date
    @counted_days = 0
    while cursor_day <= Date.current
      if business_days.has_key?(cursor_day)
        # rest a day unless is a holiday
        @counted_days += 1 unless business_days[cursor_day].map(&:kind).include?(:holiday)
      else
        # rest a day unless is weekened
        @counted_days += 1 unless cursor_day.saturday? or cursor_day.sunday?
      end
      cursor_day = cursor_day + 1.day
    end
    @counted_days
  end

  def update_status
    if self.check_status > 3 and self.status.to_s == "process"
      self.status = "delayed"
      self.save
      NoticeComplaintRecord.create!(notice_complaint_id: self.id, status: self.status)
    end
  end

  def label_class(object)
    @label_class ||= object.status.to_s == "newest" ? 'label-info' : (object.status.to_s == "process" ? 'label-success' : '')
  end

  private
    def set_correlative
      self.correlative = "SGC-#{Date.current.strftime('%Y%m%d')}-#{'%03i' % (NoticeComplaint.count + 1)}"
    end

    def self.set_status(object)
      if object.status.nil? && object.solution_comment.nil?
        object.status = "process"
        object.save
        NoticeComplaintRecord.create!(notice_complaint_id: object.id, status: object.status)
      elsif !object.solution_comment.blank?
        object.status = "finished"
        object.save
        NoticeComplaintRecord.create!(notice_complaint_id: object.id, status: object.status)
      end
    end

end
