class JobsController < ApplicationController
  skip_before_action :authenticate_user!, only: [ :index, :cancel_button, :badge ]
  skip_before_action :verified_user!, only: [ :index ]
  skip_verify_authorized only: [ :index, :new, :create, :cancel_button, :badge]

  # GET /jobs or /jobs.json
  def index
    @jobs = Job.currently_working_on
    @no_turbo_stream = true
  end

  # GET /jobs/new
  def new
    @job = Job.new(customer_firstname: current_user.firstname, customer_lastname: current_user.lastname)
  end

  # POST /jobs or /jobs.json
  def create
    @job = Job.new(job_params)
    @job.customer = current_user

    respond_to do |format|
      if @job.save
        flash[:notice] = "Job was successfully created."
        broadcast_create_job
        format.html { redirect_to jobs_url }
      else
        format.html { render :new, status: :unprocessable_entity }
      end
    end
  end

  def cancel
    @job = Job.find(params[:id])
    authorize! @job
    if @job.canceled!
      flash[:notice] = "Job successfully canceled"
      @status_changed = true
    else
      flash[:alert] = "Job could not be canceled"
    end

    respond_to do |format|
      broadcast_update_job
      format.turbo_stream
      format.html { redirect_to jobs_url }
    end
  end

  def cancel_button
    @job = Job.find(params[:id])

    render partial: "jobs/cancel_button", locals: { job: @job }
  end

  def badge
    @job = Job.find(params[:id])

    render partial: "jobs/badge", locals: { job: @job }
  end

  private

  def broadcast_update_status_cards_and_start_next_job_button
    # FIXME: Duplicate code, see operator/jobs_controller, move to the model?
    openjobs = Job.open.with_attached_pdf.order(:created_at)
    printingjobs = Job.printing.with_attached_pdf.order(:status_changed_at)
    pickupjobs = Job.pickup.with_attached_pdf.order(:status_changed_at)
    paidjobs = Job.paid.with_attached_pdf.status_changed_today.order(:status_changed_at)
    canceledjobs = Job.canceled.with_attached_pdf.status_changed_today.order(:status_changed_at)
    paidcanceledjobs = Job.paidcanceled.with_attached_pdf.status_changed_today.order(:status_changed_at)
    Turbo::StreamsChannel.broadcast_replace_to "operator_jobs", target: "openjobs_status_card", partial: "operator/jobs/job_status_card", locals: { jobs: openjobs, status: :open }
    Turbo::StreamsChannel.broadcast_replace_to "operator_jobs", target: "printingjobs_status_card", partial: "operator/jobs/job_status_card", locals: { jobs: printingjobs, status: :printing }
    Turbo::StreamsChannel.broadcast_replace_to "operator_jobs", target: "pickupjobs_status_card", partial: "operator/jobs/job_status_card", locals: { jobs: pickupjobs, status: :pickup }
    Turbo::StreamsChannel.broadcast_replace_to "operator_jobs", target: "paidjobs_status_card", partial: "operator/jobs/job_status_card", locals: { jobs: paidjobs, status: :paid }
    Turbo::StreamsChannel.broadcast_replace_to "operator_jobs", target: "canceledjobs_status_card", partial: "operator/jobs/job_status_card", locals: { jobs: canceledjobs, status: :canceled }
    Turbo::StreamsChannel.broadcast_replace_to "operator_jobs", target: "start_next_job_btn", partial: "operator/jobs/start_next_job_btn", locals: { jobs: openjobs }
  end

  def broadcast_create_job
    Turbo::StreamsChannel.broadcast_append_later_to "jobs", target: :jobs, partial: "jobs/job_tr", locals: { job: @job }
    Turbo::StreamsChannel.broadcast_append_later_to "operator_jobs", target: :openjobs, partial: "operator/jobs/job_tr", locals: { job: @job }
    broadcast_update_status_cards_and_start_next_job_button
  end

  def broadcast_update_job
    Turbo::StreamsChannel.broadcast_replace_later_to "jobs", target: @job, partial: "jobs/job_tr", locals: { job: @job }
    if @status_changed
      Turbo::StreamsChannel.broadcast_remove_to "operator_jobs", target: @job
      broadcast_update_status_cards_and_start_next_job_button
      case @job.status.to_sym
      when :canceled, :paid then Turbo::StreamsChannel.broadcast_append_later_to "operator_jobs", target: :paidcanceledjobs, partial: "operator/jobs/job_tr", locals: { job: @job }
      when :open then Turbo::StreamsChannel.broadcast_append_later_to "operator_jobs", target: :openjobs, partial: "operator/jobs/job_tr", locals: { job: @job }
      when :printing then Turbo::StreamsChannel.broadcast_append_later_to "operator_jobs", target: :printingjobs, partial: "operator/jobs/job_tr", locals: { job: @job }
      when :pickup then Turbo::StreamsChannel.broadcast_append_later_to "operator_jobs", target:  :pickupjobs, partial:  "operator/jobs/job_tr", locals: { job: @job }
      end
    else
      Turbo::StreamsChannel.broadcast_replace_later_to "operator_jobs", target: @job, partial: "operator/jobs/job_tr", locals: { job: @job }
    end
  end

  # Only allow a list of trusted parameters through.
  def job_params
    params.require(:job).permit(:customer_id, :customer_firstname, :customer_lastname, :privacy_policy, :pdf)
  end
end
