module Suppliers
  class ProductCategoriesController < Suppliers::ApplicationController
    layout 'tablet'

    # 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 }
        format.yaml do
          @product_categories.include_relation(:products)
          h = {product_categories: []}
          @product_categories.each do |product_category|
            h[:product_categories] << {
              name: product_category.name,
              products: product_category.products.map{|product| {
                name: product.name,
                price: product.price,
                code: product.code,
                description: product.description
              }}
            }
          end
          render text: h.deep_stringify_keys.to_yaml
        end
      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

    def import
      redirect_to({action: :index}, alert: 'no_file_given') and return unless params[:file].present?
      result = ProductCategoriesImporter.new(current_supplier).import(params[:file].read)
      if result.errors.any?
        redirect_to({action: :index}, alert: result.errors.message)
      else
        redirect_to action: :index
      end
    rescue => e
      redirect_to({action: :index}, alert: 'something went wrong')
    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 }
        else
          format.html { render action: "new" }
          format.json { render json: {errors: @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 { render json: @product_category }
        else
          format.html { render action: "edit" }
          format.json { render json: {errors: @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
      head :no_content
    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, :position,
        :active_on_sunday, :active_on_monday, :active_on_tuesday, :active_on_wednesday, :active_on_thursday, :active_on_friday, :active_on_saturday,
        product_ids: [])
    end
  end
end
