class Panel::Gallery::BackEnd::AlbumsController < OrbitBackendController
include AdminHelper
	
	# before_filter :force_order_for_visitor,:only=>[:index]
  before_filter :force_order_for_user#,:except => [:index]

	def index
		if is_manager? || is_admin? || is_sub_manager?
			@authenticated = true  #turn this value to false for view only
		else
			@authenticated = false
		end
		@categorylist = GalleryCategory.all
		@cid = params['cat']
		@tags = GalleryTag.all
	end

	def show
		if is_manager? || is_admin? || is_sub_manager?
			@authenticated = true  #turn this value to false for view only
		else
			@authenticated = false
		end
		@album = GalleryAlbum.find(params[:id])
		@tags = GalleryTag.all
	end

	def new
		if is_manager? || is_admin?
			@categorylist = GalleryCategory.all
		elsif is_sub_manager?
			@categorylist = GalleryCategory.authed_for_user(current_user,"new_album")
		end
		@album = GalleryAlbum.new
		render :layout => false
	end

	def create
		album = GalleryAlbum.new(params[:gallery_album])
		album.save!
		respond_to do |h|
			h.js
		end
	end

	def get_albums
		@categoryids = params["cid"]
		@tags = params["tid"]
		@albums = Array.new
		if @categoryids == "all"
			if @tags
				if @tags.kind_of?(Array)
					@tags.each do |tag|
						@albums << GalleryAlbum.where(tag_ids: tag)
					end
				else
					@albums <<  GalleryAlbum.where(tag_ids: @tags)
				end
			else
				@albums << GalleryAlbum.all
			end
		else
			@categoryids.each do |id|
				category = GalleryCategory.find(id)
				if @tags
					if @tags.kind_of?(Array)
						@tags.each do |tag|
							@albums << category.gallery_albums.where(tag_ids: tag)
						end
					else
						@albums << category.gallery_albums.where(tag_ids: @tags)
					end
				else
					@albums << category.gallery_albums.all
				end
			end
		end
		@output =  Array.new
		@albums.each do |album|
			@albs = Array.new
			album.each do |values|
				tags = Array.new
				values.tag_ids.each do |tag|
					tags << GalleryTag.find(tag)[I18n.locale]
				end
				category = GalleryCategory.find(values.gallery_category_id).name
				@albs << {"_id"=>values.id,"show_path"=>panel_gallery_back_end_album_path(values),"cover"=>values.cover,"cover_path"=>values.cover_path,"description"=>values.description,"category_name"=>category,"gallery_category_id" => values.gallery_category_id,"name"=>values.name,"tag_ids"=>values.tag_ids,"tag_names"=>tags}
			end
			@output << @albs
		end
		render :json=>@output.to_json
	end

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

	def edit
		if is_manager? || is_admin? || is_sub_manager?
			@album = GalleryAlbum.find(params[:id])
			@images = @album.gallery_images.all
			@album_name = @album.name_translations
			@cover = @album.cover
			@tags = GalleryTag.all
		end
	end

	def set_cover
		album = GalleryAlbum.find(params[:album_id])
		image = GalleryImage.find(params[:imageid])
		album.update_attributes({:cover_path => image.file.thumb.url, :cover=>params[:imageid]})
		render :json =>{"success"=>true}.to_json
	end

	def imgs
		@album = GalleryAlbum.find(params[:album_id])
		@tag_names = Array.new
		@images = @album.gallery_images.all
		@output =  Array.new
		@images.each do |values|
			tags = Array.new
			values.tag_ids.each do |tag|
				tags << GalleryTag.find(tag)[I18n.locale]
			end
			@output << {"_id"=>values.id,"theater_link"=>panel_gallery_back_end_album_image_path(values),"description"=>values.description,"title"=>values.title,"file"=>values.file,"gallery_album_id"=>values.gallery_album_id,"tag_ids"=>values.tag_ids,"tag_names"=>tags}
		end
		if is_manager? || is_admin? || is_sub_manager?
			@authenticated = true  #turn this value to false for view only
		else
			@authenticated = false
		end
		if @authenticated
			render :json=>{"images" => @output, "tags" => @album.tag_ids}.to_json
		else
			@album.tag_ids.each do |tag|
				@tag_names << GalleryTag.find(tag)[I18n.locale]
			end
			render :json=>{"images" => @output, "tags" => @album.tag_ids, "tag_names" => @tag_names}.to_json
		end
	end
	def upload_panel
		@album = GalleryAlbum.find(params[:album_id])
		render :layout => false
	end
	def upload_image
		@album = GalleryAlbum.find(params[:album_id])
		@files = params['files']
		a = Array.new
		@files.each do |file|
			@image = @album.gallery_images.new
			@image.file = file
			@image.save!
			a << {"thumbnail_url"=>@image.file.thumb.url}
		end
		render :json=>a.to_json
	end
	def images_tags
		album = GalleryAlbum.find(params[:album_id])
		tags = Array.new
		images = album.gallery_images.all
		images.each do |image|
			tags << {"id"=>image.id, "tags" => image.tag_ids}
		end
		render :json=>tags.to_json
	end

	def update
		@album = GalleryAlbum.find(params[:id])
		@album.update_attributes(params[:gallery_album])
		if is_manager? || is_admin? || is_sub_manager?
			@authenticated = true  #turn this value to false for view only
		else
			@authenticated = false
		end
		if params['delete_cover'] == "true"
			@album.update_attributes(:cover=>"default")
		end
		@tags = GalleryTag.all
		render :action => :show
	end

end






