

$ = jQuery
context = window
context.JK ||= {};

class AffiliateProgram
  constructor: (@app) ->
    @logger = context.JK.logger
    @rest = new context.JK.Rest();
    @agreeBtn = null
    @disagreeBtn = null
    @entityForm = null
    @disagreeNotice = null
    @entityName = null
    @entityType = null
    @entityRadio = null
    @fieldEntityName = null
    @fieldEntityType = null
    @entityOptions = null

  removeErrors: () =>
    @fieldEntityName.removeClass('error').find('.error-info').remove();
    @fieldEntityType.removeClass('error').find('.error-info').remove();
    @entityOptions.removeClass('error').find('.error-info').remove();

  onRadioChanged: () =>
    @removeErrors()
    value = @page.find('input:radio[name="entity"]:checked').val()

    if value == 'individual'
      @entityForm.slideUp()
    else
      @entityForm.slideDown()

    return false


  onCreatedAffiliatePartner:(response) =>
    if response.partner_user_id?
      # this was an existing user, so tell them to go on in
      context.JK.Banner.show({buttons: [{name: 'GO TO AFFILIATE PAGE', href: '/client#/account/affiliatePartner'}], title: 'congratulations', html: 'Thank you for joining the JamKazam affiliate program!<br/><br/>You can visit the <a href="/client#/account/affiliatePartner">Affiliate Page</a> in your JamKazam Account any time to get links to share to refer users, and to view reports on affiliate activity levels.'})
    else
      context.JK.Banner.show({buttons: [{name: 'GO SIGNUP', href:'/signup?affiliate_partner_id=' + response.id}], title: 'congratulations', html: 'Thank you for joining the JamKazam affiliate program!<br/><br/>There is still one more step: you still need to create a user account on JamKazam, so that you can access your affiliate information.'})

  onFailedCreateAffiliatePartner: (jqXHR) =>
    if jqXHR.status == 422
      body = JSON.parse(jqXHR.responseText)
      if body.errors && body.errors.affiliate_partner && body.errors.affiliate_partner[0] == 'You are already an affiliate.'
        @app.notify({title:'Error', text:'You are already an affiliate.'})
      else
        @app.notifyServerError(jqXHR, 'Unable to Create Affiliate')
    else
      @app.notifyServerError(jqXHR, 'Unable to Create Affiliate')

  onAgreeClicked: () =>
    @removeErrors()

    value = @page.find('input:radio[name="entity"]:checked').val()

    error = false

    if value?
      if value == 'individual'
        entityType = 'Individual'
      else
        # insist that they fill out entity type info
        entityName = @entityName.val()
        entityType = @entityType.val()

        entityNameNotEmpty = !!entityName
        entityTypeNotEmpty = !!entityType

        if !entityNameNotEmpty
          @fieldEntityName.addClass('error').append('<div class="error-info">must be specified</div>')
          error = true

        if !entityTypeNotEmpty
          @fieldEntityType.addClass('error').append('<div class="error-info">must be specified</div>')
          error = true
    else
      @entityOptions.addClass('error')
      error = true

    unless error
      @rest.createAffiliatePartner({partner_name: entityName, entity_type:  entityType})
        .done(@onCreatedAffiliatePartner)
        .fail(@onFailedCreateAffiliatePartner)

    @disagreeNotice.hide  ('hidden')
    return false

  onDisagreeClicked: () =>
    @removeErrors()

    @disagreeNotice.slideDown('hidden')
    return false

  events:() =>
    @entityRadio.on('change', @onRadioChanged)
    @agreeBtn.on('click', @onAgreeClicked)
    @disagreeBtn.on('click', @onDisagreeClicked)

  initialize: () =>
    @page = $('body')
    @agreeBtn = @page.find('.agree-button')
    @disagreeBtn = @page.find('.disagree-button')
    @entityForm = @page.find('.entity-info')
    @disagreeNotice = @page.find('.disagree-text')
    @entityName = @page.find('input[name="entity-name"]')
    @entityType = @page.find('select[name="entity-type"]')
    @entityRadio = @page.find('input[name="entity"]')
    @fieldEntityName = @page.find('.field.entity.name')
    @fieldEntityType = @page.find('.field.entity.type')
    @entityOptions = @page.find('.entity-options')

    @events()

context.JK.AffiliateProgram =  AffiliateProgram