class BrandsController < ApplicationController
  before_action :set_brand, only: [:show, :edit, :update, :destroy]

  # GET /brands
  # GET /brands.json
  def index
    @brands = Brand.all    
    if params[:device_type] && params[:device_type] != ''
      @device_type = DeviceType.find(params[:device_type].to_i)
      @brands = @brands.select { |b| b.models.collect { |m| m.device_type }.include? @device_type }
    end
  end

  # GET /brands/1
  # GET /brands/1.json
  def show
    if params[:device_type] && params[:device_type] != ''
      @device_type = DeviceType.find(params[:device_type].to_i)
      @models = @brand.models.where( device_type: @device_type )
      @devices = @models.collect {|m| m.devices }.flatten.uniq
    else
      @devices = @brand.models.collect {|m| m.devices }.flatten.uniq
    end
  end

  # GET /brands/new
  def new
    @brand = Brand.new
  end

  # GET /brands/1/edit
  def edit
  end

  # POST /brands
  # POST /brands.json
  def create
    @brand = Brand.new(brand_params)

    respond_to do |format|
      if @brand.save
        format.html { redirect_to brand_url( @brand, protocol: redirect_protocol ), notice: 'Brand was successfully created.' }
        format.json { render :show, status: :created, location: @brand }
      else
        format.html { render :new }
        format.json { render json: @brand.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /brands/1
  # PATCH/PUT /brands/1.json
  def update
    respond_to do |format|
      if @brand.update(brand_params)
        format.html { redirect_to brand_url(@brand, protocol: redirect_protocol), notice: 'Brand was successfully updated.' }
        format.json { render :show, status: :ok, location: @brand }
      else
        format.html { render :edit }
        format.json { render json: @brand.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /brands/1
  # DELETE /brands/1.json
  def destroy
    @brand.destroy
    respond_to do |format|
      format.html { redirect_to brands_url(protocol: redirect_protocol), notice: 'Brand was successfully destroyed.'}
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def brand_params
      params.require(:brand).permit(:name, :display_name, :alternative)
    end
end
