# frozen_string_literal: true

module Decidim
  module Budgets
    # This controller is the abstract class from which all other controllers of
    # this engine inherit.
    #
    # Note that it inherits from `Decidim::Components::BaseController`, which
    # override its layout and provide all kinds of useful methods.
    class ApplicationController < Decidim::Components::BaseController
      helper_method :current_workflow, :voting_finished?, :voting_open?, :show_votes_count?

      def current_workflow
        @current_workflow ||= Decidim::Budgets.workflows[workflow_name].new(current_component, current_user)
      end

      def voting_open?
        current_settings.votes == "enabled"
      end

      def voting_finished?
        current_settings.votes == "finished"
      end

      def show_votes_count?
        current_settings.show_votes?
      end

      def resource_added?(project)
        current_order && current_order.projects.include?(project)
      end

      private

      def workflow_name
        @workflow_name ||= current_component.settings.workflow.to_sym
      end

      def set_focus_mode_if_voting_open
        @focus_mode = true if voting_open?
      end
    end
  end
end
