class PrintSettingsController < ApplicationController
  load_and_authorize_resource except: [:create]
  before_action :set_print_setting, only: [:show, :edit, :update, :destroy]

  # GET /print_settings
  # GET /print_settings.json
  def index
    @print_settings = PrintSetting.all
  end

  # GET /print_settings/1
  # GET /print_settings/1.json
  def show
    @lookup = Lookup.shift_sale_items_lookup_value
  end

  # GET /print_settings/new
  def new
    @print_setting = PrintSetting.new
    @server_mode = ENV["SERVER_MODE"]
  end

  # GET /print_settings/1/edit
  def edit
    @lookup = Lookup.shift_sale_items_lookup_value
    @server_mode = ENV["SERVER_MODE"]
  end

  # POST /print_settings
  # POST /print_settings.json
  def create
    @print_setting = PrintSetting.new(print_setting_params)

    respond_to do |format|
      if @print_setting.save
        format.html { redirect_to @print_setting, notice: 'Print setting was successfully created.' }
        format.json { render :show, status: :created, location: @print_setting }
      else
        format.html { render :new }
        format.json { render json: @print_setting.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /print_settings/1
  # PATCH/PUT /print_settings/1.json
  def update
    respond_to do |format|
      if @print_setting.update(print_setting_params)
        if @print_setting.unique_code == 'CloseCashierPdf'
          Lookup.save_shift_sale_items_settings(params[:shift_sale_items])
        end

        format.html { redirect_to @print_setting, notice: 'Print setting was successfully updated.' }
        format.json { render :show, status: :ok, location: @print_setting }
      else
        format.html { render :edit }
        format.json { render json: @print_setting.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /print_settings/1
  # DELETE /print_settings/1.json
  def destroy
    @print_setting.destroy
    flash[:notice] = 'Print setting was successfully destroyed.'
    render :json => {:status=> "Success", :url => print_settings_url }.to_json
  end

  def get_printer_options    
    printer_name = params[:printer_name]    
    printer_options = Printer::PrinterWorker.printer_options(printer_name)
    options = {
      :url => printer_options['device-uri'],
      :model => printer_options['printer-info'],
    }

    render :json => options.to_json
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_print_setting
      @print_setting = PrintSetting.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def print_setting_params
      params.require(:print_setting).permit(:name, :unique_code, :template, :printer_name, :font,:header_font_size, :item_font_size, :api_settings, :page_width, :page_height, :print_copies,:precision,:delimiter,:heading_space)
    end
end
