/*
 * Copyright (c) 2016, Aegir Labs LLC. All rights reserved.
 */

package com.aegir.demo.adapters;

import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.aegir.demo.model.BaseFloorItem;
import com.aegir.demo.model.FloorItem;
import com.aegir.demo.views.BuildingItemView;
import com.aegir.demo.views.FloorItemView;

import java.util.List;

/**
 * Adapter used to gets views for the Floor Selector List
 */
public class FloorItemAdapter extends ArrayAdapter<BaseFloorItem> {

    private FloorItemView.OnInfoImageViewClickListener infoImageViewClickListener;

    /**
     * Designated constructor
     * @param c the activity context
     * @param items the list of floor items
     */
    public FloorItemAdapter(Context c, List<BaseFloorItem> items, FloorItemView.OnInfoImageViewClickListener infoImageViewClickListener) {
        super(c, 0, items);
        this.infoImageViewClickListener = infoImageViewClickListener;
    }

    /**
     * Creates the view for the given row
     * @param position the row number
     * @param convertView a view to recycle
     * @param parent the parent group
     * @return A FloorItemView instances
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (!getItem(position).isBuilding()) {
            FloorItemView floorItemView = null;
            if (convertView instanceof FloorItemView) {
                floorItemView = (FloorItemView) convertView;
            }
            if (floorItemView == null) {
                floorItemView = FloorItemView.inflate(parent);
            }
            floorItemView.setItem((FloorItem)getItem(position), infoImageViewClickListener);
            return floorItemView;
        }
        else {
            BuildingItemView buildingItemView = null;
            if (convertView instanceof BuildingItemView) {
                buildingItemView = (BuildingItemView)convertView;
            }

            if (buildingItemView == null) {
                buildingItemView = BuildingItemView.inflate(parent);
            }
            buildingItemView.setItem(getItem(position));
            return buildingItemView;
        }
    }
}
