module Suppliers
  class SectionsController < Suppliers::ApplicationController

    # GET /sections
    # GET /sections.json
    def index
      # render json: {} and return
      @sections = current_supplier.sections
      @sections.include_relation(:tables)

      # @active_lists = List.active_for_supplier(current_supplier)
      # @active_table_ids = @active_lists.map(&:table_id).compact
      # for section in @sections
      #   for table in section.tables
      #     if active_list = @active_lists.find{|l| l.table_id == table.id}
      #       table.active_list = active_list
      #     end
      #   end
      # end
      render json: @sections
    end

    # GET /sections/1
    # GET /sections/1.json
    def show
      @section = Section.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
      @tables =  @section.tables_with_active_list_id
      render json: @section
    end

    # GET /sections/new
    # GET /sections/new.json
    def new
      @section = Section.new
      @section.supplier = current_supplier
      render json: @section
    end

    # GET /sections/1/edit
    def edit
      @section = Section.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
    end

    # POST /sections
    # POST /sections.json
    def create
      @section = Section.new(section_params)
      @section.supplier = current_supplier

      if @section.save
        render json: @section
      else
        render json: {errors: @section.errors}, status: :unprocessable_entity
      end
    end

    # PUT /sections/1
    # PUT /sections/1.json
    def update
      @section = Section.find_by_supplier_id_and_id!(current_supplier.id, params[:id])

      if @section.update_attributes(section_params)
        render json: @section
      else
        render json: {errors: @section.errors}, status: :unprocessable_entity
      end
    end

    # DELETE /sections/1
    # DELETE /sections/1.json
    def destroy
      @section = Section.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
      @section.destroy

      head :no_content
    end

    # GET /sections/1/manage_tables
    # GET /sections/1/manage_tables.json
    def manage_tables
      @section = Section.find_by_supplier_id_and_id!(current_supplier.id, params[:id])

      render json: @section
    end

    # GET /sections/1/tables_view
    # GET /sections/1/tables_view.json
    def tables_view
      @section = Section.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
      @tables =  @section.tables_with_active_list_id

      render json: @section
    end

    # POST /sections/1/add_tables {number_start: 1423, number_end: 234234}
    def add_tables
      @section = Section.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
      number_start = params[:number_start].to_i
      number_end = params[:number_end].to_i
      added_tables = []
      for table_number in number_start..number_end
        next if table_number.zero?
        table = Table.new(number: table_number)
        table.supplier = current_supplier
        table.section = @section
        table.save
        added_tables << table
      end
      @section.arrange_tables_in_grid added_tables
      render json: JSONAPI::Serializer.serialize(@section.tables, serializer: Supplier::TableSerializer, is_collection: true, include: %w[section])
    end

    # POST /sections/1/arrange_tables {number_start: 1423, number_end: 234234}
    def arrange_tables
      @section = Section.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
      #TODO handle messages client side
      #json_alert('messages.could_not_arrange_tables_distributed')
      #json_alert('messages.could_not_arrange_tables_by_row')
      #json_alert('messages.could_not_arrange_tables_by_column')))
      #json_alert('messages.could_not_arrange_tables')
      success = case params[:option]
      when 'distributed' then @section.arrange_tables_in_grid
      when 'by_row' then @section.arrange_tables_in_rows_of(params[:row_count].to_i)
      when 'by_column' then @section.arrange_tables_in_columns_of(params[:column_count].to_i)
      else false
      end

      if success
        table_json =  ActiveModel::ArraySerializer.new(@section.tables, each_serializer: SupplierTableSerializer, root: false).as_json
        render json: {tables: table_json}
      else
        render json: {}, status: 500
      end
    end

    def table_actions
      @section = Section.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
      render(text: 'No table_id given', content_type: Mime::HTML.to_s) and return unless params[:table_id].present?
      @table = Table.find(params[:table_id])
      render(text: 'Table is not correct', content_type: Mime::HTML.to_s) and return unless @table.section_id == @section.id
      @list = List.active_for_table(@table).first
      @orders = @list ? @list.active_orders : []
      render layout: false
    end

    private

    def section_params
      permitted_attributes = [:title, :path, :width, :height]
      # do not raise in development and test for json communication
      if request.format.json?
        params.require(:section).slice(*permitted_attributes).permit!
      else
        params.require(:section).permit permitted_attributes
      end
    end
  end
end
