class ProfileQuestionsController < ApplicationController

  # Edit profile Information through profile questions appearing at the top of the page.
  def edit
    @step = next_profile_question(params['step'].to_i+1) #next empty field from where user has skipped.
    if @step && (@step == 'complete') # if the skipped field is the only empty one.
      @step = 'complete'
    end
    unless @step == 'complete' #if its not the last field
      respond_to do |format|
        format.js {render 'skip'}
      end
    else
      redirect_via_turbolinks_to dashboard_path
    end
  end

  # Update profile Information through profile questions
  def update
    if current_user.update_attributes(user_params)
      @step =  next_profile_question(User::PROFILE_QUESTION.rindex(params['step']))
      respond_to do |format|
        unless @step.blank? || @step == 'complete'
          format.js { render 'update'}
        else
          format.js {redirect_to dashboard_path}
        end
      end
    else
      respond_to do |format|
        format.js { render template: "profile_questions/edit", locals: {partial_name: params[:step].gsub(':', '/')}, within: '#profile_questions' }
      end
    end


  end

  private
    def user_params
      params.required((current_user.try(:type) || 'User').downcase.to_sym).permit(
        :rate, :response_time, :exp_title,
        educations_attributes: [:field_of_study, :minor, :school_name, :end_date, :id],
        awards_attributes: [:name, :description, :issuer, :date_received, :id],
        positions_attributes: [:university_name, :title, :start_date, :end_date, :location, :description, :id],
        uploaded_syllabuses_attributes: [:course_id, :id, :_destroy, attachments_attributes: [:attachment, :id, :_destroy]],
        profile_image_attributes: [:crop_x, :crop_y, :crop_w, :crop_h, :image, :id]
      )
    end

end
