package ru.job4j.start;

import ru.job4j.models.Item;

import java.util.ArrayList;
import java.util.Iterator;

/**
* Tracker.
*
* auhtor Indyukovs
* version 0.1
*/
public class Tracker {
    /**
     * @param items
     */
    private ArrayList<Item> items = new ArrayList<Item>(10);
    /**
     * @param position second
     */
    private int position = 0;

    /**
     * method add.
     *
     * @param item first
     */
    public void add(Item item) {
        //item.setId(String.valueof(RN.next()));
        this.items.add(position++, item);
    }

    /**
     * method finById.
     *
     * @param id first
     * @return result
     */
    protected Item findById(String id) {
        Item result = null;
        Iterator iterator = items.iterator();
        while (iterator.hasNext()) {
            Item next = (Item) iterator.next();
            if (next != null && next.getId().equals(id)) {
                result = next;
                break;
            }
        }
        return result;
    }

    /**
     * method update.
     *
     * @param item first
     */
    public void update(Item item) {
        for (int i = 0; i < items.size(); i++) {
            if (items.get(i) != null && items.get(i).getId().equals(item.getId())) {
                int index = items.indexOf(items.get(i));
                items.remove(items.get(i));
                items.add(index, item);
            }
        }
    }

    /**
     * method delete.
     *
     * @param item first
     */
    public void delete(Item item) {
        Iterator<Item> iterator = items.iterator();
        int i = 0;
        while (iterator.hasNext()) {
            Item next = iterator.next();
            if (next != null && next.getId().equals(item.getId())) {
                iterator.remove();
                i++;
                System.out.println("log");
            }
        }
        if (i == 1) {
            items.add(null);
        }
    }

    /**
     * method findAll.
     *
     * @return tempItem
     */
    public ArrayList<Item> findAll() {
        items.trimToSize();
        return items;
    }

    /**
     * method findByName.
     *
     * @param key first
     * @return tempItem
     */
    public ArrayList<Item> findByName(String key) {
        Iterator iterator = items.iterator();
        ArrayList<Item> tempItems = new ArrayList<>();
        while (iterator.hasNext()) {
            Item next = (Item) iterator.next();
            if (next != null && next.getName().equals(key)) {
                tempItems.add(next);
            }
        }
        tempItems.trimToSize();
        return tempItems;
    }
/**
* method getAll.
* @return items
*/
	public ArrayList<Item> getAll() {
        return items;
	}
}
@@ -280,84 +280,8 @@
 10);
-%0A    /**%0A     * @param position second%0A     */%0A    private int position = 0;
 %0A%0A  
@@ -454,20 +454,8 @@
 add(
-position++, 
 item
@@ -632,32 +632,38 @@
         Iterator
+%3CItem%3E
  iterator = item
@@ -728,39 +728,32 @@
      Item next =
- (Item)
  iterator.next()
@@ -1244,61 +1244,13 @@
 ems.
-remove(items.get(i));%0A                items.add(index
+set(i
 , it
@@ -2131,16 +2131,22 @@
 Iterator
+%3CItem%3E
  iterato
@@ -2286,15 +2286,8 @@
 xt =
- (Item)
  ite

task 10039