class Panel::Calendar::BackEnd::CalsController < OrbitBackendController
	include AdminHelper
	include Panel::Calendar::BackEnd::CalsHelper
	before_filter :force_order_for_user
	def index
		@calendars = Cal.all
	end

	def new
		@calendar = Cal.new
		@calendars = Cal.all
	end

	def edit
		@calendar = Cal.find(params[:id])
		render :layout=>false
	end

	def update
		@calendar = Cal.find(params[:id])
		@calendar.update_attributes(params[:cal])
		respond_to do |h|
			h.js
		end
	end

	def create
		@calendar = Cal.new(params[:cal])
		@calendar.save!
		respond_to do |h|
			h.js
		end
	end

	def destroy
		calendar = Cal.find(params[:id])
		calendar.delete
		render :json => {"success"=>"true"}.to_json
	end

	def day_view
		@date = params[:date].to_i
		month = params[:month].to_i
		year = params[:year].to_i
		@cur_day = getDayName(@date,month,year) + " " + month.to_s + "/" + @date.to_s + " - " + year.to_s
		@hours = getHours(12)
		render :layout => false	
	end

	def week_view
		week = params[:week].to_i
		year = params[:year].to_i
		@dates = week_dates(week,year)
		@range = week_range(week,year)
		@d = getWeekDataSet(week,year)
		t = Time.now
		dt = Date.new(t.year,t.month,t.day)
		today_cur_week = dt.strftime("%U").to_i

		if week == today_cur_week && t.year == year
			@today = @d.index(t.day.to_s)
		else
			@today = 20
		end
		@hours = getHours(12)
		render :layout => false
	end

	def month_view
		month = params[:month].to_i
		year = params[:year].to_i
		t = Time.now
		startday = monthStartDay(month,year)
		@pre_disabled_days = startday - 1
		if t.month == month && t.year == year
			@today = @pre_disabled_days + t.day
		else
			@today = 50
		end
		cur_month_days = getMonthDays(year)
		@post_disabled_days = @pre_disabled_days + cur_month_days[month]
		@dateset = getDateSet(month,year,true)
		render :layout => false
	end

	def agenda_view
		@start_year = params[:s_year].to_i
		@start_month = params[:s_month].to_i
		@end_year = params[:e_year].to_i
		@end_month = params[:e_month].to_i
		t = Time.now
		if !params[:s_year]
			@start_year = t.year
		end
		if !params[:s_month]
			@start_month = t.month
			@end_month = t.month + 3
			if @end_month > 12
				@end_month = @end_month - 12
				@end_year = @start_year + 1
			end
		end
		
		startdt = Date.new(@start_year,@start_month,1)
		temp = getMonthDays(@end_year)
  		enddt = Date.new(@end_year,@end_month,temp[@end_month])
  		diff = enddt - startdt
		diff = diff.to_i / 30

		
		@d_s_year = @start_year - 5
		@datesets = Array.new
		@calendartitle = Array.new
		@events = Array.new
		events = Event.all.asc(:start_date).desc(:total_days)
		@calevents = Array.new
		y = @start_year
		m = @start_month
		for i in 0..diff-1
			if m == 13
				m = 1
				y = y + 1
			end
			@calendartitle << [m,y]
			@datesets << getDateSet(m,y,false)
			e = Array.new
			h = Array.new
			events.each_with_index do |event,i|
			# @temp = Array.new
			startdt = Date.new(event.start_year,event.start_month)
  			enddt = Date.new(event.end_year,event.end_month)
  			range = startdt..enddt
				dt = Date.new(y,m)
	  			if range === dt
					no_of_days = event.total_days
					start_date = event.start_date

					if event.start_year < y
						no_of_days = temp[m] - event.start_date
						no_of_days += 1
						temp = m + 12
						if event.start_month < temp
							start_date = 1
						end
					end
					if event.end_year > y
						no_of_days = temp[m] - event.start_date
						no_of_days += 1
						temp = m + 12
					end
					if event.end_month > m
						no_of_days = temp[m] - event.start_date
						no_of_days += 1
					elsif event.end_month == m
						no_of_days = event.end_date
					end

					if event.start_month == m && event.end_month == m
						no_of_days = event.total_days
						no_of_days += 1
					end

					if event.start_month < m
						start_date = 1
					end

					if event.start_date == event.end_date
						display_date = getDayName(event.start_date,event.start_month,event.start_year) + ", " + Date::ABBR_MONTHNAMES[event.start_month] + " " + event.start_date.to_s
					else
						display_date = getDayName(event.start_date,event.start_month,event.start_year) + ", " + Date::ABBR_MONTHNAMES[event.start_month].to_s + " " + event.start_date.to_s + " - " + getDayName(event.end_date,event.end_month,event.end_year) + ", " + Date::ABBR_MONTHNAMES[event.end_month] + " " + event.end_date.to_s
					end
					if event.all_day 
						display_time = "All Day"
					else
						x = event.start_time.to_i
						if event.start_time > x
							est = x.to_s + ":30"
						else
							est = x.to_s
						end
						x = event.end_time.to_i
						if event.end_time > x
							eet = x.to_s + ":30"
						else
							eet = x.to_s 
						end
						display_time = est + " " + event.start_am_pm + " - " + eet + " " + event.end_am_pm
					end
					 for c in start_date..(start_date + no_of_days)
					 	if h.index(c) == nil
					 		h << c
					 	end 
					 end
					color = Cal.find(event.cal_id).color
					e << {"display_date"=>display_date,"show_link"=>panel_calendar_back_end_event_path(event), "display_time"=>display_time, "start_date"=>start_date,"end_date"=>event.end_date,"start_month"=>event.start_month,"end_month"=>event.end_month,"title" => event.title,"color"=>color}
				end			
			end
			@events << e
			@calevents << h
			m = m + 1
		end
		render :layout => false
	end

	def get_month_events
		month = params[:month].to_i
		year = params[:year].to_i
		fromcalendars = params[:calendars]
		
		# events = Event.where(:start_month.lt => month).and(:start_year => year).and(:end_month.gte => month).asc(:start_date).desc(:total_days)
		@events = Array.new
		no_of_days_in_month = getMonthDays(year)
		# events.each_with_index do |event,i|
		# 	no_of_days = event.total_days

		# 	if event.end_month > month
		# 		no_of_days = no_of_days_in_month[month]
		# 	elsif event.end_month == month
		# 		no_of_days = event.end_date
		# 	end
				
		# 	no_of_days = no_of_days.to_i
			
		# 	color = Cal.find(event.cal_id).color
		# 	@events << {"id"=>event.id,"index"=>i,"start_date"=>"1", "total_days" => no_of_days, "title" =>	 event.title,"color"=>color,"show_link"=>panel_calendar_back_end_event_path(event)}
		# end
		# events = Event.where(:start_month => month).and(:start_year => year).asc(:start_date).desc(:total_days)
		# events.each_with_index do |event,i|
		# 	# @temp = Array.new
		# 	no_of_days = event.total_days
		# 	if event.end_month > month
		# 		no_of_days = no_of_days_in_month[month] - event.start_date
		# 	end
		# 	no_of_days = no_of_days.to_i
		# 	no_of_days += 1

		# 	color = Cal.find(event.cal_id).color
		# 	@events << {"id"=>event.id,"index"=>i,"start_date"=>event.start_date, "total_days" => no_of_days, "title" => event.title,"color"=>color,"show_link"=>panel_calendar_back_end_event_path(event)}
		# end
		events = Event.where(:cal_id.in=>fromcalendars).asc(:start_date).desc(:total_days)
		events.each_with_index do |event,i|
			# @temp = Array.new
			startdt = Date.new(event.start_year,event.start_month)
  			enddt = Date.new(event.end_year,event.end_month)
  			range = startdt..enddt
  
				dt = Date.new(year,month)
	  			if range === dt
					no_of_days = event.total_days
					start_date = event.start_date

					if event.start_year < year
						no_of_days = no_of_days_in_month[month] - event.start_date
						no_of_days += 1
						temp = month + 12
						if event.start_month < temp
							start_date = 1
						end
					end
					if event.end_year > year
						no_of_days = no_of_days_in_month[month] - event.start_date
						no_of_days += 1
						temp = month + 12
					end
					if event.end_month > month
						no_of_days = no_of_days_in_month[month] - event.start_date
						no_of_days += 1
					elsif event.end_month == month
						no_of_days = event.end_date
					end

					if event.start_month == month && event.end_month == month
						no_of_days = event.total_days
						no_of_days += 1
					end

					if event.start_month < month
						start_date = 1
					end

					color = Cal.find(event.cal_id).color
					@events << {"start_year"=>event.start_year, "id"=>event.id,"index"=>i,"start_date"=>start_date, "total_days" => no_of_days, "title" => event.title,"color"=>color,"show_link"=>panel_calendar_back_end_event_path(event)}
				end
			
		end
		
		render :json => @events.to_json
	end
	
	def get_week_events
		week = params[:week].to_i
		year = params[:year].to_i
		fromcalendars = params[:calendars]
		@d = getWeekDataSet(week,year)
		events = Event.where(:cal_id.in=>fromcalendars).and(:start_week.lte => week).and(:start_year => year).and(:end_week.gte => week).and(:start_year => year).asc(:start_week).asc(:start_date)
		@events = Array.new
		events.each_with_index do |event,i|
			# @temp = Array.new
			days = event.total_days.to_i + 1
			colcount = 0
			startdt = event.start_date

			all_day = event.all_day

			if days > 1
				all_day = true
			end

			if event.end_week > week && event.start_week < week
				days = 7
			end

			if event.end_week == week
				days = @d.index(event.end_date.to_s).to_i
			end

			if event.start_week == week
				days = 7 - @d.index(event.start_date.to_s).to_i
			end

			
			if event.start_week == week && event.end_week == week
				x = @d.index(event.end_date.to_s).to_i
				y = @d.index(event.start_date.to_s).to_i
				colcount = x - y
				colcount+=1
				
			end

			if event.start_week < week 
				startdt = @d[0].to_i
				event.all_day = true
				if event.end_week == week
					colcount = 7 - @d.index(event.end_date.to_s).to_i
					days = 7;
					startdt = event.start_date
				end
			end

			color = Cal.find(event.cal_id).color
			@events << {"id"=>event.id,"index"=>i,"start_date"=>startdt,"end_date"=>event.end_date ,"all_day"=>all_day, "start_week" => event.start_week, "end_week" => event.end_week, "total_days" => days, "title" => event.title,"color"=>color,"show_link"=>panel_calendar_back_end_event_path(event),"start_time"=>event.start_time,"end_time"=>event.end_time,"start_am_pm"=>event.start_am_pm,"end_am_pm"=>event.end_am_pm,"colcount"=>colcount}
		end
		render :json => @events.to_json
	end

	def get_day_events
		day = params[:date].to_i
		month = params[:month].to_i
		year = params[:year].to_i
		fromcalendars = params[:calendars]
		dt =  Date.new(year,month,day)
		week = dt.strftime("%U")
		events = Event.where(:start_month.lte => month).and(:start_year => year).and(:end_month.gte => month)
		@events = Array.new
		events.each_with_index do |event,i|
			days = event.total_days.to_i + 1
			colcount = 0
			
			all_day = event.all_day

			if days > 1
				all_day = true
			end
			startdt = Date.new(event.start_year,event.start_month,event.start_date)
  			enddt = Date.new(event.end_year,event.end_month,event.end_date)

  			range = startdt..enddt

  			if range === dt
				color = Cal.find(event.cal_id).color
				@events << {"id"=>event.id,"index"=>i,"start_date"=>event.start_date,"end_date"=>event.end_date ,"all_day"=>all_day, "start_week" => event.start_week, "end_week" => event.end_week, "total_days" => days, "title" => event.title,"color"=>color,"show_link"=>panel_calendar_back_end_event_path(event),"start_time"=>event.start_time,"end_time"=>event.end_time,"start_am_pm"=>event.start_am_pm,"end_am_pm"=>event.end_am_pm}
			end
		end
		render :json => @events.to_json
	end

	def week_number_test
		events = Event.all

		events.each do |event|
			dt = Date.new(event.start_year,event.start_month,event.start_date)
			sweeknumber = dt.strftime("%U")
			dt = Date.new(event.end_year,event.end_month,event.end_date)
			eweeknumber = dt.strftime("%U")
			event.start_week = sweeknumber
			event.end_week = eweeknumber
			event.save!
		end

		dt = Date.new(2012,10,4)
		@week = dt.strftime("%U")
		render :text => @week.to_s
	end
end



