class CabPrinter < ActionPrinter
  # J Job start
  # S Set label size
  # H Heat, speed, and printing method
  # O Set print options
  # T Text field definition
  # B Barcode field definition
  # G Graphic field definition
  # I Image field definition
  # A Amount of labels
  def do_render(print_job,*args)
    path = print_job.view_template_path
    #TODO vi får en fejl her når vi begynder at mixe modellerne f.eks. Employee.find_by_sql('select * from products')
    locals = { resource_class: print_job.printing_class.constantize, collection: print_job.printing_class.constantize.find_by_sql( print_job.print_sql)  }
    #
    # TODO args must be mergeable ;)
    # if args.flatten.compact.any?
    #   locals.merge! args
    # end
    if (path =~/^---/) # this will return falsy if found
      of = text_file render_string_in path.gsub( /^---/, ''), locals
    else
      of = text_file render( file: path, formats: [:html], handlers: [:haml,:erb], locals: locals)
    end
    logit :info, "created a label file: #{of.path}"
    var = %x[ mv #{of.path} #{label_file_path} ]
    logit :info, "moved it to: #{label_file_path}"
    true
  rescue => e
    logit :error, "Rendering to TXT failed! The error was #{e.message}"
    false
  end

  # CAB print last command is "A n" where n is the number of copies - an information which we add at print time
  def do_print copies=1, usr=nil
    result = nil
    prn = printer
    unless prn.command.blank?
      if prn.command=="email"
        prn.delete
        return super
      else
        if copies.to_i > 0
          cpcmd = `echo 'A #{copies.to_i}\n' >> #{label_file_path}`
          label_path =  label_file_path.split("/")[-1]
          pap = paper || prn.paper
          cmd = prn.command.gsub(/\$1/, prn.cups_printer).gsub(/\$2/, label_file_path)
          # Here we start actually printing
          # $1 is the CUPS printer
          # $2 is the PDF file_path_and_name
          # $3 is the PDF filename
          # the command being something along the lines of: lp -d $1 -o media=a4 $2
          result = `#{cmd}`
        end
      end
    end
    !result.blank?
  rescue
    false
  end

  def file_path
    label_file_path
  end

end
#
#
# cmd = self.say_c4_label
#
# cmd = "
# J
# H 175,+0
# O R
# S l1;0,0,145,149,104;label
# G 5,5,0;R:140,100,.5,.5
# A 1
# "
#
# return false if cmd=="" # say_c4_label returned false due to some error
#
# #
# #   TODO 2008-09-06 remove the tmp/labels_for_task_#{self.id.to_s} file
# #   TODO 2008-09-09 make a Spool to show printer assigned jobs and printer status
# File.open("#{Rails.root}/public/documents/labels/label_for_stock_item_#{self.id.to_s}", "w") do |f|
#       f.write(cmd)
# end
#
# # user=User.logged_in_user
# user=User.find(1) if Rails.env == "development"
# p = user.printers.on_paper("Pakkelabel").preferred_printer rescue []
# if p.any?
#   p = p.first
#   cmd = p.command.gsub(/\$1/, p.cups_printer).gsub(/\$2/, "#{Rails.root}/public/documents/labels/label_for_stock_item_#{self.id.to_s}")
#   # return false unless self.upload_logo( p.cups_printer)
#   value = `#{cmd}`
#   puts cmd
#   return true
# else
#   return false
# end
#