module WebinarActions
  def given_webinar(name)
    unless Template.find_by_name('Webinar Host/Inviter')
      template = Template.create!(
        name: 'Webinar Host/Inviter',
        plural: 'Webinar Hosts/Inviters'
      )

      unless Template.find_by_name('Webinar Bonus Bundle')
        Template.create!(
          name: 'Webinar Bonus Bundle',
          plural: 'Webinar Bonus Bundles'
        )
      end

      unless Template.find_by_name('Webinar Download')
        Template.create!(
          name: 'Webinar Download',
          plural: 'Webinar Downloads'
        )
      end

      name_field = Field.create!(
        template_id: template.id,
        name: 'Name',
        field_type: 'text',
        mandatory: true,
        char_limit: 100,
        order: 0
      )

      title_field = Field.create!(
        template_id: template.id,
        name: 'Title',
        field_type: 'text',
        mandatory: false,
        char_limit: 100,
        order: 1
      )

      image_field = Field.create!(
        template_id: template.id,
        name: 'Image',
        field_type: 'image',
        mandatory: false,
        order: 2
      )

      # Cameron Webinar Host/Inviter Objekt

      cameron_objekt = Objekt.create!(
        template_id: template.id
      )

      ObjektField.create!(
        field_id: name_field.id,
        objekt_id: cameron_objekt.id,
        value: 'Cameron'
      )

      ObjektField.create!(
        field_id: title_field.id,
        objekt_id: cameron_objekt.id,
        value: 'Head of Sales, Teachable'
      )

      cameron_image = Upload.create!(
        url: 'https://cdn.filepicker.io/api/file/U59TcHySViLDEpAS2iqL',
        category: 'object',
        uploaded_by: 1,
        upload_type: 'image'
      )

      ObjektField.create!(
        field_id: image_field.id,
        objekt_id: cameron_objekt.id,
        value: cameron_image.id
      )

      # Jess Webinar Host/Inviter Objekt

      jess_objekt = Objekt.create!(
        template_id: template.id
      )

      ObjektField.create!(
        field_id: name_field.id,
        objekt_id: jess_objekt.id,
        value: 'Jess'
      )

      ObjektField.create!(
        field_id: title_field.id,
        objekt_id: jess_objekt.id,
        value: 'Partnerships'
      )

      jess_image = Upload.create!(
        url: 'https://cdn.filepicker.io/api/file/iSFIVFOYRAaVoNPuqI9O',
        category: 'object',
        uploaded_by: 1,
        upload_type: 'image'
      )

      ObjektField.create!(
        field_id: image_field.id,
        objekt_id: jess_objekt.id,
        value: jess_image.id
      )
    end

    unless Webinar.find_by(name: name)
      name = name.downcase
      webinar = Webinar.create!(
        id: DateTime.now.to_i,
        name: name.capitalize,
        title: name.capitalize,
        url: name,
        time: DateTime.parse('1/1/2030'),
        recurring: false,
        recurring_info: '',
        recurrence: nil,
        bullet_points:"- Yay webinar!\n- Another bullet point",
        header: 'Teachable Quickstart Webinar',
        subheader: 'Learn the exact steps you can take to create your own professional online school and start making money',
        thank_you_video_id: 'fcjtl0zjw3',
        upgrade_timeframe: 'monthly'
      )
      webinar.create_in_firebase
      self.show_upgrade_button
    end
  end

  def create_user(name: 'someone else', color: '#000000', blocked: false)
    webinar = Webinar.first.id
    firebase = Firebase::Client.new(Webinar.get_firebase_url, Webinar.firebase_auth_json)
    new_user = {
      name: name,
      color: color,
      blocked: blocked,
    }
    response = firebase.push("#{webinar}/users", new_user)
    expect(response.code).to eql(200)
    user_id = response.body['name']
    firebase.update("#{webinar}/users/#{user_id}", {id: user_id})
    @firebase_user = new_user.merge({ id: user_id })
  end

  def send_message(user, message, blocked = false)
    webinar = Webinar.first.id
    firebase = Firebase::Client.new(Webinar.get_firebase_url, Webinar.firebase_auth_json)
    message_json = {
      "color" => user[:color],
      "date-created" => DateTime.now.to_i,
      "message-blocked" => user[:blocked] || blocked,
      "name" => user[:name],
      "text" => ERB::Util.html_escape(message),
      "user" => user[:id],
      "user-blocked" => user[:blocked] || false,
    }
    msg = firebase.push("#{webinar}/messages", message_json)
    expect(msg.code).to eql(200)
    msg
  end

  def show_upgrade_button
    webinar = Webinar.first.id
    firebase = Firebase::Client.new(Webinar.get_firebase_url, Webinar.firebase_auth_json)
    firebase.update("#{webinar}/controls", {showUpgradeButton: true})
  end
end
