module Support
  module Helpers
    class ProductDataTransformer
      def self.prep_data_for_api_sumbit(product_data, project_id)
        new_product_dict = product_data.clone
        new_product_dict = set_values(new_product_dict, product_data, project_id)
        new_product_dict = replace_key_names new_product_dict
        new_product_dict = delete_key_values new_product_dict
        new_product_dict = replace_country_with_number new_product_dict
        new_product_dict
      end

      private_class_method def self.set_values(new_product_dict, old_product_dict, project_id)
        new_product_dict[:distribution_format_id] =
          Support::Helpers::ProductPhysicalQueries.distribution_format_id_from_data(
            old_product_dict[:format], old_product_dict[:configuration]
          )
        new_product_dict[:genre_id] =
          Support::Helpers::ProductPhysicalQueries.genre_id old_product_dict[:genre]
        new_product_dict[:subgenre_id] =
          Support::Helpers::ProductPhysicalQueries.subgenre_id old_product_dict[:subgenre]
        new_product_dict[:packaging_id] =
          Support::Helpers::ProductPhysicalQueries.packaging_id old_product_dict[:packaging]
        new_product_dict[:assign_display_upc] = false
        new_product_dict[:display_configuration] = display_config_string new_product_dict
        new_product_dict[:project_id] = project_id
        new_product_dict
      end

      private_class_method def self.replace_key_names(new_product_dict)
        new_product_dict[:label] = new_product_dict.delete :imprint
        new_product_dict[:explicit] = new_product_dict.delete :parental_advisory
        new_product_dict[:sale_start_date] = new_product_dict.delete :sales_date
        new_product_dict[:discount] = new_product_dict.delete :discount_policy
        new_product_dict[:display_upc] = new_product_dict.delete :upc
        new_product_dict[:version] = new_product_dict.delete :product_version
        new_product_dict
      end

      private_class_method def self.delete_key_values(new_product_dict)
        new_product_dict.delete :genre
        new_product_dict.delete :subgenre
        new_product_dict.delete :packaging
        new_product_dict.delete :configuration
        new_product_dict.delete :format
        new_product_dict.delete :orchard_pricing
        new_product_dict.delete :version_notes
        new_product_dict
      end

      private_class_method def self.display_config_string(new_product_dict)
        new_product_dict[:units_per_set].to_s + " x " +
        new_product_dict[:configuration] + " " + new_product_dict[:format]
      end

      private_class_method def self.replace_country_with_number(new_product_dict)
        # Currently only functional for USA
        new_product_dict[:country_of_origin] = 1 if new_product_dict[:country_of_origin] == "USA"
        new_product_dict
      end
    end
  end
end
