class Panel::Gallery::BackEnd::AlbumsController < OrbitBackendController
  open_for_visitor :only => [:index, :show, :get_album_json, :get_imgs_json, :imgs]
  open_for_manager :except => [:index,:show,:update]
  open_for_sub_manager :except => [:index, :show]

  before_filter :only => [ :new, :create, :edit, :update ] do |controller|
    @categories = get_categories_for_form
  end

  # before_filter lambda
  
  def index
    @categories = get_categories_for_index
    @tags = get_tags
    category_ids = @categories.collect{|t| t.id}
    @albums = get_sorted_and_filtered("album", :category_id.in => category_ids)

  end

  def show
    @album = Album.find(params[:id])
    @images = @album.album_images
    @image_content = []
    @images.each do |image|
      @image_content << {"id" => image.id.to_s, "description" => image.description_translations,"tags" => image.tagged_ids}
    end
    @tags = get_tags
    respond_to do |h|
      h.html
      h.json {render json: @image_content}
    end
  end

  def new
    @categories = get_categories_for_index
    @tags = get_tags 
    @album = Album.new
   
  end

  def create
    album = Album.new(params[:album])
    album.save!
    redirect_to panel_gallery_back_end_albums_path
  end



  def destroy
    album = Album.find(params[:id])
    album.destroy
    redirect_to panel_gallery_back_end_albums_path

  end

  def edit
      @album = Album.find(params[:id])
      @tags = get_tags
  end

  def set_cover
    if params[:set_cover] == "true"
      album = Album.find(params[:album_id])
      image = AlbumImage.find(params[:image_id])
      album.update_attributes({:cover_path => image.file.thumb.url, :cover=>params[:image_id]})
    else
      album = Album.find(params[:album_id])
      album.update_attributes({:cover_path => nil, :cover=>"default"})
    end
    render :json =>{"success"=>true}.to_json
  end

  def get_album_json
    albums = Album.all
    output = Array.new

    albums.each do |album|
      tag_names = Array.new

      album.tag_ids.each do |tag|
        tag_names << get_tags.include?(tag)
      end
      if album.cover_path
        cover_path = album.cover_path
      else
        cover_path = "/assets/gallery/default.jpg"
      end
      output << {
        album_cover_file: "http://#{request.host_with_port}"+cover_path,
        album_name: album.name,
        album_tag_names: tag_names,
        album_link:"http://#{request.host_with_port}#{panel_gallery_back_end_album_get_imgs_json_path(album)}",
      }
    end

    render :json=>JSON.pretty_generate(output)
  end

  def get_imgs_json
    album = Album.find(params[:album_id])
    images = album.album_images.all
    output = Array.new

    images.each do |image|
      tags = Array.new

      image.tag_ids.each do |tag|
        tags << get_tags.include?(tag)
      end
       
      if image.file.theater.present?
        @image_file = image.file.theater.url
      else
        @image_file = image.file.url
      end

      output << {
        image_title: image.title,
        image_description: image.description,
        image_file: { url: "http://#{request.host_with_port}#{@image_file}",
                      thumb: "http://#{request.host_with_port}#{image.file.thumb.to_s}"},
                      image_tag_names: tags}
    end

    render :json=>JSON.pretty_generate(output)
  end

  def imgs
    @album = Album.find(params[:album_id])
    @tag_names = Array.new
    @images = @album.album_images.all
    @output =  Array.new
    @images.each do |values|
      tags = Array.new
      values.tag_ids.each do |tag|
        tags << get_tags.include?(tag)
      end
      @output << { _id:  values.id,
                   theater_link: panel_gallery_back_end_album_image_path(values),
                   description: values.description,
                   title: values.title,
                   file: values.file.as_json[:file],
                   gallery_album_id: values.album_id,
                   tag_ids: values.tag_ids,
                   tag_names: tags}
    end
      render :json=>{"images" => @output, "tags" => @album.tag_ids}.to_json
  end



  def upload_image
    @album = Album.find(params[:album_id])
    @files = params['files']
    a = Array.new
    @files.each do |file|
      @image = @album.album_images.new
      @image.file = file
      @image.tagged_ids = @album.tag_ids
      @image.save!
      a << {"thumbnail_url"=>@image.file.thumb.url,"url"=>panel_gallery_back_end_album_image_path(@image)}
    end
    render :json=>{"files"=>a}.to_json
  end

  def new_images 
    if params[:last_image_id] != ""
      @lastimage = AlbumImage.find(params[:last_image_id])
      @album = Album.find(params[:album_id])
      @newimages = @album.album_images.where(:created_at.gt => @lastimage.created_at)
    else
      @album = Album.find(params[:album_id])
      @newimages = @album.album_images
    end
    render :layout=>false
  end


  def images_tags
    album = Album.find(params[:album_id])
    tags = Array.new
    images = album.album_images.all
    images.each do |image|
      tags << {"id"=>image.id, "tags" => image.tag_ids}
    end
    render :json=>tags.to_json
  end

  def update
    @album = Album.find(params[:id])
    tagsToDestroy = []
    tagsToAdd = []
    new_tags = params[:album][:tag_ids] || []
    old_tags = @album.tagged_ids
    old_tags.each do |tag|
       if !new_tags.include?(tag)
        tagsToDestroy << tag
        end
    end
    if new_tags != nil
      new_tags.each do |tag|
        if !old_tags.include?(tag)
          tagsToAdd << tag
        end
      end
    end
    update_children_image_tag(tagsToDestroy,tagsToAdd)
    params[:album][:tag_ids] = params[:album][:tag_ids] || []
    @album.update_attributes(params[:album])
    redirect_to panel_gallery_back_end_album_path(@album)
  end

  def update_children_image_tag(tagsToDestroy,tagsToAdd)
      # tagsToDestroy will contain  all tag ids which have to be deleted from the galley_images
      # tagsToAdd will contain all tag ids which ve to be added in tall album_images
      @images = AlbumImage.all
      @images.each do |image|
        image.tagged_ids.concat(tagsToAdd) if !tagsToAdd.blank?
        tagsToDestroy.each do |tag|
          if image.tagged_ids.include?tag
            image.tagged_ids.delete(tag)
          end
        end
        image.save
      end
  end
end
