/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.nplusc.izc.iZpl.GUI;

import de.nplusc.izc.iZpl.MultiPlayListItem;
import de.nplusc.izc.iZpl.PlProcessorV2;
import de.nplusc.izc.iZpl.PlayListItem;
import de.nplusc.izc.iZpl.SinglePlayListItem;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;

/**
 *
 * @author LH
 */
public class PlayListManagingBackend
{
    private static PlProcessorV2 iPPP=null; 
    
    
    public static void initPPP(PlProcessorV2 p)
    {
       iPPP = p;
    }
    
    private static List<String> playListItemKeys = new ArrayList<>();//wird genutzt um idx auf hasmap zu linkern
    public static void refreshKeyList()
    {
        playListItemKeys = new ArrayList<>();//Liste flushen
        for (String k : iPPP.getPool().keySet())
        {
            playListItemKeys.add(k);
        }
    }
    public static String[] getTrackTitles()
    {
        String[] tt = new String[playListItemKeys.size()];
        for (int i=0;i<playListItemKeys.size();i++)
        {
            String k=playListItemKeys.get(i);
            PlayListItem x = iPPP.getPool().get(k);
            if(x instanceof MultiPlayListItem)
            {
                tt[i] = ((MultiPlayListItem)x).getSrc();
            }
            else
            {
                String t = ((SinglePlayListItem) x).getTitle();
                if(t!=null && !t.equals(""))
                {
                    tt[i] = t.split(",")[1];
                }
                else
                {
                    tt[i] =((SinglePlayListItem) x).getPath();
                }
            }
        }
        return tt;
    }
    
    public static int getPlayCount(int id)
    {
        String k=playListItemKeys.get(id);
        PlayListItem x = iPPP.getPool().get(k);
        return x.getCountPlayed();
    }
    
    public static int getTargetPlayCount(int id)
    {
        String k=playListItemKeys.get(id);
        PlayListItem x =iPPP.getPool().get(k);
        return x.getTargetPlaycount();
    }
    public static void setTargetPlayCount(int id,int tpc)
    {
        String k=playListItemKeys.get(id);
        PlayListItem x = iPPP.getPool().get(k);
        int c = x.getCountPlayed();
        if(c>=tpc)
        {
            tpc=c+1;
        }
        x.setTargetPlaycount(tpc);
        iPPP.rebuildCache();//falls ein lied schon verbraucht ist; da es dann wieder playready ist uss der cache resetted werden...
    }
    public static void reloadList()
    {
        iPPP.reloadLists(true);
    }
    public static void forceElement(int id)
    {
        iPPP.forceNext(playListItemKeys.get(id));
    }
    
    public static boolean useSkinnable=false;
    
    public static void resetplayListWindows()
    {
        playListedit=null;
    }
    
    private static JFrame playListedit=null;
    public static void loadPLEditor()
    {
        if(playListedit==null)
        {
            if(useSkinnable)
            {
                //TODO: skinloader...
                playListedit=new PlayListManagingSkinnable(null,null);
            }
            else
            {
                playListedit = new PlayListManagingDefault();
            }
        }
        playListedit.setVisible(true);
    }
}
