module Suppliers
  class ProductCategoriesController < Suppliers::ApplicationController

    # GET /product_categories
    # GET /product_categories.json
    def index
      @product_categories = current_supplier.product_categories.sort_by(&:position)

      respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @product_categories }
      end
    end

    # GET /product_categories/1
    # GET /product_categories/1.json
    def show
      @product_category = ProductCategory.find_by_supplier_id_and_id!(current_supplier.id, params[:id])

      respond_to do |format|
        format.html # show.html.erb
        format.json { render json: @product_category }
      end
    end

    # GET /product_categories/new
    # GET /product_categories/new.json
    def new
      @product_category = ProductCategory.new

      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @product_category }
      end
    end

    # GET /product_categories/1/edit
    def edit
      @product_category = ProductCategory.find(params[:id])
    end

    # POST /product_categories
    # POST /product_categories.json
    def create
      @product_category = ProductCategory.new(product_category_params)
      @product_category.supplier = current_supplier

      respond_to do |format|
        if @product_category.save
          format.html { redirect_to [:suppliers, :product_categories], notice: t('action.create.successfull', model: ProductCategory.model_name.human) }
          format.json { render json: @product_category, status: :created, location: @product_category }
        else
          format.html { render action: "new" }
          format.json { render json: @product_category.errors, status: :unprocessable_entity }
        end
      end
    end

    # PUT /product_categories/1
    # PUT /product_categories/1.json
    def update
      @product_category = ProductCategory.find_by_supplier_id_and_id!(current_supplier.id, params[:id])

      respond_to do |format|
        if @product_category.update_attributes(product_category_params)
          format.html { redirect_to [:suppliers, :product_categories], notice: t('action.update.successfull', model: ProductCategory.model_name.human) }
          format.json { head :no_content }
        else
          format.html { render action: "edit" }
          format.json { render json: @product_category.errors, status: :unprocessable_entity }
        end
      end
    end

    # DELETE /product_categories/1
    # DELETE /product_categories/1.json
    def destroy
      @product_category = ProductCategory.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
      @product_category.destroy

      respond_to do |format|
        format.html { redirect_to suppliers_product_categories_url, notice: t('action.destroy.successfull', model: ProductCategory.model_name.human) }
        format.json { head :no_content }
      end
    end

    # POST /supplier/product_categories/sort
    #   params ~= product_category: ['abc', 'def', 'another id', ...]
    def sort
      @product_categories = ProductCategory.find(params[:product_category]).select{|pc| pc.supplier_id == current_supplier.id} # The select is hack prevention
      1.upto(@product_categories.size){|i| @product_categories[i-1].position = i}
      CouchPotato.database.couchrest_database.bulk_save(@product_categories)
      render nothing: true
    end

    private

    def product_category_params
      params.require(:product_category).permit(:name, :start_from, :end_on, :full_day, product_ids: [], week_days: [])
    end
  end
end
