class DashboardController < ApplicationController
  layout 'theme1'
  before_action :allow_all_origins, only: :error_report

  def demo_both
    render layout: 'demo-both'
  end

  def user_app_log
    log = UserAppLog.new(params: params[:log])
    log.user = current_user
    log.save
    render nothing: true
  end

  def scan

  end

  def close_window
    render layout: false
  end

  # Testing action
  def select_qrcode
    #@tables = Table.all.sample(2) | List.active.map(&:table)
    if Rails.env.test?
      @tables = Table.all
    else
      @tables = List.active.map(&:table) | Supplier.find_by_name('Mozo').tables.select{|t| t.number.between? 20, 500}.sample(3)
    end
    respond_to do |format|
      format.html { render layout: 'phone' }
      format.json { render json: @tables.to_json }
    end
  end

  def empty_page
    render text: ''
  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.mozo.bar/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 = 'mozo.bar'
    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
