class SeedGenerator < ApplicationRecord
  # Generate ID for Tables
  def self.generate_id(model, prefix)
    prefix ||= ''
    prefix << '-' if prefix.present?

    if ENV["SERVER_MODE"] == 'cloud'
      prefix << 'C'
    # else
      prefix << 'L'
    end

    if shop = Shop.current_shop
      prefix << shop.shop_code
    end

    seed = self.update_seed(model)
    length = 16 - prefix.length
    prefix + seed.to_s.rjust(length, '0')
  end

  def self.generate_ids(model, prefix, count = 1)
    prefix ||= ''
    prefix << '-' if prefix.present?

    if ENV["SERVER_MODE"] == 'cloud'
      prefix << 'C'
    else
      prefix << 'L'
    end

    if shop = Shop.current_shop
      prefix << shop.shop_code
    end

    start = self.update_seed(model, count)
    stop = start + count - 1
    length = 16 - prefix.length
    (start..stop).map { |c| prefix + c.to_s.rjust(length, '0') }
  end

  def self.sync_seed_generator_records(seed_generators)
    if !seed_generators.nil?
      seed_generators.each do |sg|
        seed = SeedGenerator.find_by_model(sg['model'])
        if seed.nil?
          seed = SeedGenerator.new
        end
        seed.model = sg['model']
        seed.increase_by = sg['increase_by']
        seed.current = sg['current']
        seed.next = sg['next']
        seed.save
      end
      Rails.logger.debug '....... Seed Generator sync completed! .......'
    end
  end

  # Generate Receipt No for number order (1,2,3) Don't touch
  def self.new_receipt_no
    seed = SeedGenerator.find_by_model("Sale")
    new_receipt_no = 1
    # if (seed.nil?)
    #   seed = SeedGenerator.new()
    #   seed.model = "Sale"
    #   new_receipt_no = seed.next
    #   seed.save
    # else
      # current_no = seed.next
      # seed.next = seed.next
      # seed.current = current_no
      # seed.save
    # end
    if !seed.nil?
      if seed.current > 0
        new_receipt_no = seed.current
      else
        new_receipt_no = seed.current + 1
      end
    end

    return new_receipt_no
  end

  # Generate for 4 digit Code
  # def self.generate_code(model, prefix)
  #   seed = SeedGenerator.find_by_model(model)
  #   new_code = 0

  #   if (seed.nil?)
  #     seed = SeedGenerator.new()
  #     seed.model = model
  #     new_code = seed.next
  #     seed.save
  #   else
  #     current_no = seed.next
  #     seed.next = seed.next + seed.increase_by
  #     seed.current = current_no
  #     seed.save
  #   end

  #   if prefix.length == 1
  #     padding_len = 5 - prefix.length
  #     count = 4-prefix.length
  #   else prefix.length == 2
  #     padding_len = 6 - prefix.length
  #     count = 5-prefix.length
  #   end
  #   next_code = prefix + seed.current.to_s.to_s.rjust((count)+1,'0')
  #   return next_code
  # end

  def self.get_model_name(model)
    model_name = ""
    if ENV["SERVER_MODE"] == 'cloud'
      model_name = "Cloud#{model}"
    else
      model_name = model
    end

    return model_name
  end

  def self.update_seed(model, count = 1)
    SeedGenerator.transaction do
      seed = SeedGenerator.lock.find_by_model(get_model_name(model)) ||
              SeedGenerator.lock.find_by_model(model)
      seed.next = seed.next + (count * seed.increase_by)
      seed.current = seed.next - seed.increase_by
      seed.save!
      seed.next_before_last_save
    end
  end
end
