require 'time'
require 'date'

class Event
  include Mongoid::Document
  include Mongoid::Timestamps
  include OrbitCategory::Categorizable
  include Mongoid::MultiParameterAttributes

  field :title
  field :note
  field :start, type: DateTime
  field :end, type: DateTime
  field :all_day, type: Boolean
  field :recurring, type: Boolean
  field :frequency
  field :period

  attr_accessor :agenda_start, :agenda_end, :get_agenda_events


  validates_presence_of :title, :message => "Please fill the title of the Event"
  
  def self.format_date(date_time)
    Time.at(date_time.to_i).to_formatted_s(:db)
  end

  REPEATS = [
              "Daily" ,
              "Weekly" ,
              "Monthly" ,
              "Yearly"
  ]

    def as_json(options = {})
    {
      :id => self.id,
      :title => self.title,
      :note => self.note || "",
      :start => self.start.rfc822,
      :end => self.end.rfc822,
      :allDay => self.all_day,
      :recurring => self.recurring,
      :calendar => self.category_id,
      :color => Category.find(self.category_id).custom_value,
      :edit_url => Rails.application.routes.url_helpers.edit_panel_calendar_new_back_end_event_path(self.id),
      :delete_url =>  Rails.application.routes.url_helpers.panel_calendar_new_back_end_event_path(self.id)
    }

  end

  def self.monthly_event(start_date,end_date)
    self.any_of(:start.gte => start_date, :end.gte => start_date).and(:start.lte => end_date).asc(:start)
  end

  def self.recurring_event(start_date,end_date)  
    @recurring_events = self.where(:recurring => true)
    @recurring = []
    @recurring_events.each do |re|
     case re.period
      when 'Daily'
        if (start_date..end_date).cover?(re.start)
           @i = TimeDifference.between(re.start,end_date).in_days.to_i
           (1..@i).each do |i|
            @start_date = re.start + i
            @recurring << {:id => re.id, :title=>re.title, :note=>re.note, :start=>@start_date, :end => @start_date, :allDay => re.all_day, :recurring => re.recurring, :calendar => re.category_id.to_s, :color => Category.find(re.category_id).custom_value, :edit_url => Rails.application.routes.url_helpers.edit_panel_calendar_new_back_end_event_path(re.id), :delete_url => Rails.application.routes.url_helpers.panel_calendar_new_back_end_event_path(re.id)}
          end
        elsif re.start < start_date
           @i = TimeDifference.between(start_date,end_date).in_days.to_i
           (0..@i-1).each do |i|
              @start_date = start_date.to_date + i
              @recurring << {:id => re.id, :title=>re.title, :note=>re.note, :start=>@start_date, :end => @start_date, :allDay => re.all_day, :recurring => re.recurring, :calendar => re.category_id.to_s, :color => Category.find(re.category_id).custom_value, :edit_url => Rails.application.routes.url_helpers.edit_panel_calendar_new_back_end_event_path(re.id), :delete_url => Rails.application.routes.url_helpers.panel_calendar_new_back_end_event_path(re.id)}
            end
        end
      when "Weekly"
          @start_date = re.start
          @end_date = re.end
          @i = TimeDifference.between(re.start,end_date).in_weeks.to_i
           (1..@i).each do |i|
            @start_date += (7*re.frequency.to_i)
            @end_date += (7*re.frequency.to_i)
           
            @recurring << {:id => re.id, :title=>re.title, :note=>re.note, :start=>@start_date, :end => @end_date, :allDay => re.all_day, :recurring => re.recurring, :calendar => re.category_id.to_s, :color => Category.find(re.category_id).custom_value, :edit_url => Rails.application.routes.url_helpers.edit_panel_calendar_new_back_end_event_path(re.id), :delete_url => Rails.application.routes.url_helpers.panel_calendar_new_back_end_event_path(re.id)}
          end
      when "Monthly"
        if !(start_date..end_date).cover?(re.start)
          sd = re.start
          ed = re.end
          @i = TimeDifference.between(re.start,end_date).in_months.to_i
           @start_date = sd
           sd = sd >> @i*re.frequency.to_i
           ed = ed >> @i*re.frequency.to_i
             @recurring << {:id => re.id, :title=>re.title, :note=>re.note, :start=>sd, :end => ed, :allDay => re.all_day, :recurring => re.recurring, :calendar => re.category_id.to_s, :color => Category.find(re.category_id).custom_value, :edit_url => Rails.application.routes.url_helpers.edit_panel_calendar_new_back_end_event_path(re.id), :delete_url => Rails.application.routes.url_helpers.panel_calendar_new_back_end_event_path(re.id)}
          end
      when "Yearly"
        if !(start_date..end_date).cover?(re.start)
         sd = re.start
         ed = re.end
          @i = TimeDifference.between(re.start,end_date).in_years.to_i
           @start_date = sd
           sd = sd >> 12 * @i*re.frequency.to_i
           ed = ed >> 12 * @i*re.frequency.to_i
            @recurring << {:id => re.id, :title=>re.title, :note=>re.note, :start=>sd, :end => ed, :allDay => re.all_day, :recurring => re.recurring, :calendar => re.category_id.to_s, :color => Category.find(re.category_id).custom_value, :edit_url => Rails.application.routes.url_helpers.edit_panel_calendar_new_back_end_event_path(re.id), :delete_url => Rails.application.routes.url_helpers.panel_calendar_new_back_end_event_path(re.id)}
        end
    end
   end
   
   @recurring
  end

  def self.agenda_events(agenda_start, agenda_end)
    @all_events = self.any_of(:start.gte => agenda_start, :end.gte => agenda_start).and(:start.lte => agenda_end).asc(:start)
  end
end