module PsAdmin
  # Images controller
  class ImageController < ApplicationController
    before_action :authenticate_admin!
    layout 'admin'

    def index; end

    def create
      @product = Product.where(id: params[:product_id]).first
      return if @product.blank?
      return if params[:images].blank?
      params[:images].each do |image|
        if @product.images.blank?
          @product.images.create(image: image, updated_by: current_admin.id,
                                 position: @product.images.size + 1,
                                 cover: true)
        else
          @product.images.create(image: image, updated_by: current_admin.id,
                                 position: @product.images.size + 1)
        end
      end
    end

    def set_cover
      @image = Image.where(id: params[:id]).first
      unless @image.blank?
        @image.cover = true
        @image.save
        Image.where('imageable_type = ? AND imageable_id = ? AND id != ?',
                    @image.imageable_type, @image.imageable_id, @image.id)
             .update_all(cover: false)
      end
      @product = Product.where(id: params[:product_id]).first
    end

    def set_active
      if !params[:id].blank? && !params[:active].blank?
        @image = Image.where(id: params[:id]).first
        unless @image.blank?
          @image.active = params[:active].to_i
          @image.save
          unless params[:product_id].blank?
            @product = Product.where(id: params[:product_id]).first
          end
        end
      end
    end

    def pos_up
      im_this = Image.find(params[:id])
      im_this_pos = im_this.position
      im_last =
        Image.by_image_type(im_this.imageable_type)
             .where('imageable_id = ? AND position < ?', im_this.imageable_id,
                    im_this_pos)
             .by_position_desc.first
      im_this.position = im_last.position
      im_this.save
      im_last.position = im_this_pos
      im_last.save
      @product = im_this.imageable
    end

    def pos_down
      im_this = Image.find(params[:id])
      im_this_pos = im_this.position
      im_last =
        Image.by_image_type(im_this.imageable_type)
             .where('imageable_id = ? AND position > ?', im_this.imageable_id,
                    im_this_pos).by_position_asc.first
      im_this.position = im_last.position
      im_this.save
      im_last.position = im_this_pos
      im_last.save
      @product = im_this.imageable
    end

    def destroy
      image = Image.where(id: params[:id]).first
      unless image.blank?
        if image.cover.eql?(true)
          img = Image.by_image_type(image.imageable_type)
                     .where('imageable_id = ? AND id != ?',
                            image.imageable_id, image.id).first
          unless img.blank?
            img.cover = true
            img.save
          end
        end
        image.destroy
      end
      @product = Product.where(id: params[:product_id]).first
    end
  end
end
