class AvailableProductsDatatable
  delegate :params, :fa_icon, :available_products_path, to: :@view

  def initialize(view, pointsale)
    @view = view
    @pointsale = pointsale
  end

  def as_json(*)
    {
      sEcho: params[:sEcho].to_i,
      iTotalRecords: @pointsale.products.activos_children.size,
      iTotalDisplayRecords: products.total_entries,
      aaData: data
    }
  end

  private

  def data
    if params[:table] == 'in_pointsale'
      products.map do |available|
        category = available.product.categories[0]
        {
          'DT_RowId' => "available_#{available.id}",
          '0' => '<input class="form-control checkboxes" type="checkbox"/>',
          '1' => available.product.sku,
          '2' => get_display_name(available.product),
          '3' => (category.parent_id.zero? ? category.category : category.parent.category),
          '4' => (category.parent_id != 0 ? category.category : ' '),
          '5' => available.stock
        }.compact.reject { |_k, v| v.nil? }
      end

    else
      products.map do |product|
        category = product.categories[0]
        {
          'DT_RowId' => "product_#{product.id}",
          '0' => '<input class="form-control checkboxes" type="checkbox"/>',
          '1' => product.sku,
          '2' => get_display_name(product),
          '3' => (category.parent_id.zero? ? category.category : category.parent.category),
          '4' => (category.parent_id != 0 ? category.category : ' ')
        }.compact.reject { |_k, v| v.nil? }
      end
    end
  end

  def get_display_name(product)
    name = "<label style='margin-bottom:0px'><strong>#{product.name}</strong></label> <br>"

    if product.display_attributes.present?
      name += "#{product.display_attributes} <br>"
    end

    if product.barcode.present?
      name += "<i class='fa fa-barcode'></i>: #{product.barcode} <br>"
    end

    if product.description.present?
      name += "Descripción: #{product.description}"
    end
    name
  end

  def products
    @products ||= fetch_products
  end

  def fetch_products
    if params[:table] == 'in_pointsale'
      products = AvailableProduct.activos.where(pointsale_id: params[:id]).activos
    else
      products_in_pointsale = @pointsale.products.activos_children.pluck(:id)
      products = Product.activos_children.where.not(id: products_in_pointsale).order('products.name')
    end

    products = products.page(page).per_page(per_page)
    search = params[:busqueda]
    name_searched = if search.include? ':'
                      search[0, search.index(':') - 1]
                    else
                      search
                    end
    unless search.blank?
      products = products.where("products.sku ilike :search or products.name ilike :search", search: "%#{name_searched}%").order('products.name')
      if search.include? ':'
        attribute = search[search.index(':') + 1, search.length]
        products = products.where('attributes_json ilike :attribute', attribute: "%#{attribute}%")
      end
    end
    products
  end

  def page
    params[:start].to_i / per_page + 1
  end

  def per_page
    params[:length].to_i > 0 ? params[:length].to_i : 20
  end

  def sort_column
    columns = %w[name]
    columns[params[:iSortCol_0].to_i]
  end

  def sort_direction
    params[:sSortDir_0] == "desc" ? "desc" : "asc"
  end
end
