package com.jens.rhasspy.shoppinglist;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class StoreType
{
	long id;
	String name;
	
	static ArrayList<StoreType> storeTypeCache = null;
	
	public static ArrayList<StoreType> readAllStoreTypes()
	{
		if(storeTypeCache == null)
		{
			try
			{
				storeTypeCache = new ArrayList<StoreType>();
				
				Connection conn = DatabaseHandler.getInstance().getConnection();
				PreparedStatement preparedStmt = null;
		
				String query = "SELECT id, name FROM storeTypes";
				
				preparedStmt = conn.prepareStatement(query);
				
				Miscellaneous.logEvent(preparedStmt.toString(), 5);
				
		        ResultSet res = preparedStmt.executeQuery();
		        
		        while (res.next())
		        {
		        	StoreType st = new StoreType();
		        	
		        	st.setId(res.getLong("id"));
		        	st.setName(res.getString("name"));
		        	
		        	storeTypeCache.add(st);
		        }
		        
		        res.close();
				preparedStmt.close();
			}
			catch(Exception e)
			{
				
			}
		}
	        		
        return storeTypeCache;
	}

	public long getId()
	{
		return id;
	}

	public void setId(long id)
	{
		this.id = id;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	@Override
	public boolean equals(Object obj)
	{
		return name.equalsIgnoreCase(((StoreType)obj).getName());
	}

	@Override
	public String toString()
	{
		return this.getName();
	}

	public static StoreType getById(long id)
	{
		for(StoreType st : readAllStoreTypes())
		{
			if(st.getId() == id)
				return st;
		}
		
		return null;
	}
}
