class MobileAppApi::V1::RegistrationController < MobileAppApi::V1::BaseController
before_filter :login_required!, :except => [:sign_up]

  # Method to signup manually from mobile app i.e. create new user account with its mail Id.
  def sign_up
    params[:user].permit!
    user = User.new params[:user], as: :creator
    if user.save
      user.generate_auth_token
      render json: { status: 200, is_valid_user: user.valid?, user_info: user, auth_token: user.auth_token }
    else
      render json: { status: 400, error: "Sign up process failed.", is_valid_user: user.valid?, error_details: user.errors.full_messages }
    end
  end

  # Method to activate the user signup manually through clicking its email link.
  def update_billing_shipping_details
    result = User.update_all(
      {
        :billing_address_1 => params[:billing_address_1],
        :billing_address_2 => params[:billing_address_2],
        :billing_city => params[:billing_city],
        :billing_state => params[:billing_state],
        :billing_zipcode => params[:billing_zipcode],
        :shipping_address_1 => params[:shipping_address_1],
        :shipping_address_2 => params[:shipping_address_2],
        :shipping_city => params[:shipping_city],
        :shipping_state => params[:shipping_state],
        :shipping_zipcode => params[:shipping_zipcode]
      },
      {:id => user[0].id}
    )
    if result == 1
      render json: { status: 200 }
    else
      render json: { status: 400, error: "Update failed." }
    end
  end

  # Method to get credit card details of the current user.
  def get_credit_cards
    credit_card_details = @user.credit_cards
    if credit_card_details
      render json: { status: 200, credit_card_details: credit_card_details.map{|c| c.attributes.except('uri', 'created_at', 'updated_at', 'university_id')}}
    else
      render json: { status: 401, error: "Credit card for user #{@user.name} not found." }
    end
  end

  # Method to add credit card details for the current user.
  def add_credit_card
    @credit_card = @user.credit_cards.build(credit_card_params)
    @credit_card.active = true
    if @credit_card.save
      render json: {status: 200, success: "Successfully created card."}
    else
      render json: {status: 401, error: "Invalid card details."}
    end
  end

  # Method to activate the credit card when added by user.
  def activate_credit_card
    @credit_card = @user.credit_cards.find(params[:id])
    @credit_card.active = true
    if @credit_card.save
      render json: {status: 200, :success => "Card successfully activated."}
    else
      render json: {status: 401, error: "Invalid card details."}
    end
  end

  # Method to remove the credit card from user account.
  def remove_credit_card
    @credit_card = @user.credit_cards.find(params[:id])
    if @credit_card.destroy
      render json: {status: 200, :success => "Successfully destroyed card."}
    else
      render json: {status: 401, error: "Can't destroy card."}
    end
  end

  # Method to upadate and create bank account for current user.
  def update_bank_details
    if Payment.create_new_bank params[:payment], @user
      render json: {status: 200, :success => "Successfully created bank account."}
    else
      render json: {status: 400, error: "Invalid bank details."}
    end
  end

  #"payment" =>
  # {
  #   "name"=>"Kiran Sarode",
  #   "bank_code"=>"021000021",
  #   "account_number"=>"9900000002",
  #   "type"=>"SAVINGS"
  # }

  # Method to find all courses of subjects with subject Id.
  def get_courses_for_subject
    subject = Subject.find(params[:subject_id])
    if !subject.nil?
      courses = subject.courses
      render json: {status: 200, :courses => courses, :total_courses => courses.length}
    else
      render json: {status: 400, error: "Subject with subject_id #{params[:subject_id]} not found."}
    end
  end

  # Method to add course to tutor account.
  def add_courses
    tutor = Tutor.where(auth_token: params[:auth_token])
    if !tutor.nil? && !tutor[0].nil?
      subject_tutored = SubjectsTutor.where(:subject_id => params[:subject_id], :tutor_id => tutor[0].id)
      if(!subject_tutored.nil? && !subject_tutored[0].nil?)
      else
        st = SubjectsTutor.new(:subject_id => params[:subject_id], :tutor_id => tutor[0].id)
        st.save
      end
      if params[:selected_course_ids].kind_of?(Array)
        params[:selected_course_ids].each do |course_id|
          begin
            course = Course.find course_id
          rescue => e
            render json: {status: 400, error: "Course with id #{course_id} not found."}
            return
          end
          if course
            Tutor.find(tutor[0].id).courses << course unless Tutor.find(tutor[0].id).courses.include?(course)
          else
            render json: {status: 400, error: "Course with id #{course_id} not found."}
            return
          end
        end
        render json: {status: 200}
      else
        render json: {status: 400, error: "Array of course ids not found in parameters, please check course id array and try again."}
        return
      end
    else
      render json: {status: 400, error: "Tutor with auth_token #{params[:auth_token]} not found."}
    end
  end

  # To update user profile
  def update_profile
    if @user.type == "Tutor"
      if user[0].update_attributes params[user[0].type_to_symbol], as: :creator
        render json: { status: 200 }
      else
        render json: { status: 400, error: "Update failed." }
      end
    else
      if user[0].update_attributes params[user[0].type_to_symbol], as: :creator
        render json: { status: 200 }
      else
        render json: { status: 400, error: "Update failed." }
      end
    end
  end

  private
    def credit_card_params
      params.required(:credit_card).permit(:number, :name, :expiry_date, :cvv, :active, :id, :_destroy)
    end

end
