require 'google_client'
class UserManager < BaseManager

  def initialize(options={})
    super(options)
    @log = Logging.logger[self]
    @google_client = GoogleClient.new()
  end

  def get_google_client
    @google_client
  end

  # Note that almost everything can be nil here. This is because when users sign up via social media,
  # we don't know much about them.
  def signup(options)
    remote_ip = options[:remote_ip]
    first_name = options[:first_name]
    last_name = options[:last_name]
    email = options[:email]
    password = options[:password]
    password_confirmation = options[:password_confirmation]
    terms_of_service = options[:terms_of_service]
    instruments = options[:instruments]
    birth_date = options[:birth_date]
    location = options[:location]
    musician = options[:musician]
    photo_url = options[:photo_url]
    invited_user = options[:invited_user]
    fb_signup = options[:fb_signup]
    signup_confirm_url = options[:signup_confirm_url]
    affiliate_referral_id = options[:affiliate_referral_id]
    any_user = options[:any_user]
    signup_hint = options[:signup_hint]
    affiliate_partner = options[:affiliate_partner]
    gift_card = options[:gift_card]
    student = options[:student]
    teacher = options[:teacher]
    school_invitation_code = options[:school_invitation_code]
    school_id = options[:school_id]
    retailer_invitation_code = options[:retailer_invitation_code]
    retailer_id = options[:retailer_id]
    retailer_interest = options[:retailer_interest]
    school_interest = options[:school_interest]
    education_interest = options[:education_interest]
    origin = options[:origin]
    test_drive_package = options[:test_drive_package]
    under_13 = options[:under_13]
    timezone = options[:timezone]
    platform_instructor = options[:platform_instructor]
    license_start = options[:license_start]
    license_end = options[:license_end]
    import_source = options[:import_source]
    desired_plan_code = options[:desired_plan_code]

    recaptcha_failed = false
    unless options[:skip_recaptcha] # allow callers to opt-of recaptcha
      recaptcha_failed = fb_signup ? false : !@google_client.verify_recaptcha(options[:recaptcha_response])
    end

    user = User.new

    # check if we have disabled open signup for this site.   open == invited users can still get in
    if !SampleApp::Application.config.signup_enabled  && invited_user.nil?
      raise JamPermissionError, "Signups are currently disabled"
    end

    loc = GeoIpLocations.lookup(remote_ip)
    # there are three cases here: if location is missing, we'll auto set the city, etc. from
    # the ip address; if location is present, empty or not empty, we'll set the city, etc. from
    # what is present in location. we should NOT normally default city, etc. for the user, they
    # own it, they may want it to be unspecified, that is their right.
    unless location.nil?
      loc[:city] = location[:city]
      loc[:state] = location[:state]
      loc[:country]  = location[:country]
    end

    # sends email to email account for confirmation
    user = User.signup(first_name: first_name,
                        last_name: last_name,
                        email: email,
                        password: password,
                        password_confirmation: password_confirmation,
                        terms_of_service: terms_of_service,
                        location: loc,
                        instruments: instruments,
                        birth_date: birth_date,
                        musician: musician,
                        photo_url: photo_url,
                        recaptcha_failed: recaptcha_failed,
                        invited_user: invited_user,
                        fb_signup: fb_signup,
                        signup_confirm_url: signup_confirm_url,
                        affiliate_referral_id: affiliate_referral_id,
                        any_user: any_user,
                        signup_hint: signup_hint,
                        affiliate_partner: affiliate_partner,
                        gift_card: gift_card,
                        student: student,
                        teacher: teacher,
                        school_invitation_code: school_invitation_code,
                        school_id: school_id,
                        retailer_invitation_code: retailer_invitation_code,
                        retailer_id: retailer_id,
                        retailer_interest: retailer_interest,
                        school_interest: school_interest,
                        education_interest: education_interest,
                        origin: origin,
                        test_drive_package: test_drive_package,
                        under_13: under_13,
                        timezone: timezone,
                        platform_instructor: platform_instructor,
                        license_start: license_start,
                        license_end: license_end,
                        import_source: import_source,
                        desired_plan_code: desired_plan_code)
    user
  end

  def signup_confirm(signup_token, remote_ip=nil)
    begin
      user = User.signup_confirm(signup_token)
      user.location = GeoIpLocations.lookup(remote_ip) if remote_ip
    rescue ActiveRecord::RecordNotFound
      user = nil
    end

    return user
  end

end
