class Settings::MenuItemInstancesController < ApplicationController
  load_and_authorize_resource except: [:create]
  before_action :set_settings_menu_item_instance, only: [:show, :edit, :update, :destroy]
  before_action :set_settings_menu_item, only: [ :show, :edit, :new, :update]

  # GET /settings/menu_item_instances
  # GET /settings/menu_item_instances.json
  def index
    @settings_menu_item_instances = MenuItemInstance.all
  end

  # GET /settings/menu_item_instances/1
  # GET /settings/menu_item_instances/1.json
  def show
    id = MenuItemInstance.findParentCategory(@item)
    @category = MenuCategory.find(id)
  end

  # GET /settings/menu_item_instances/new
  def new
    id = MenuItemInstance.findParentCategory(@item)
    @category = MenuCategory.find(id)
    @settings_menu_item_instances = MenuItemInstance.new
  end

  # GET /settings/menu_item_instances/1/edit
  def edit
    id = MenuItemInstance.findParentCategory(@item)
    @category = MenuCategory.find(id)
  end

  # POST /settings/menu_item_instances
  # POST /settings/menu_item_instances.json
  def create
    @settings_menu_item_instances = MenuItemInstance.new(settings_menu_item_instance_params)

    #check if the menu item type is simple or not( not only the diff of status .. here is different routes)
    if params[:simple_menu_item_id]
      @settings_menu_item_instances.menu_item_id = params[:simple_menu_item_id]
      catID  = MenuItem.find(params[:simple_menu_item_id])
    else
      @settings_menu_item_instances.menu_item_id = params[:set_menu_item_id]
      catID  = MenuItem.find(params[:set_menu_item_id])
    end

    id = MenuItemInstance.findParentCategory(catID)
    category = MenuCategory.find(id)
    respond_to do |format|
      if @settings_menu_item_instances.save
        @settings_menu_item_instances.item_attributes = params[:menu_item_instance][:item_attributes].reject(&:blank?)
        @settings_menu_item_instances.save
        format.html { redirect_to settings_menu_category_simple_menu_item_path(category,catID), notice: 'Menu item instance was successfully created.' }
        format.json { render :show, status: :created, location: @settings_menu_item_instances }
      else
        format.html { render :new }
        format.json { render json: @settings_menu_item_instances.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /settings/menu_item_instances/1
  # PATCH/PUT /settings/menu_item_instances/1.json
  def update
    #check if the menu item type is simple or not( not only the diff of status .. here is different routes)
    if params[:simple_menu_item_id]
      catID  = MenuItem.find(params[:simple_menu_item_id])
    else
      catID  = MenuItem.find(params[:set_menu_item_id])
    end

    id = MenuItemInstance.findParentCategory(catID)
    category = MenuCategory.find(id)
    respond_to do |format|

      if @settings_menu_item_instances.update(settings_menu_item_instance_params)
        @settings_menu_item_instances.item_attributes = params[:menu_item_instance][:item_attributes].reject(&:blank?)
        @settings_menu_item_instances.save
        format.html { redirect_to settings_menu_category_simple_menu_item_path(category,catID), notice: 'Menu item instance was successfully updated.' }
        format.json { render :show, status: :ok, location: @settings_menu_item_instances }
      else
        format.html { render :edit }
        format.json { render json: @settings_menu_item_instances.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /settings/menu_item_instances/1
  # DELETE /settings/menu_item_instances/1.json
  def destroy
    @settings_menu_item_instances.destroy
    if params[:simple_menu_item_id]
      catID  = MenuItem.find(params[:simple_menu_item_id])
    else
      catID  = MenuItem.find(params[:set_menu_item_id])
    end
    # check if the menu item is sub of another menu item
    if catID.menu_category_id
      category = MenuCategory.find(catID.menu_category_id)
    else
      item = MenuItem.find(catID.menu_item_id)
      category = MenuCategory.find(item.menu_category_id)
    end
    respond_to do |format|
      format.html { redirect_to settings_menu_category_simple_menu_item_path(category,catID), notice: 'Menu item instance was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    def set_settings_menu_item
      if params[:simple_menu_item_id]
        @item = MenuItem.find(params[:simple_menu_item_id])
      else
        @item = MenuItem.find(params[:set_menu_item_id])
      end
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def settings_menu_item_instance_params

      params.require(:menu_item_instance).permit(:item_instance_code, :item_instance_name, :price, :item_attributes, :is_on_promotion, :promotion_price, :is_available,:menu_category_id,:menu_item_id)

    end
end
