class LandingsController < ApplicationController

  include LandingsHelper

  respond_to :html

  before_filter :posa_http_basic_auth, only: [:posa_activation]

  def watch_bands
    @promo_buzz = PromoBuzz.active

    if Rails.application.config.use_promos_on_homepage
      @promo_latest = PromoLatest.active
    else
      @promo_latest, start = Feed.index(nil, limit: 10)
    end

    page = params['p'].to_i

    if page == 2
      render 'watch_bands_2', layout: 'web'
    elsif page == 3
      render 'watch_bands_3', layout: 'web'
    else
      render 'watch_bands_1', layout: 'web'
    end
  end

  def watch_overview_kick
    render 'watch_kick', layout: 'web'
  end

  def watch_overview_kick2
    render 'watch_kick_2', layout: 'web'
  end

  def watch_overview_kick3
    render 'watch_kick_3', layout: 'web'
  end

  def watch_overview_kick4
    render 'watch_kick_4', layout: 'web'
  end


  def press_release_music_festival
    render "music_festival_2020", layout: 'basic'
  end

  def watch_overview
    @promo_buzz = PromoBuzz.active

    if Rails.application.config.use_promos_on_homepage
      @promo_latest = PromoLatest.active
    else
      @promo_latest, start = Feed.index(nil, limit: 10)
    end

    page = params['p'].to_i

    if page == 2
      render 'watch_overview_2', layout: 'web'
    elsif page == 3
      render 'watch_overview_3', layout: 'web'
    else
      render 'watch_overview_1', layout: 'web'
    end
  end

  def watch_bands_tight
    render 'watch_bands_tight', layout: 'web'
  end

  def watch_overview_tight
    render 'watch_overview_tight', layout: 'web'
  end

  def jam_class_students
    enable_olark
    @no_landing_tag = true
    @landing_tag_play_learn_earn = true
    @show_after_black_bar_border = true
    @free = false

    package_name = params['utm-teachers']
    if package_name
      package = TestDrivePackage.find_by_name(package_name)

      if package
        package_data = {}
        package_data[:id] = package.id
        package_data[:name] = package.name
        package_data[:package_type] = package.package_type
        teachers = []
        package.test_drive_package_teachers.each do |package_teacher|
          teacher = package_teacher.user
          teachers.push({id: teacher.id, name: teacher.name, first_name: teacher.first_name, last_name: teacher.last_name, biography: teacher.teacher.short_bio, photo_url: teacher.photo_url})
        end
        package_data[:teachers] = teachers

        @package_data = package_data
      end

    end

    @page_data = {free: @free, package: @package_data}

    render 'jam_class_students', layout: 'web'
  end

  def jam_class_students_free
    enable_olark
    @no_landing_tag = true
    @landing_tag_play_learn_earn = true
    @show_after_black_bar_border = true
    @free = true

    @page_data = {free: @free}

    render 'jam_class_students', layout: 'web'
  end

  def jam_class_teachers
    enable_olark
    @no_landing_tag = true
    @landing_tag_play_learn_earn = true
    @show_after_black_bar_border = true
    render 'jam_class_teachers', layout: 'web'
  end

  def jam_class_affiliates
    enable_olark
    @no_landing_tag = true
    @landing_tag_play_learn_earn = true
    @show_after_black_bar_border = true
    render 'jam_class_affiliates', layout: 'web'
  end

  def jam_class_schools
    enable_olark
    @no_landing_tag = true
    @landing_tag_play_learn_earn = true
    @show_after_black_bar_border = true
    render 'jam_class_schools', layout: 'web'
  end

  def jam_class_education
    enable_olark
    @no_landing_tag = true
    @landing_tag_play_learn_earn = true
    @show_after_black_bar_border = true
    render 'jam_class_education', layout: 'web'
  end

  def jam_class_retailers
    enable_olark
    @no_landing_tag = true
    @landing_tag_play_learn_earn = true
    @show_after_black_bar_border = true
    render 'jam_class_retailers', layout: 'web'
  end

  def individual_jamtrack

    enable_olark
    @no_landing_tag = true
    @landing_tag_play_learn_earn = true
    @show_after_black_bar_border = true
    instrument_id = nil
    instrument_name = nil
    instrument_count = 0
    if params[:instrument]
      instrument = params[:instrument].downcase.sub('-', ' ')
      instrument = Instrument.find_by_id(instrument)
      instrument_id = instrument.id if instrument
      instrument_name = instrument.description
      query, next_ptr, instrument_count = JamTrack.index({instrument: instrument_id}, current_user)
    end
    @jam_track = JamTrack.find_by_slug(params[:plan_code])
    @jam_track = JamTrack.find_by_plan_code("jamtrack-" + params[:plan_code]) unless @jam_track

    if @jam_track.nil?
      redirect_to '/client/#jamtrack'
      return
    end

    band_jam_track_count = @jam_track.band_jam_track_count
    jam_track_count = JamTrack.count
    @title = individual_jamtrack_title(false, params[:generic], @jam_track)
    @description = individual_jamtrack_desc(false, params[:generic], @jam_track)
    @page_data = {jam_track: @jam_track, all_track_count: jam_track_count, band_track_count: band_jam_track_count, band: false, generic: params[:generic], instrument: instrument_name, instrument_id: instrument_id, instrument_count: instrument_count}
    gon.jam_track_plan_code = @jam_track.plan_code if @jam_track
    gon.generic = params[:generic]
    gon.instrument_id = instrument_id
    gon.env = Rails.env
    render 'individual_jamtrack', layout: 'web'
  end


  def individual_jamtrack_band
    enable_olark
    @no_landing_tag = true
    @landing_tag_play_learn_earn = true
    @show_after_black_bar_border = true
    @jam_track = JamTrack.find_by_slug(params[:plan_code])
    @jam_track = JamTrack.find_by_plan_code("jamtrack-" + params[:plan_code]) unless @jam_track

    if @jam_track.nil?
      redirect_to '/client/#jamtrack'
      return
    end

    band_jam_track_count = @jam_track.band_jam_track_count
    jam_track_count = JamTrack.count
    @title = individual_jamtrack_title(true, params[:generic], @jam_track)
    @description = individual_jamtrack_desc(true, params[:generic], @jam_track)
    @page_data = {jam_track: @jam_track, all_track_count: jam_track_count, band_track_count: band_jam_track_count, band: true, generic: params[:generic]}
    gon.jam_track_plan_code = @jam_track.plan_code if @jam_track
    gon.generic = params[:generic]
    render 'individual_jamtrack', layout: 'web'
  end

  def individual_jamtrack_v2
    @no_landing_tag = true
    instrument_id = nil
    instrument_name = nil
    instrument_count = 0
    if params[:instrument]
      instrument = params[:instrument].downcase.sub('-', ' ')
      instrument = Instrument.find_by_id(instrument)
      instrument_id = instrument.id if instrument
      instrument_name = instrument.description
      query, next_ptr, instrument_count = JamTrack.index({instrument: instrument_id}, current_user)
    end
    @jam_track = JamTrack.find_by_slug(params[:plan_code])
    @jam_track = JamTrack.find_by_plan_code("jamtrack-" + params[:plan_code]) unless @jam_track
    band_jam_track_count = @jam_track.band_jam_track_count
    jam_track_count = JamTrack.count
    @title = individual_jamtrack_title(false, params[:generic], @jam_track)
    @description = individual_jamtrack_desc(false, params[:generic], @jam_track)
    @page_data = {jam_track: @jam_track, all_track_count: jam_track_count, band_track_count: band_jam_track_count, band: false, generic: params[:generic], instrument: instrument_name, instrument_id: instrument_id, instrument_count: instrument_count}
    gon.jam_track_plan_code = @jam_track.plan_code if @jam_track
    gon.generic = params[:generic]
    gon.instrument_id = instrument_id
    render 'individual_jamtrack_v2', layout: 'web'
  end


  def individual_jamtrack_band_v2
    @no_landing_tag = true
    @jam_track = JamTrack.find_by_slug(params[:plan_code])
    @jam_track = JamTrack.find_by_plan_code("jamtrack-" + params[:plan_code]) unless @jam_track
    band_jam_track_count = @jam_track.band_jam_track_count
    jam_track_count = JamTrack.count
    @title = individual_jamtrack_title(true, params[:generic], @jam_track)
    @description = individual_jamtrack_desc(true, params[:generic], @jam_track)
    @page_data = {jam_track: @jam_track, all_track_count: jam_track_count, band_track_count: band_jam_track_count, band: true, generic: params[:generic]}
    gon.jam_track_plan_code = @jam_track.plan_code if @jam_track
    gon.generic = params[:generic]
    render 'individual_jamtrack_v2', layout: 'web'
  end

  def individual_jamtrack_v1
    @no_landing_tag = true
    @show_cta_free_jamtrack = true
    @jam_track = JamTrack.find_by_plan_code("jamtrack-" + params[:plan_code])
    gon.jam_track_plan_code = params[:plan_code] ? "jamtrack-" + params[:plan_code] : nil
    gon.generic = params[:generic]
    render 'individual_jamtrack_v1', layout: 'web'
  end

  def individual_jamtrack_band_v1
    @no_landing_tag = true
    @show_cta_free_jamtrack = true
    @jam_track = JamTrack.find_by_plan_code("jamtrack-" + params[:plan_code])
    gon.jam_track_plan_code = params[:plan_code] ? "jamtrack-" + params[:plan_code] : nil

    render 'individual_jamtrack_band_v1', layout: 'web'
  end

  def product_jamblaster
    enable_olark
    render 'product_jamblaster', layout: 'web'
  end

  def product_platform
    enable_olark
    @no_landing_tag = true
    @landing_tag_play_learn_earn = true
    @show_after_black_bar_border = true
    render 'product_platform', layout: 'web'
  end

  def product_jamtracks
    enable_olark
    gon.generic = true
    gon.just_previews = true
    jam_track = JamTrack.select('plan_code').where(plan_code: Rails.application.config.nominated_jam_track).first
    unless jam_track
      jam_track = JamTrack.first
    end

    gon.jam_track_plan_code = jam_track.plan_code if jam_track
    render 'product_jamtracks', layout: 'web'
  end

  def affiliate_program
    render 'affiliate_program', layout: 'web'
  end

  def redeem_giftcard
    @no_landing_tag = true
    @landing_tag_play_learn_earn = true
    render 'redeem_giftcard', layout: 'web'
  end

  def account_activate
    @no_landing_tag = true
    @landing_tag_play_learn_earn = true
    render 'account_activate', layout: 'web'
  end

  def buy_gift_card
    @no_landing_tag = true
    @landing_tag_play_learn_earn = true
    @show_after_black_bar_border = true
    @jam_track = JamTrack.find_by_slug('elton-john-rocket-man')
    @jam_track = JamTrack.first unless @jam_track

    instrument_id = nil
    instrument_name = nil
    instrument_count = 0

    band_jam_track_count = @jam_track.band_jam_track_count
    jam_track_count = JamTrack.count
    @title = individual_jamtrack_title(false, params[:generic], @jam_track)
    @description = individual_jamtrack_desc(false, params[:generic], @jam_track)
    @page_data = {jam_track: @jam_track, all_track_count: jam_track_count, band_track_count: band_jam_track_count, band: false, generic: params[:generic], instrument: instrument_name, instrument_id: instrument_id, instrument_count: instrument_count}
    gon.jam_track_plan_code = @jam_track.plan_code if @jam_track
    gon.generic = params[:generic]
    gon.instrument_id = instrument_id

    render 'buy_gift_card', layout: 'web'
  end

  def school_student_register
    @no_landing_tag = true
    @landing_tag_play_learn_earn = true

    @school = School.find_by_id(params[:id])

    if @school.nil?
      redirect_to '/signup'
      return
    end

    @title = 'Become a student with ' + @school.name
    @description = "Using JamKazam, take online lessons with the music school " + @school.name
    @preview = !params[:preview].nil?
    @invitation = SchoolInvitation.find_by_invitation_code(params[:invitation_code]) if params[:invitation_code]
    defaultEmail = ''
    if @invitation
      defaultEmail = @invitation.email
    end


    @page_data = {school: @school, invitation_code: params[:invitation_code], defaultEmail: defaultEmail, preview: @preview}
    render 'school_student_register', layout: 'web'
  end

  def school_teacher_register
    @no_landing_tag = true
    @landing_tag_play_learn_earn = true
    @school = School.find_by_id(params[:id])

    if @school.nil?
      redirect_to '/signup'
      return
    end


    @title = 'Become a teacher with ' + @school.name
    @description = "Using JamKazam, teach online lessons with the music school " + @school.name
    @preview = !params[:preview].nil?


    @invitation = SchoolInvitation.find_by_invitation_code(params[:invitation_code]) if params[:invitation_code]
    defaultEmail = ''
    if @invitation
      defaultEmail = @invitation.email
    end


    @page_data = {school: @school, invitation_code: params[:invitation_code], defaultEmail: defaultEmail, preview: @preview}
    render 'school_teacher_register', layout: 'web'
  end

  def school_teacher_list
    @no_landing_tag = true
    @landing_tag_play_learn_earn = true
    @school = School.find_by_id(params[:id])

    if @school.nil?
      redirect_to '/signup'
      return
    end


    @title = 'Our teachers at ' + @school.name
    @description = "View the profile and pick a teacher right for you from " + @school.name
    @preview = !params[:preview].nil?

    @show_approved_only = true
    # isn't working. totaly hack:
    @school.teachers = @school.approved_teachersL
    schoolRabl = Rabl::Renderer.json(@school, 'api_schools/show')

    @page_data = {school: @school, preview: @preview, rabl: schoolRabl}
    render 'school_teacher_list', layout: 'web'
  end


  def retailer_teacher_register
    @no_landing_tag = true
    @landing_tag_play_learn_earn = true
    @retailer = Retailer.find_by_id(params[:id])

    if @retailer.nil?
      redirect_to '/signup'
      return
    end


    @title = 'Become a teacher with ' + @retailer.name
    @description = "Using JamKazam, teach online lessons with " + @retailer.name
    @preview = !params[:preview].nil?

    @invitation = RetailerInvitation.find_by_invitation_code(params[:invitation_code]) if params[:invitation_code]
    defaultEmail = ''
    if @invitation
      defaultEmail = @invitation.email
    end


    @page_data = {retailer: @retailer, invitation_code: params[:invitation_code], defaultEmail: defaultEmail, preview: @preview}
    render 'retailer_teacher_register', layout: 'web'
  end

  def posa_activation
    @no_landing_tag = true
    @landing_tag_play_learn_earn = true
    @retailer = Retailer.find_by_slug(params[:slug])

    if @retailer.nil?
      redirect_to '/signup'
      return
    end


    @page_data = {retailer: @retailer, has_teachers: @retailer.teachers.count > 0}
    render 'posa_activation', layout: 'web'
  end

  def simple_jamtracks
    @description = 'JamTracks'
    @no_landing_tag = true
    @landing_tag_play_learn_earn = false
    @responsive = true

    render 'simple_jamtracks', layout: 'web'
  end

  def simple_jamclass
    @description = 'JamClass'
    @no_landing_tag = true
    @landing_tag_play_learn_earn = false
    @responsive = true

    render 'simple_jamclass', layout: 'web'
  end


  def amazon_lessons_promo_1
    @error = params[:error]
    @code = params[:code]
    @description = 'Redeem Your Lessons Code From Amazon'
    @responsive = true

    render 'amazon_lessons_promo_1', layout: 'basic'
  end

  def amazon_lessons_promo_1_post

    code = params[:code]
    @code = code

    card = PosaCard.find_by_amazon_code(code)

    if card
      if card.claimed_at
        @error = 'Code already claimed'
        render 'amazon_lessons_promo_1', layout: 'basic'
        return
      else
        redirect_to '/account/activate/signup?code=' + code
        return
      end

    else
      @error = 'Code not valid'
      render 'amazon_lessons_promo_1', layout: 'basic'
    end
  end

  def amazon_lessons_promo_2
    @description = 'Redeem Your Lessons Code From Amazon'
    @responsive = true
    @code = params[:code]

    if @code.nil? || @code == ''
      redirect_to action: :amazon_lessons_promo_1
      return
    end

    card = PosaCard.find_by_amazon_code(@code)

    if card
      if card.claimed_at
        @error = 'Code already claimed'
        redirect_to action: :amazon_lessons_promo_1, error: @error
        return
      else

      end
    else
      @error = 'Code not valid'
      redirect_to action: :amazon_lessons_promo_1, error: @error
      return
    end


    render 'amazon_lessons_promo_2', layout: 'basic'
  end

  def amazon_lessons_promo_2_post

    code = params[:code]

    @code = code
    card = PosaCard.find_by_amazon_code(code)

    if card.nil?
      @error = 'Code not valid'
      redirect_to action: :amazon_lessons_promo_1, error: @error
      return
    end

    if card.claimed_at
      @error = 'Code already claimed'
      redirect_to action: :amazon_lessons_promo_1, error: @error
      return
    end

    if @current_user

      card.claim(current_user)

      if card.errors.any?
        first = card.errors.first
        @error = "#{first[0].to_s.humanize} #{first[1]}"
        render 'amazon_lessons_promo_2', layout: 'basic'
      else

        @current_user.update_timezone(current_timezone)
        body = "Name: #{@current_user.name}\n"
        body << "Email: #{@current_user.email}\n"
        body << "Admin: #{@current_user.admin_onboarding_url}\n"
        body << "Package Details: \n"
        body << "        Package: #{card.lesson_package_type.id}\n"
        body << "        Credits: #{card.credits}\n"
        body << "        Code: #{card.code}\n"

        AdminMailer.jamclass_alerts({subject: "#{@current_user.email} just applied lesson credits to their account", body: body}).deliver_now
        redirect_to '/account/activate/done'
      end

    else


      @first = params[:first]
      if !@first || @first == ''
        @error = 'Please specify first name'
        render 'amazon_lessons_promo_2', layout: 'basic'
        return
      end

      @instrument = params[:instrument]
      if @instrument.nil? || @instrument == ''
        @error = 'Please select an instrument'
        render 'amazon_lessons_promo_2', layout: 'basic'
        return
      end

      @email = params[:email]
      @password = params[:password]
      terms_of_service = params[:terms].nil? || params[:terms] == "0" ? false : true
      under_13 = params[:under_13].nil? || params[:under_13] == "0" ? false : true

      @terms = terms_of_service ? "checked" : nil
      @under13 = under_13 ? "checked" : nil

      # try to signup now
      @user = UserManager.new.signup(remote_ip: request.remote_ip,
                                     email: params[:email],
                                     password: params[:password],
                                     password_confirmation: params[:password],
                                     gift_card: @code,
                                     terms_of_service: terms_of_service,
                                     under_13: under_13,
                                     skip_recaptcha: true,
                                     musician: true,
                                     timezone: current_timezone,
                                     first_name: @first,
                                     instruments:  [{:instrument_id => @instrument, :proficiency_level => 1, :priority => 1}])
      if @user.errors.any?
        first = @user.errors.first
        @error = "#{first[0].to_s.humanize} #{first[1]}"
        render 'amazon_lessons_promo_2', layout: 'basic'
        return
      else
        sign_in @user

        new_user(@user, nil) # sets a cookie used for GA analytics (one-time new user stuff in JavaScript)
        redirect_to '/account/activate/done'
        return
      end
    end
  end

  def amazon_lessons_promo_3
    @description = 'Redeem Your Lessons Code From Amazon - Done!'
    @responsive = true

    render 'amazon_lessons_promo_3', layout: 'basic'
  end


  def amazon_offer_splash
    @description = 'Redeem Your Lessons Code From Amazon - Start Here!'
    @responsive = true

    render 'amazon_promo_splash', layout: 'basic'
  end
end

