class Admin::TutorsController < Admin::BaseController

  include Admin::InheritedResource

  # Used to send mail to intimate Tutor to complete profile.
  def intimate_profile_completion
    UserMailer.delay(queue: :mailer).intimation_profile_completion_email(resource.id, true)
    respond_to do |format|
      format.js {}
    end
  end

  # Method to set all Tutor as searchable.
  def mark_all_searchable
    respond_to do |format|
      if Tutor.active.not_searchable.update_all(searchable: true)
        Tutor.searchable.each do |tutor|
          Indexer.perform_async(:update,  tutor.class.to_s, tutor.id)
        end
        format.html {redirect_via_turbolinks_to admin_tutors_path, alert: 'All Active tutors are searchable. Please allow two to five minutes to complete the process in the background before they are visible in the Search Page.'}
      else
        format.js {redirect_to collection_path, alert: 'Unable to mark all tutors searchable.'}
      end
    end
  end

  # Method to update(mark) a Tutor as featured.
  def mark_featured
    respond_to do |format|
      if resource.update_attribute(:is_featured, '1')
        format.js {render 'referesh_datatable'}
      else
        format.js {redirect_to collection_path, alert: 'Unable to add in featured.'}
      end
    end
  end

  # Method to update(unmark) a Tutor as not featured.
  def unmark_featured
        respond_to do |format|
      if resource.update_attribute(:is_featured, '0')
        format.js {render 'referesh_datatable'}
      else
        format.js {redirect_to collection_path, alert: 'Unable to remove from featured.'}
      end
    end
  end

  # Method to update Tutor as searchable.
  def mark_searchable
    respond_to do |format|
      if !resource.is_registration_completed?
        format.js {redirect_to collection_path, alert: 'Unable to make searchable, User has not completed his profile.'}
      else
        if resource.update_attribute(:searchable, true)
          format.js {render 'referesh_datatable'}
        else
          format.js {redirect_to collection_path, alert: 'Unable to add in search.'}
        end
      end
    end
  end

  # Method to update Tutor as non-searchable.
  def unmark_searchable
    respond_to do |format|
      if resource.update_attribute(:searchable, false)
        format.js {render 'referesh_datatable'}
      else
        format.js {redirect_to collection_path, alert: 'Unable to remove from search.'}
      end
    end
  end

  # Method to export all Tutor in csv file containing its username and email.
  def export_tutors
    @tutors = User.where('type = "Tutor"').order('created_at desc')
    report = CSV.generate() do |csv|
      csv << ['Username', 'Email']
      @tutors.each do |t|
        username = t.name.nil? ? '-' : t.name
        email    = t.email
        csv << [username, email]
      end
    end

    send_data( report,
      :type => 'text/csv; charset=utf-8; header=present',
      :disposition => "attachment; filename=tutors_list.csv" )
  end

end
