class ProfileController < ApplicationController
  layout 'application'

  before_filter :require_login,  except: [:show]
  skip_before_filter :check_bank_account, only: [:edit, :update]

  # To edit profile steps
  def edit
    @user = current_user
    @user.profile_step = User::PROFILE_STEPS.call.rindex(step)
    build_nested_objects
    if step.present?
      render "profile/steps/#{User.profile_step_partial(step)}"
    else
      render "#{Rails.root.to_s}/public/404.html", :layout => false, :status => 404
    end
  end

  # To update profile steps
  def update
    @user = current_user
    respond_to do |format|
      if @user.update_attributes(user_params)
        if next_step
          format.html { redirect_to edit_profile_path(next_step) }
          format.js { redirect_via_turbolinks_to edit_profile_path(next_step)}
        else
          message = current_user.update_columns(is_registration_completed: true) if current_user.student?
            unless @user.is_registration_completed?
              'Your profile is now complete, you will be visible to students once your profile is approved. You will receive an email shortly.'
            else
              'Your profile is updated'
            end
          redirect_path = personal_calendar_path
          format.html { redirect_to redirect_path, notice: message}
          format.js { redirect_via_turbolinks_to redirect_path, notice: message}
          UserMailer.delay(queue: :mailer).profile_completion(@user.id)
        end
      else
        format.html {redirect_to "profile/steps/#{User.profile_step_partial(step)}"}
        format.js { render template: "profile/steps/#{User.profile_step_partial(step)}", object: @user, within: '#profile_steps_content'}
      end
    end
  end

  # To visit user profile.
  def show
    @user = User.find_by_id(params[:id])
    if @user
      add_breadcrumb "Dashboard", :dashboard_path  if logged_in?
      add_breadcrumb "Search Results", :tutors_path
      add_breadcrumb @user.name, :profile_path
      ProfileVisit.increment!(@user, current_user)
      @reviews = @user.recieved_reviews.order('average_review DESC').page(1).per(3) if @user.tutor?
      render layout: 'application'
    else
      flash[:notice] = 'That user does not exist'
      redirect_to root_url
    end
  end

  private
  def build_nested_objects
    @user.bank_accounts.build if step == 'payment' and !@user.bank_accounts.exists?
  end

  def next_step
    return params[:next_step] if params[:next_step]
    current_user.next_profile_step(params[:id])
  end

  def step
    User::PROFILE_STEPS.call.find {|a_step| a_step == params[:id].to_s.downcase}
  end

  def user_params
    permitted_params = params.required(:user).permit(
      :location, :industry, :headline, :response_time, :exp_title, :summary,
      :category_ids, :user_course_ids, :course_ids, :rate,
      educations_attributes:
        [:school_name, :degree, :field_of_study, :minor, :description, :_destroy, :start_date, :end_date, :id],
      positions_attributes:
        [:university_name, :title, :start_date, :end_date, :is_current, :location, :description, :_destroy, :id],
      awards_attributes: [:name, :issuer, :date_received, :description, :_destroy, :id],
      bank_accounts_attributes: [:name_on_account, :routing_number, :account_number,
        :account_number_confirmation, :account_type, :active, :_destroy, :id],
      availabilities_attributes: [:start_time_hr, :start_time_min, :end_time_hr, :end_time_min, :day, :id, :_destroy],
        user_courses_attributes: [:id, :course_id, :_destroy]
    ) rescue {}
    permitted_params.merge(profile_step: User::PROFILE_STEPS.call.rindex(step)) # Merge Current step in permitted params
  end

end
