class Panel::Gallery::FrontEnd::OrbitGalleriesController < OrbitWidgetController
include AdminHelper
	def index
		@authenticated = false
		@categorylist = GalleryCategory.all
		@cid = params['cat']
		@tags = get_tags
	end

	def new_category
		names = params['category']
		category = GalleryCategory.new
		I18n.locale = :en
		category.name = names['en']
		I18n.locale = :zh_tw
		category.name = names['zh_tw']
		category.save!
    	render :json=>{"success"=>true,"id"=>category.id}.to_json
	end

	def categorylist
		@categorylist = GalleryCategory.all
		#render :layout => false
	end

	def gallery_category_delete
		@category = GalleryCategory.find(params['id'])
		@category.delete
		render :json=>{"success"=>"true"}.to_json
	end

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

	def create_album
		category = GalleryCategory.find(params['cid'])
		albumnames = params["albumname"]
		albumdescs = params["albumdesc"]
		album = category.gallery_albums.new
		I18n.locale = :en
		album.name = albumnames["en"]
		album.description = albumdescs["en"]
		I18n.locale = :zh_tw
		album.name = albumnames["zh_tw"]
		album.description = albumdescs["zh_tw"]
		album.save!
		#x = category.gallery_albums.create({name: params['albumname'],description: params['albumdesc']})
		render :json=>{"success"=>true,"id"=>album.id}.to_json
	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(tagged_ids: tag)
					end
				else
					@albums <<  GalleryAlbum.where(tagged_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(tagged_ids: tag)
						end
					else
						@albums << category.gallery_albums.where(tagged_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 = Tag.find(values.tagged_ids).map{|t| t.name}
				category = GalleryCategory.find(values.gallery_category_id).name
				@albs << {"_id"=>values.id,"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 upload_image
		albumid = params['aid'];
		@album = GalleryAlbum.find(albumid)
		@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 upload_panel
		render :layout => false
	end

	def get_images
		@album = GalleryAlbum.find(params["aid"])
		render :json=>{"images" => @album.gallery_images.all, "tags" => @album.tag_ids}.to_json
	end

	def theater
		picid = params["pic"]
		@image = GalleryImage.find(picid)
		@albumid = @image.gallery_album_id
		album = GalleryAlbum.find(@albumid)
		@images = album.gallery_images.all
		render :layout=>false
	end

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

	def edit_album
		if is_manager? || is_admin? || is_sub_manager?
			aid = params['aid']
			album = GalleryAlbum.find(aid)
			@images = album.gallery_images.all
			@album_name = album.name_translations
			@cover = album.cover
			render :layout => false
		end
	end

	def set_cover
		aid = params['aid']
		album = GalleryAlbum.find(aid)
		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 delete_images
		images = params['images']
		images.each do |image|
			img = GalleryImage.find(image)
			img.delete
		end
		if params['delete_cover'] == "true"
			album = GalleryAlbum.find(params['aid'])
			album.update_attributes(:cover=>"default")
		end
		render :json =>{"success"=>true}.to_json
	end

	def update_album
		# data = params['data']
		# album_name = params['name']
		# aid = params['aid']
		# data.each do |d|
		# 	image = GalleryImage.find(d[1][:id])
		# 	image.update_attributes(:description=>d[1][:text])
		# end
		# album = GalleryAlbum.find(aid)
		# album.update_attributes(:name => album_name)
		album_names = params[:albumnm]
		data = params[:data]
		aid =params[:aid]
		debugger
		data.each do |d|
			image = GalleryImage.find(d[1][:id])
			@site_valid_locales.each do |locale|
				image.description_translations[locale] = d[1][:text][locale] 
			end
			image.save!
		end

		album = GalleryAlbum.find(aid)
		
		@site_valid_locales.each_with_index do |locale,i|
			album.name_translations[locale] = album_names[i]
		end
		album.save!

		 render :json =>{"success"=>true}.to_json
	end

	def save_tags
		case params[:tag]
		when "album"
			@object = GalleryAlbum.find(params[:id])
		when "pic"
			@object = GalleryImage.find(params[:id])
		end
		@object.update_attributes({:tag_ids => params[:tids]})
		render :json => {"success"=> "true"}.to_json
	end
end






