require_relative '../global_context'

class PublicContext < GlobalContext

  def initialize
    super
    @users = {}
  end

  def visit_page(url, name = nil)
    @current_user = @users[name]
    visit url unless current_path == url
  end

  def given_user(name, account_type, num_schools)
    @users[name] = credentials[account_type][num_schools]
    @current_user = nil
  end

  def enter_login_credentials(credential_type)
    credentials = @current_user || credentials[credential_type]
    fill_in 'login-email-field', with: credentials['email']
    fill_in 'login-password-field', with: credentials['password']
  end

  def enter_support_login_credentials(credential_type)
    credentials = @current_user || credentials[credential_type]
    fill_in 'support-email-field', with: credentials['email']
    fill_in 'support-password-field', with: credentials['password']
  end

  def log_out_of_fedora
    within('.btn-group.tch-dropdown-group.dropup.dropdown') {
      find('.dropdown-toggle').click
    }
    expect(page).to have_content("Log Out")
    click_a_tag('Log Out')
  end

  private

  def create_login_page
    unless Page.find_by(name: 'Login')
      login_page = Page.create!(
        name: 'Login',
        url: '/login',
        active: true,
        title_text: 'login',
        description: 'login description',
        canonical_url: 'http://teachable.com/login',
        creator_id: 1
      )
      login_partial = Partial.create!(name: "Log In Form")
      PagePartial.create!(partial_id: login_partial.id, page_id: login_page.id, order: 1)
    end
  end

  def create_support_login_page
    unless Page.find_by(name: 'Support Login')
      support_login_page = Page.create!(
        name: 'Support Login',
        url: '/support-login',
        active: true,
        title_text: 'support login',
        description: 'support login description',
        canonical_url: 'http://teachable.com/support-login',
        creator_id: 1
      )
      support_login_partial = Partial.create!(name: "Support Log In")
      PagePartial.create!(partial_id: support_login_partial.id, page_id: support_login_page.id, order: 1)
    end
  end

  def create_signup_page
    unless Page.find_by(name: 'Create Account')
      signup_page = Page.create!(
        name: 'Create Account',
        url: '/create-account',
        active: true,
        title_text: 'create account',
        description: 'create account description',
        canonical_url: 'http://teachable.com/create-account',
        creator_id: 1
      )
      @page_partials ||= ['Sign Up Form', 'Single Page Checkout']
      signup_partial = Partial.create!(name: 'Sign Up Form')
      PagePartial.create!(partial_id: signup_partial.id, page_id: signup_page.id, order: @page_partials.find_index('Sign Up Form'))
      create_single_page_checkout_partial(signup_page)
    end
  end

  def create_forgot_password_page
    unless Page.find_by(name: 'Forgot Password')
      forgot_password_page = Page.create!(
        name: 'Forgot Password',
        url: '/forgot-password',
        active: true,
        title_text: 'forgot password',
        description: 'forgot password description',
        canonical_url: 'http://teachable.com/forgot-password',
        creator_id: 1
      )
      forgot_password_partial = Partial.create!(name: "Forgot Password Form")
      PagePartial.create!(partial_id: forgot_password_partial.id, page_id: forgot_password_page.id, order: 1)
    end
  end

  def create_single_page_checkout_partial(page)
    single_page_checkout_partial = Partial.create!(name: 'Single Page Checkout V2')

    testimonal_template = Template.create!({ name: 'Testimonial', plural: 'Testimonials' })
    school_template = Template.create!({ name: 'School', plural: 'Schools' })

    testimonal_objekt = Objekt.create!(template_id: testimonal_template.id)
    school_objekt = Objekt.create!(template_id: school_template.id)

    fields = ['School', 'Text']

    school_field = Field.create!({name: 'School', template_id: testimonal_objekt.template_id, field_type: 'object', order: fields.find_index('School')})
    text_field = Field.create!({name: 'Text', template_id: testimonal_objekt.template_id, field_type: 'text', char_limit: 500, order: fields.find_index('Text')})

    school_objekt_field = ObjektField.create!({objekt_id: testimonal_objekt.id, field_id: school_field.id, value: school_objekt.id})
    text_objekt_field = ObjektField.create!({objekt_id: testimonal_objekt.id, field_id: text_field.id, value: "Test quote"})

    create_partial_fields(single_page_checkout_partial.id, get_partial_fields['Single Page Checkout'])
    spc_page_partial = create_page_partial(page.id, single_page_checkout_partial.id, nil)
    add_value_to_single_page_checkout_fields(spc_page_partial)

    create_plans
    create_plan_features
  end

  def add_value_to_single_page_checkout_fields(spc_page_partial)
    values = get_spc_partial_field_values
    spc_page_partial.partial_fields.each do |field|
      value = values[field.field_type]
      PagePartialField.find_by(partial_field_id: field.id).update(value: value) if value
    end
  end

  def credentials
    {
      'MyTeachable' => {
        '0' => {
          'email' => 'peter.noschools@teachable.com',
          'password' => ENV['TEST_PETER_NOSCHOOLS_PW']
        },
        '1' => {
          'email' => 'peter@teachable.com',
          'password' => ENV['TEST_PETER_PW'],
          'plan_type' => 'Monthly-Professional'
        },
        'many' => {
          'email' => 'jackson@teachable.com',
          'password' => ENV['TEST_JACKSON_PW']
        }
      },
      'old' => {
        '1' => {
          'email' => 'donald.trump@teachable.com',
          'password' => ENV['TEST_DONALD_TRUMP_PW'],
          'plan_type' => 'Monthly-Professional'
        },
        'many' => {
          'email' => 'obama@teachable.com',
          'password' => ENV['TEST_OBAMA_PW'],
          'plan_type' => 'Monthly-Professional'
        },
      },
      'blank' => {
        'email' => '',
        'password' => ''
      },
      'incorrect' => {
        'email' => 'x',
        'password' => 'x'
      }
    }
  end

  def get_partial_fields
    {
      'Single Page Checkout' => {
          'dropdown' => {
            'field_names' => ['AB Test Active'],
            'char_limit' => '0',
            'mandatory' => 'true'
          },
          'object' => {
            'field_names' => ["Quote"],
            'char_limit' => '0',
            'mandatory' => 'false',
            'object_type' => 'Testimonial'
          },
          'number' => {
            'field_names' => ["Computer Padding Bottom", "Tablet Padding Bottom", "Mobile Padding Bottom"],
            'char_limit' => '0',
            'mandatory' => 'false'
          }
        }
      }
  end

  def get_spc_partial_field_values
    {
      'number' => '15'
    }
  end
end
