# frozen_string_literal: true

module UiRules
  class TipBinRule < Base
    def generate_rules
      @repo = ProductionApp::ResourceRepo.new
      make_form_object
      apply_form_values
      common_values_for_fields common_fields
      user_production_line unless @form_object.production_line_id.nil?
      enter_gross_weight if @mode == :enter_gross_weight
      form_name 'tip_bin'
    end

    def enter_gross_weight
      fields[:bin_asset_number] = { renderer: :label }
      fields[:gross_weight] = { renderer: :numeric, required: true }
    end

    def user_production_line
      packhouse_resource_id_label = ProductionApp::ResourceRepo.new.find_plant_resource(@form_object.production_line_id)&.plant_resource_code
      fields[:production_line_label] = { renderer: :label, with_value: packhouse_resource_id_label, caption: 'Production Line' }
      fields[:production_line_id] = { renderer: :hidden }
    end

    def common_fields
      {
        production_line_id: { renderer: :select,
                              options: @repo.for_select_plant_resources_of_type(Crossbeams::Config::ResourceDefinitions::LINE),
                              caption: 'Production Line', prompt: true, required: true },
        production_line_label: { renderer: :label, invisible: true },
        bin_asset_number: { required: true }
      }
    end

    def make_form_object
      @form_object = OpenStruct.new
    end
  end
end
