class InstitutionTravel < ActiveRecord::Base
  before_validation :clean_params_utf8
  # Accessible attributes
  attr_accessible :destination, :end_date, :institution_id, :institution_official_job, :institution_official_name,
    :lodgment_cost, :name, :objetive, :others_cost, :sponsor_contribution, :start_date, :travel_cost, :viatical_cost

  # Associations
  belongs_to :institution

  # Validations
  validates :name,
    :institution_official_name,
    :destination,
    :institution_id,
    :objetive,
    presence: true

  validates :travel_cost, :lodgment_cost, :viatical_cost, numericality: true, allow_nil: true

  validates_date :end_date, :start_date
  validates_date :end_date, on_or_after: :start_date
  
  validates :institution_id, uniqueness: { scope: [:name, :institution_official_name, :objetive, :institution_official_name, :institution_official_job, :destination, :start_date, :end_date, :travel_cost, :lodgment_cost, :viatical_cost, :others_cost, :sponsor_contribution], :message => "Datos del estandar ya se encuentran en uso"}


  # Will paginate uses this method
  # to know how many rows to show
  #
  def self.per_page
    50
  end

  def data_for_csv
    [
      name,
      institution_official_name,
      destination,
      institution_official_job,
      start_date,
      end_date,
      travel_cost,
      lodgment_cost,
      viatical_cost,
      ActionController::Base.helpers.strip_tags(objetive),
      others_cost,
      sponsor_contribution
    ]
  end

  def self.header_for_csv
    [
      :name,
      :institution_official_name,
      :destination,
      :institution_official_job,
      :start_date,
      :end_date,
      :travel_cost,
      :lodgment_cost,
      :viatical_cost,
      :objetive,
      :others_cost,
      :sponsor_contribution
    ]
  end

  def self.attributes_for_grid
    [
      :institution_id,
      :name,
      :institution_official_name,
      :destination,
      :institution_official_job,
      :start_date,
      :end_date,
      :travel_cost,
      :lodgment_cost,
      :viatical_cost,
      :objetive,
      :others_cost,
      :sponsor_contribution
    ]
  end

  def self.dedupe
    # find all models and group them on keys which should be common
    grouped = all.group_by{|model| [model.institution_id, model.name, model.institution_official_name, model.objetive, model.institution_official_name, model.institution_official_job, model.destination, model.start_date, model.end_date, model.travel_cost, model.lodgment_cost, model.viatical_cost, model.others_cost, model.sponsor_contribution] }
    grouped.values.each do |duplicates|
      # the first one we want to keep right?
      first_one = duplicates.shift # or pop for last one
      duplicates.each{|double| double.destroy} # duplicates can now be destroyed
    end
  end

  def clean_params_utf8
    puts self.institution_official_name.valid_encoding?
    self.name = self.name.encode("UTF-8", invalid: :replace, undef: :replace)
    self.institution_official_name = self.institution_official_name.encode("UTF-8", invalid: :replace, undef: :replace)
    self.institution_official_job = self.institution_official_job.encode("UTF-8", invalid: :replace, undef: :replace)
    self.destination = self.destination.encode("UTF-8", invalid: :replace, undef: :replace)
  end

end
