class KidungsController < ApplicationController
  before_action :authenticate_user!
  before_action :set_kidung, only: [:show, :edit, :update, :destroy]

  # GET /kidungs
  # GET /kidungs.json
  def index
    @kidungs = Kidung.all
  end

  # GET /kidungs/1
  # GET /kidungs/1.json
  def show
    jsonResponse = {}
    if @kidung
      jsonResponse = @kidung
    end
    respond_to do |format|
      format.html
      format.json { render json: jsonResponse }
    end
  end

  # GET /kidungs/new
  def new
    @kidung = Kidung.new
  end

  # GET /kidungs/1/edit
  def edit
  end

  # POST /kidungs
  # POST /kidungs.json
  def create
    @kidung = Kidung.new(kidung_params)
    @kidung.id = @kidung.tipe.ord.to_s + @kidung.number.to_s.rjust(4, "0")
    @kidung.history = @kidung.history.squish
    @kidung.history = @kidung.history.gsub('/media/photos/', 'http://kidung.org:3000/media/photos/')
    @kidung.history = @kidung.history.gsub('class="wysiwyg-float-left"', 'align="left"')
    @kidung.history = @kidung.history.gsub('class="wysiwyg-float-right"', 'align="right"')

    respond_to do |format|
      if @kidung.save
        format.html { redirect_to kidungs_url, notice: 'Kidung was successfully created.' }
        format.json { render :show, status: :created, location: @kidung }
      else
        format.html { render :new }
        format.json { render json: @kidung.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /kidungs/1
  # PATCH/PUT /kidungs/1.json
  def update
    @kidung.destroy()
    @kidung = Kidung.new(kidung_params)
    @kidung.id = @kidung.tipe.ord.to_s + @kidung.number.to_s.rjust(4, "0")
    @kidung.history = @kidung.history.gsub('http://kidung.org:3000', '');
    @kidung.history = @kidung.history.gsub('/media/photos/', 'http://kidung.org:3000/media/photos/');
    respond_to do |format|
      if @kidung.save
        format.html { redirect_to kidungs_url, notice: 'Kidung was successfully updated.' }
        format.json { render :show, status: :ok, location: @kidung }
      else
        format.html { render :edit }
        format.json { render json: @kidung.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /kidungs/1
  # DELETE /kidungs/1.json
  def destroy
    @kidung.destroy
    respond_to do |format|
      format.html { redirect_to kidungs_url, notice: 'Kidung was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  def upload_file
    render "upload_file"
  end

  def post_upload_file
    uploaded_io = params[:mp3]
    File.open(Rails.root.join('public', 'media/mp3voice/' + params[:tipe], uploaded_io.original_filename), 'wb') do |file|
      file.write(uploaded_io.read)
    end
    redirect_to kidungs_url, notice: 'File was successfully uploaded.'
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_kidung
      begin
        @kidung = Kidung.find(params[:id])
      rescue
      end
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def kidung_params
      params.require(:kidung).permit(:tipe, :number, :content, :history, :bootsy_image_gallery_id)
    end
end
