class InformationRequestRequirement < ActiveRecord::Base
  attr_accessible :comment_id, :weight, :information_type, :resolution_type, :redirected_id

  belongs_to :comment

  validates :comment_id, :weight, :information_type, :resolution_type, presence: true
  validates :weight, numericality: { greater_than: 0, only_integer: true }

  before_create :redirect_blank

  delegate :message, :process_days, :assigned_administrative_units, :assigned_administrative_units_emails, to: :comment, prefix: false, allow_nil: true

  def redirect_blank
    if resolution_type == 'art_67_laip'
      clone_request
    else
      self.redirected_id = nil
    end
  end

  def get_information_type
    InformationRequest::INFORMATION_TYPE[information_type]
  end

  def get_resolution_type
    s = ""
    InformationRequest::RESOLUTION_TYPE[get_information_type].each{|a| s = a[0] if a[1] == resolution_type } rescue nil
    s
  end

  def complete?
    information_type=='informal' or information_type=='public' or (information_type=='confidential' and resolution_type=='art_31_32_laip')
  end

  def partial?
    (information_type=='reserved' and resolution_type=='art_30_laip_reserved') or (information_type=='confidential' and resolution_type=='art_30_laip_confidential')
  end

  def not_appropiate?
    information_type=='re_routed' or information_type=='non_existent' or information_type=='undelivered'
  end

  def total_reserve?
    (information_type=='reserved' and resolution_type=='art_19_laip') or (information_type=='confidential' and resolution_type=='art_24_laip')
  end

  def not_processed?
    information_type=='not_processed'
  end

  def informal?
    information_type=='informal'
  end

  def public?
    information_type=='public'
  end

  def denied_confidential?
    resolution_type=='art_24_laip'
  end

  def public_confidential?
    resolution_type=='art_30_laip_confidential'
  end

  def personal_data?
    resolution_type=='art_31_32_laip'
  end

  def denied_reserved?
    resolution_type=='art_19_laip'
  end

  def public_reserved?
    resolution_type=='art_30_laip_reserved'
  end

  def public_version?
    resolution_type=='art_30_laip_reserved' or resolution_type=='art_30_laip_confidential'
  end

  def non_existent?
    information_type=='non_existent'
  end

  def undelivered?
    information_type=='undelivered'
  end

  def redirected?
    information_type=='re_routed'
  end

  def unprocessed?
    information_type=='not_processed'
  end

  def closed_date
    created_at
  end

  def clone_request
    request = comment.commentable
    # dup the information request
    new_request = request.dup
    new_request.redirected_institution_id = new_request.institution_id
    new_request.institution_id = redirected_id
    new_request.state_id = new_request.city.state_id rescue nil
    new_request.skip_textcaptcha = true
    new_request.document_front_image = request.document_front_image
    new_request.document_back_image = request.document_back_image
    new_request.delivery_deadline = nil
    new_request.extension_type = nil
    new_request.information_type = nil
    new_request.resolution_type = nil
    new_request.correlative = nil
    new_request.delivery_deadline = nil
    #new_request.set_correlative
    new_request.confirmed_at = Time.current
    new_request.admitted_at = nil
    new_request.state = 'new'
    if new_request.save
      c = new_request.comments.new
      c.message = "Solicitud redireccionada desde #{request.institution.try(:name)}, con el siguiente detalle:\n#{comment.events.last.try(:justification)}"
      c.user_id = comment.user_id
      c.save
      new_request.deliver_information_request
    end
  end

end
