class FullScreenLightBoxesController < ApplicationController

  before_action :current_user_light_boxes
  after_action :store_current_light_box, except: :update

  layout "window"

  def create
    @light_box = current_user.light_boxes.new(name: params[:name], title: current_user.title)
    if @light_box.save
      flash[:alert] = I18n.t('successfully_updated')
      redirect_to(action: :show, id: @light_box.id)
    else
      flash[:alert] = I18n.t('failed')
      render action: :show
    end
  end

  def index
    @light_box = current_user_light_boxes.last
    @title = I18n.t("viewer", name: @light_box.name)
    @light_box_images = @light_box.images.where(content_error: 0)
    render action: :show, id: @light_box.id
  end

  def show
    @light_box = LightBox.find(params[:id])
    redirect_to action: :index if @light_box.nil?
    @title = I18n.t("viewer", name: @light_box.name)
    unless session[:provs]
      provbd = []
      provhd = []
      provpe = []
      tpgn = Title.find(current_user.title_id).title_provider_group_name.id
      provs = []
      TitleProviderGroup.joins(:provider).where(title_provider_group_name_id: tpgn).order('providers.name').collect{|tpg| provs << tpg.provider_id}
      provs.each do |p|
        tpg_id = TitleProviderGroup.where(provider_id: p, title_provider_group_name_id: tpgn)
        provbd[p] = Authorization.is_ok?(1, tpg_id)
        provhd[p] = Authorization.is_ok?(2, tpg_id)
        provpe[p] = Authorization.is_ok?(3, tpg_id)
      end
      session[:provs] = provs
      session[:provbd] = provbd
      session[:provhd] = provhd
      session[:provpe] = provpe
    end
    if params[:ids]
      pos = 0
      params[:ids].each do |id|
        if session[:provs][Image.find(id).provider_id] == 1
          lbi = LightBoxImage.where(light_box_id: @light_box, image_id: id).first
          lbi.update_attributes(position: pos) unless lbi.nil?
          pos += 1
        end
      end
    end
    if params[:order_by_provider]
      @light_box_images = @light_box.images.where(content_error: 0).order('provider_id')
    else
      @light_box_images = @light_box.images.where(content_error: 0).order('light_box_images.position')
    end
    @nopicto = no_picto
    if params[:print]
      render "show_print"
    else
      @ids = params[:ids]
      render
    end

  end

  def destroy
    @light_box = current_user_light_boxes.find(params[:id])
    redirect_to action: :show, id: current_user_light_boxes.last.id if @light_box.destroy
  end

  def update
    unless params[:lbid].blank?
      unless params[:ids].blank?
        @light_box = current_user_light_boxes.find(params[:lbid])
        params[:ids].split(',').collect{ |i| @light_box.light_box_images.create( {image_id: i.to_i}) }
        flash[:alert] = I18n.t('successfully_updated')
        redirect_to(action: :show, id: @light_box.id)
      else
        flash[:alert] = I18n.t("Pas d'images")
        redirect_to(action: :show, id: params[:lbid])
      end
    else
      @light_box = current_user_light_boxes.find(params[:id])
      if @light_box.update_attributes(permitted_params)
        flash[:alert] = I18n.t('successfully_updated')
        redirect_to(action: :show, id: @light_box.id)
      else
        render action: :edit
      end
    end
  end

  def edit
    @light_box = current_user_light_boxes.find(params[:id])
  end

  private

  def current_user_light_boxes
    @light_boxes ||= current_user.light_boxes
  end

  def store_current_light_box
    session[:active_light_box] = @light_box.id
  end

  def permitted_params
    params.require(:light_box).permit(:name)
  end

end
