class Lookup < ApplicationRecord

  has_many :accounts

  scope :number_formats, -> { where(lookup_type: 'number_format')}

  TIME_LIMIT = 5

  def available_types
    {'Employee Roles' => 'employee_roles',
      'Dining Facilities Status' => 'dining_facilities_status',
      'Menu Item Type' => 'menu_item_type',
      'Menu Item Attribute Type' => 'menu_item_attribute_type',
      'Order Type' => 'order_type',
      'Order Source' => 'order_source',
      'Order Status' => 'order_status',
      'Order Item Status' => 'order_item_status',
      'Sale Status' => 'sales_status',
      'Payment Status' => 'payment_status',
      'Payment Methods' => 'payment_methods',
      "Gateway Communication Type" => "gateway_communication_type"}
  end

  # def self.get_by_type( lookup_type)
  #   Lookup.select("value, name").where("lookup_type = ?", lookup_type ).order("name asc").map { |r| [r.name, r.value] }
  # end

  def self.time_limit
    TIME_LIMIT
  end

  def self.get_checkin_time_limit
    return RequestStore[:checkin_time_limit] if RequestStore[:checkin_time_limit]
    RequestStore[:checkin_time_limit] = Lookup.find_by_lookup_type('checkin_time_limit').value.to_i rescue time_limit
  end

  def self.sync_url
    url = ""

    lookup = Lookup.find_by_lookup_type('sync_data')
    if !lookup.nil?
      url = lookup.value
    end

    return url
  end

  def self.get_sync_token
    token = ""

    lookup = Lookup.find_by_lookup_type('sync_token')
    if !lookup.nil?
      token = lookup.value
    end

    return token
  end

  def self.collection_of(type)
    Lookup.where("lookup_type" => type ).pluck("name, value")
  end

  def self.create_shift_sale_lookup
    @lookup = Lookup.new
    @lookup.lookup_type = 'shift_sale_items'
    @lookup.name = 'Shift Sale Items'
    @lookup.value = 0
    @lookup.save

    return @lookup
  end

  def self.create_reprint_receipt_lookup
    @lookup = Lookup.new
    @lookup.lookup_type = 'reprint_receipt'
    @lookup.name = 'Reprint Receipt in Report'
    @lookup.value = 0
    @lookup.save

    return @lookup
  end

  def self.save_shift_sale_items_settings(val)
    @lookup = Lookup.where("lookup_type=?", 'shift_sale_items').last
    if @lookup.nil?
      @lookup = Lookup.create_shift_sale_lookup
    end
    @lookup.value = val
    @lookup.save
  end

  def self.shift_sale_items_lookup_value
    @lookup = Lookup.where("lookup_type=?", 'shift_sale_items').last
    if @lookup.nil?
      @lookup = Lookup.create_shift_sale_lookup
    end
    return @lookup.value
  end
end
