/*
 * The MIT License
 *
 * Copyright 2014 sfcook.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package steamgameselector;

import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Random;
import java.util.Map.Entry;

/**
 *
 * @author sfcook
 */
public class SteamGameSelector {
    private SteamData sdb;
    private ArrayList<Game> sharedGames;
    private Map<String,Tag> tags;
    
    public SteamGameSelector(SteamData instance)
    {
        sdb=instance;
        sharedGames=new ArrayList();
        tags=new HashMap();
    }
    
    public Map<String,Tag> getTags()
    {
        ArrayList<String> tags=sdb.getTags();
        
        for(String tag:tags)
        {
            this.tags.put(tag,new Tag(tag));
        }
        
        Map<String,Tag> newTags=new HashMap();
        
        for(Entry entry:this.tags.entrySet())
        {
            Tag item=(Tag)entry.getValue();
            Tag tag=new Tag(item.tag,item.and,item.or,item.not);
            newTags.put(item.tag,tag);
        }
        
        return newTags;
    }
    
    public void setTags(Map<String,Tag> tags)
    {
        this.tags=new HashMap(tags);
    }
    public void addAccount(Account account)
    {
        sdb.addAccount(account);
    }
    
    public int addAccount(String url)
    {
        return sdb.addAccount(url);
    }
    
    public ArrayList<Account> getAccounts()
    {
        return sdb.getAccounts();
    }
    
    public void removeAccount(int accountid)
    {
        sdb.removeAccount(accountid);
    }
    
    //TODO: should check if steam game or not
    public void addGame(Game game)
    {
        sdb.addGame(game);
    }
    
    public Game getSteamGame(int appid)
    {
        return sdb.getSteamGame(appid);
    }
    
    public void reloadSteamGame(int appid)
    {
        sdb.reloadSteamGame(appid);
    }
    
    private void updateSharedGames()
    {
        sharedGames=sdb.getSharedGames();
    }
    public ArrayList<Game> getSharedGames()
    {
        updateSharedGames();
        return sharedGames;
    }
    
    public int getRandomGameIndex()
    {
        updateSharedGames();
        Random rand=new Random();
        return rand.nextInt(sharedGames.size());
    }
    public Game getRandomGame()
    {
        int selected=getRandomGameIndex();
        return sharedGames.get(selected);
    }
}

@@ -1724,17 +1724,21 @@
 ing%3E tag
-s
+Array
 =sdb.get
@@ -1779,16 +1779,21 @@
 ing 
-tag:tags
+item:tagArray
 )%0D%0A 
@@ -1810,29 +1810,66 @@
             
-this.
+if(tags.get(item)==null)%0D%0A                
 tags.put(tag
@@ -1865,19 +1865,20 @@
 ags.put(
-tag
+item
 ,new Tag
@@ -1878,19 +1878,20 @@
 new Tag(
-tag
+item
 ));%0D%0A   
@@ -1990,21 +1990,16 @@
 y entry:
-this.
 tags.ent
@@ -2272,17 +2272,16 @@
 Tag%3E tag
-s
 )%0D%0A    %7B
@@ -2294,36 +2294,144 @@
     
-this.tags=new HashMap(tags);
+for(Entry entry:tag.entrySet())%0D%0A        %7B%0D%0A            Tag item=(Tag)entry.getValue();%0D%0A            tags.put(item.tag,item);%0D%0A        %7D
 %0D%0A  

Tag functions passing tests