class BaseOrigamiController < ActionController::Base
  include LoginVerification
  layout "origami"

  before_action :check_user

  #before_action :check_installation
  protect_from_forgery with: :exception      

  helper_method :current_token

  rescue_from CanCan::AccessDenied do |exception|
    flash[:warning] = exception.message
    # redirect_to origami_root_path
    redirect_to origami_dashboard_path
  end

  def check_user       
    if check_mobile     
      if current_user.nil?
        return render status: 401, json: {
          message: "User using other device!"          
        }.to_json
      end
    else       
      if current_user.nil?
        redirect_to root_path    
      end
    end
  end

  # def checkin_process
  #   CheckinJob.set(wait: 1.minute).perform_later()
  # end

  #  Get current Cashier
  def get_cashier
    @cashier = Employee.where("role = 'cashier' AND token_session <> ''")
  end

  #check webview
  def check_mobile
    status = false
    authenticate_with_http_token do |token, options|   
      if token
        session[:webview] = true
        session[:session_token] = token
      end            
    end
    
    if session[:webview] && request.user_agent =~ /android|blackberry|iphone|ipad|ipod|iemobile|mobile|webos/i
      status = true
    end

    return status
  end
end
