class Operator::JobsController < ApplicationController
  before_action :set_job, only: %i[show edit update destroy increment_page decrement_page]
  before_action :set_job_lists, only: %i[index]
  before_action :authorize!

  # GET /jobs or /jobs.json
  def index
  end

  # GET /jobs/1 or /jobs/1.json
  def show; end

  # GET /jobs/new
  def new
    @job = Job.new
  end

  # GET /jobs/1/edit
  def edit; end

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

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

  # PATCH/PUT /jobs/1 or /jobs/1.json
  def update
    @job.assign_attributes(job_params)
    @status_changed = @job.status_changed?
    @job.current_user = current_user if @status_changed

    respond_to do |format|
      if @job.save
        broadcast_update_job
        format.turbo_stream unless request.referrer.include? "edit"
        format.html { redirect_to operator_jobs_url, notice: "Job was successfully updated." }
      else
        flash[:alert] = "Job was not updated."
        format.html { render :edit, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /jobs/1 or /jobs/1.json
  def destroy
    @job.destroy!

    respond_to do |format|
      Turbo::StreamsChannel.broadcast_remove_to "jobs", target: @job
      format.html { redirect_to operator_jobs_url, notice: "Job was successfully destroyed." }
    end
  end

  def increment_page
    if @job.increment_page(params[:din])
      flash[:notice] = "Job was successfully updated"
    else
      flash[:alert] = "Job could not be updated"
      # NOTE: Discard changes that could not be saved so turbo_stream renders the correct values
      @job.reload
    end

    respond_to do |format|
      broadcast_update_job
      format.turbo_stream { render turbo_stream: turbo_stream.replace(@job, partial: "operator/jobs/job_tr", locals: { job: @job }) }
      format.html { redirect_to operator_jobs_url }
    end
  end

  def decrement_page
    if @job.decrement_page(params[:din])
      flash[:notice] = "Job was successfully updated"
    else
      flash[:alert] = "Job could not be updated"
      # NOTE: Discard changes that could not be saved so turbo_stream renders the correct values
      @job.reload
    end

    respond_to do |format|
      broadcast_update_job
      format.turbo_stream { render turbo_stream: turbo_stream.replace(@job, partial: "operator/jobs/job_tr", locals: { job: @job }) }
      format.html { redirect_to operator_jobs_url }
    end
  end

  private

  # Use callbacks to share common setup or constraints between actions.
  def set_job
    @job = Job.find(params[:id])
  end

  def set_job_lists
    # FIXME: Duplicate code, see jobs_controller, move to the model, Mabye moving to 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)
  end

  # Only allow a list of trusted parameters through.
  def job_params
    params.require(:job).permit(:pdf, :operator_id, :customer_id, :operator_firstname, :operator_lastname, :customer_firstname, :customer_lastname, :status, :privacy_policy, :intern, :cost_center, :number_of_plans_a0, :number_of_plans_a1, :number_of_plans_a2, :number_of_plans_a3, :costum_qm_plan)
  end

  # FIXME: Move broadcast to model though i don't think view logic belongs in the model
  def broadcast_update_status_cards_and_start_next_job_button
    set_job_lists
    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_update_to "operator_jobs", target: :openjobs, partial: "operator/jobs/job_tr", collection: @openjobs, as: :job
        # 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_to "operator_jobs", target: @job, partial: "operator/jobs/job_tr", locals: { job: @job }
    end
  end
end
