# frozen_string_literal: true

# Generic Files Controller
#
# @author Robert olendorf
class GenericFilesController < ApplicationController
  before_action :load_project_owner
  before_action :load_project
  before_action :load_generic_file, except: [:index, :create, :new]
  
  def show
    authorize @generic_file
    
    add_breadcrumb @project_owner.user_name, user_projects_path(@project_owner.user_name)
    add_breadcrumb @project.name, user_project_path(@project_owner.user_name, @project.name)
    add_breadcrumb @generic_file.file.file.filename, user_project_generic_file_path(@project_owner.user_name, @project.name, @generic_file.id)
  end
  
  def edit
    authorize @generic_file
    
    add_breadcrumb @project_owner.user_name, user_projects_path(@project_owner.user_name)
    add_breadcrumb @project.name, user_project_path(@project_owner.user_name, @project.name)
    add_breadcrumb @generic_file.file.file.filename, user_project_generic_file_path(@project_owner.user_name, @project.name, @generic_file.id)
  end
  
  def new
    authorize @project, :update?
    
    
    add_breadcrumb @project_owner.user_name, user_projects_path(@project_owner.user_name)
    add_breadcrumb @project.name, user_project_path(@project_owner.user_name, @project.name)
    @generic_file = GenericFile.new
  end
  
  def create
    authorize @project, :update?
    
    @generic_file = GenericFile.new(generic_file_params)
    
    if @generic_file.save
      @generic_file.created_by = current_user
      @project.generic_files << @generic_file
      flash[:notice] = t('.success', filename: @generic_file.file.file.filename)
      redirect_to user_project_generic_file_path(@project_owner.user_name, @project.name, @generic_file.id)
    else
      flash[:error] = t('.failure', messages: @generic_file.errors.messages.map { |key, message| "#{key}: #{message.first}" })
      redirect_to new_user_project_generic_file_path(@project_owner.user_name, @project.name)
    end
  end
  
  def update
    authorize @generic_file
    
    if @generic_file.update_attributes(generic_file_params)
      @generic_file.updated_by = current_user
      @generic_file.save
      flash[:notice] = t('.success', filename: @generic_file.file.file.filename)
      redirect_to user_project_generic_file_path(@project_owner.user_name, @project.name, @generic_file.id)
    else
      @generic_file.reload
      flash[:error] = t('.failure', messages: @generic_file.errors.messages.map { |key, message| "#{key}: #{message.first}" })
      redirect_to edit_user_project_generic_file_path(@project_owner.user_name, @project.name, @generic_file.id)
    end
  end
  
  def destroy
    authorize @generic_file
    
    flash[:notice] = t('.success', filename: @generic_file.file.file.filename)
    
    @generic_file.destroy
    redirect_to user_project_path(@project_owner.user_name, @project.name)
  end
  
  def download
    authorize @generic_file
    send_file(@generic_file.file.path,
              filename: @generic_file.file.file.filename,
              type: @generic_file.file.content_type,
              disposition: 'attachmet',
              url_based_filename: true
    )
  end

  private

  def generic_file_params
    params.require(:generic_file).permit(:name, :description, :access_level, :file)
  end
  
  def load_project_owner
    @project_owner = User.find_by_user_name params[:user_name] if params[:user_name]
    @project_owner = current_user unless params[:user_name]
  end
  
  def load_project
    @project = @project_owner.owned_projects.find_by_name params[:project_name]
  end
  
  def load_generic_file
    @generic_file = @project.generic_files.find params[:id]
  end

end
