class Settings::ShopsController < ApplicationController
  load_and_authorize_resource except: [:create]
  before_action :set_shop, only: [:show, :edit, :update, :destroy]

  # GET /settings/shops
  # GET /settings/shops.json
  def index
    @settings_shops = Shop.all
  end

  # GET /settings/shops/1
  # GET /settings/shops/1.json
  def show
    @display_images = @settings_shop.display_images.all
  end

  # GET /settings/shops/new
  def new
    @settings_shop = Shop.new
    @display_image = @settings_shop.display_images.build
  end

  # GET /settings/shops/1/edit
  def edit
  end

  # POST /settings/shops
  # POST /settings/shops.json
  def create
    @settings_shop = Shop.new(shop_params)
    respond_to do |format|
      if @settings_shop.save
        if params[:display_images].present?
          params[:display_images]['image'].each do |a|
            @display_image = @settings_shop.display_images.create!(:shop_id => @shop.id, :image => a)
          end
        end
        format.html { redirect_to settings_shops_url, notice: 'Shop was successfully created.' }
        format.json { render :index, status: :created, location: @settings_shop }
      else
        format.html { render :new }
        format.json { render json: settings_shops_url.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /settings/shops/1
  # PATCH/PUT /settings/shops/1.json
  def update
    respond_to do |format|
      if @settings_shop.update(shop_params)
        if params[:display_images].present?
          params[:display_images][:image].each do |a|
            if File.extname(a.original_filename) == ".mp3"
              if !@settings_shop.display_images.find_by_name("order_audio").nil?
                audio_data = @settings_shop.display_images.find_by_name("order_audio").image
                delete_path = Rails.root.join("public/#{audio_data}")
                if File.exists?(delete_path)
                  File.delete(delete_path) 
                end  
              end              
              save_path = Rails.root.join("public/#{current_shop.shop_code}_#{a.original_filename}")
              File.open(save_path, 'wb') do |f|
                f.write a.read
              end
              audio_name = "#{current_shop.shop_code}_#{a.original_filename}"
              @settings_shop.display_images.where(:name => "order_audio").destroy_all
              @display_image = @settings_shop.display_images.create!(:shop_id => @shop.id, :name => "order_audio", :image => audio_name)
            else
              @aa = Base64.encode64(a.read)
              @display_image = @settings_shop.display_images.create!(:shop_id => @shop.id, :image => "data:image/jpeg;base64,"+@aa)
            end
          end
        end
        format.html { redirect_to settings_shops_url, notice: 'Shop was successfully updated.' }
        format.json { render :index, status: :ok, location: @settings_shop }
      else
        format.html { render :edit }
        format.json { render json: settings_shops_url.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /settings/shops/1
  # DELETE /settings/shops/1.json
  def destroy
    @settings_shop.destroy
    flash[:notice] = 'Shop was successfully destroyed.'
    render :json => {:status=> "Success", :url => settings_shops_url }.to_json
    # respond_to do |format|
    #   format.html { redirect_to settings_shops_url, notice: 'shop was successfully destroyed.' }
    #   format.json { head :no_content }
    # end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_shop
      @settings_shop = Shop.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def shop_params
      params.require(:shop).permit(:logo, :name,:address,:city,:township,:state,:country,:phone_no,:reservation_no,:license,:activated_at,:license_data,:base_currency,:cloud_token,:cloud_url,:owner_token,:id_prefix,:is_rounding_adj,:quick_sale_summary,:calc_tax_order,:show_account_info,:note, display_images_attributes: [:id, :shop_id, :name, :image])
    end
end
