class BroadcastsController < ApplicationController
  layout "project"

  before_action :set_project
  before_action :build_broadcast, only: [:new, :create]
  before_action :set_broadcast, only: [:show, :destroy, :update]
  before_action :set_multi_view_broadcasts, only: [:show]

  def index
    @broadcasts = filtered_broadcasts.order_by_recent.paginate(page: params[:page])
  end

  def new
  end

  def create
    @broadcast.attributes = broadcast_params

    if @broadcast.save
      log_create_analytics
      redirect_to [@project, :broadcasts], notice: t(".notice")
    else
      render :new
    end
  end

  def show
    @conference_url = url_for [@broadcast.project, @broadcast, :zoom_meeting]
    @recordings = @broadcast.broadcast_recordings.order_by_recent.paginate(page: params[:page])
    @files = @broadcast.files.order("created_at DESC").paginate(page: params[:files_page])
    render layout: 'application'
  end

  def update
    @broadcast.update(broadcast_params)
    @files = @broadcast.files.order("created_at DESC").paginate(page: 1)

    pagination_content = ApplicationController.render html: helpers.will_paginate(@files, params: { active_tab: params[:active_tab], page: params[:page], active_files_tab: params[:active_files_tab] })
    BroadcastsChannel.broadcast_file_upload_updates(@broadcast, @files, pagination_content)
  end

  def destroy
    if @broadcast.destroy
      redirect_to [@project, :broadcasts], alert: t(".alert")
    else
      redirect_to [@project, :broadcasts], alert: t(".api_error")
    end
  end

  private

  def broadcast_params
    params.require(:broadcast).permit(:name, files: [])
  end

  def set_project
    @project = policy_scope(Project).find(params[:project_id])
  end

  def set_broadcast
    @broadcast = authorize policy_scope(Broadcast).find(params[:id])
  end

  def build_broadcast
    @broadcast = authorize @project.broadcasts.build
  end

  def broadcasts
    authorize policy_scope(@project.broadcasts)
  end

  def set_multi_view_broadcasts
    authorized_broadcasts = authorize policy_scope(Broadcast).where(id: params[:multi_view_ids]).order_by_recent
    @multi_view_broadcasts = authorized_broadcasts.map do |b|
      files_page = params[:files_page] if params[:active_files_tab] == b.token
      MultiViewBroadcast.new(b, params[:multi_view_ids], files_page)
    end
  end

  def filtered_broadcasts
    results = broadcasts

    if params[:query].present?
      results = results.search(params[:query])
    end

    results
  end

  def log_create_analytics
    TrackAnalyticsJob.perform_later(Current.user, Current.account, :track_create_live_stream, user_agent: request.user_agent, user_ip: request.remote_ip)
  end

  class MultiViewBroadcast
    include Rails.application.routes.url_helpers

    delegate_missing_to :@broadcast

    def initialize(broadcast, multi_view_ids, paginate_page)
      @broadcast = broadcast
      @multi_view_ids = multi_view_ids
      @paginate_page = paginate_page
    end

    def url
      project_broadcast_path(@broadcast.project, @broadcast, multi_view_ids: @multi_view_ids, locale: I18n.locale)
    end

    def files
      @broadcast.files.order("created_at DESC").paginate(page: @paginate_page)
    end

    def uid
      id
    end

    def self.model_name
      Broadcast.model_name
    end
  end
end
