class DashboardController < ApplicationController
  layout 'theme1'

  def demo_both
    render layout: 'demo-both'
  end

  # Testing action
  def select_qrcode
    #@tables = Table.all.sample(2) | List.active.map(&:table)
    if Rails.env.test?
      @tables = Table.all
    else
      @tables = (current_supplier || Supplier.first).tables.sample(5) | List.active.map(&:table)
    end
    respond_to do |format|
      format.html { render layout: 'phone' }
      format.json { render json: @tables.to_json }
    end
  end


  # GET /select_qr_image
  # GET /select_qr_image.png
  # GET /select_qr_image.svg
  def table_qr_image
    @table = Table.find(params[:table_id])
    #code = [get_the_app_url, {table_id: @table.id}.to_json].join('?')
    code = "q.qwaiter.nl/s?t=#{@table.id}"
    size = RQRCode.minimum_qr_size_from_string(code)
    respond_to do |format|
      format.html
      format.svg { render qrcode: code, level: :l, unit: 10, table_number: @table.number, qcontainer: true }
      format.png do
        #render qrcode: code, level: :l, table_number: @table.number, qcontainer: true 
        size = RQRCode.minimum_qr_size_from_string(code)
        level = :l
        qrcode = RQRCode::QRCode.new(code, size: size, level: level)
        svg    = RQRCode::Renderers::SVG::render(qrcode)
        svg_file = Tempfile.new(['table_qr', '.svg']){|f| f.puts svg}
        png_target = svg_file.path.sub /svg$/, 'png'

        render nothing: true
      end
    end
  end

  def qr
    code = 'qwaiter.com'
    respond_to do |format|
      format.html
      format.png { render qrcode: code, level: :h, unit: 10, size: 7}
      format.svg { render qrcode: code, level: :h, unit: 10, size: 7}
    end
  end
end
