class SupplierController < ApplicationController
  before_filter :authenticate_supplier!
  layout 'tablet'

  def home
    render layout: 'tablet'
  end

  # GET /supplier/settings
  def edit
    @supplier = current_supplier
  end

  def current
    respond_to do |format|
      format.json { render json: {supplier: current_supplier.as_json} }
    end
  end

  # POST /supplier/settings
  def update
    @supplier = current_supplier
    if current_supplier.update_attributes(supplier_params)
      redirect_to supplier_root_path
    else
      render action: :edit
    end
  end

  def mark_as_open
    current_supplier.mark_as_open!
    redirect_to :back
  end
  def mark_as_closed
    current_supplier.mark_as_closed!
    redirect_to :back
  end
  # GET /suppliers/1/active_orders
  # GET /suppliers/1/active_orders.json
  def active_orders
    @supplier = current_supplier
    respond_to do |format|
      format.html { render layout: 'tablet' }
      format.json do
        h = @supplier.as_json
        h[:orders] = []
        list_total = 0.0
        for order in @supplier.active_orders(section_id: params[:section_id].presence)
          ho = {products: []}
          order_total = 0.0
          for product_order in order.product_orders
            order_total += (product_order.quantity * product_order.price).round(2)
            ho[:products] << {name: product_order.product.name, id: product_order.product_id, number: product_order.quantity, price: product_order.price}
          end
          ho[:total_amount] = order_total.round(2)
          ho[:state] = order.state
          ho[:table_number] = order.table_number
          ho[:section_title] = order.list.table.section.try(:title)
          ho[:id] = order.id
          ho[:list_id] = order.list_id
          list_total += ho[:total_amount]
          h[:orders] << ho
        end 
        h[:total_amount] = list_total.round(2)
        render json: h, layout: 'tablet'
      end
    end
  end

  # GET /suppliers/1/active_lists
  # GET /suppliers/1/active_lists.json
  def active_lists
    @supplier = current_supplier
    respond_to do |format|
      format.html { render layout: 'tablet' }
      format.json do
        h = @supplier.as_json
        h[:lists] = []
        grand_total = 0.0
        for list in @supplier.active_lists(section_id: params[:section_id].presence)
          hl = list.with_info_as_json
          grand_total += hl[:total_amount]
          h[:lists] << hl
        end 
        h[:total_amount] = grand_total.round(2)
        render json: h, layout: 'tablet'
      end
    end
  end

  # POST /supplier/close_list
  def close_list
    @list = List.find_by_supplier_id_and_id(current_supplier.id, params[:list_id])
    @list.close!
    render nothing: true
  end

  # POST /orders/1/is_helped
  def mark_list_as_helped
    @list = List.find_by_supplier_id_and_id(current_supplier.id, params[:list_id])
    @list.is_helped!
    render nothing: true
  end

  # POST /orders/1/is_being_processed
  def mark_order_in_process
    @order = Order.find_by_supplier_id_and_id!(current_supplier.id, params[:order_id])
    @order.is_being_processed!
    render nothing: true
  end

  # POST /orders/1/is_delivered
  def order_is_delivered
    @order = Order.find_by_supplier_id_and_id(current_supplier.id, params[:order_id])
    @order.is_delivered!
    render nothing: true
  end

  private

  def supplier_params
    params.require(:supplier).permit(:name, :email, :open, :time_zone, :night_offset, :location, :lat, :lng, :offer_wifi, :wifi_ssid, :wifi_type, :wifi_password, :iens_profile)
  end
end
