/*
 *
 * This code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
 */

#include "stdafx.h"

#include <stdlib.h>

#include "providers.h"

//#define DEBUG_MSG
#ifdef DEBUG_MSG
#define d(x) { (x); }
#include <unistd.h>
#else
#define d(x) ;
#endif


// -- cPreferredProviders -----------------------------------------------------------


bool cPreferredProviders::Init(const char *cfgdir)
{
//	Clear();
	pref_provs.CA_Anzahl = 0;
	ConfRead("preferred providers",AddDirectory(cfgdir,"providers.conf"));
	d(printf("Created %d preferred provider(s)\n",Count()))
	return true;
}

bool cPreferredProviders::ParseLine(const char *line, bool fromCache)
{
	char ctype;
	int sysid, provid,ca_count;
	ca_count = pref_provs.CA_Anzahl;
	if(sscanf(line,"%c:%x:%x",&ctype,&sysid,&provid)==3) {
		printf ("Provider %c:%04x:%08x\n",ctype, sysid, provid);
        if (ca_count >= MAX_CA_SYSTEMS)
        {
           printf ("Can not handle more than %d preferred providers \n",MAX_CA_SYSTEMS);
           return false ;
        } 
        pref_provs.CA_System[ca_count].CA_Typ=sysid;
        pref_provs.CA_System[ca_count].Provider_Id = provid ;
	    switch (ctype) {
		    case 'C':
	          pref_provs.CA_System[ca_count].EMM = PPRIO_CARDC;
			  break ;
		    case 'S':
	          pref_provs.CA_System[ca_count].EMM = PPRIO_SOFTC;
			  break ;
		    case 'I':
			  pref_provs.CA_System[ca_count].EMM = PPRIO_IGNORE;
			  break ;
		    default :
	          pref_provs.CA_System[ca_count].EMM = PPRIO_BOTH;
			  break ;
	    }
	    pref_provs.CA_Anzahl++ ;
        return true ;
	}
	return false;
}

int cPreferredProviders::Prioritize (TProgramm82 *m_curprg,int m_count)
{
  int i, j,k;
  TCA_System82 tmpCA ;
  if (pref_provs.CA_Anzahl < 1) return m_count ;
  // Loop through preferred providers in reverse order so that the first listed has the highest priority
  for (i = pref_provs.CA_Anzahl -1;i >=0;i--)
  {
	  j=0;
	  do
	  {
		  if ( (m_curprg->CA_System[j].CA_Typ == pref_provs.CA_System[i].CA_Typ) && \
               (m_curprg->CA_System[j].Provider_Id == pref_provs.CA_System[i].Provider_Id))
		  {
			  if (pref_provs.CA_System[i].EMM == PPRIO_IGNORE)
			  {
				  // Remove provider from list
				  m_count--;
				  if (j<m_count)
				    for (k=j;k<m_count;k++)
				      m_curprg->CA_System[k]=m_curprg->CA_System[k+1] ;

			  }
			  else
			  {
				  // Move provider on the list start
				  if (j>0)
				  {
  				    tmpCA = m_curprg->CA_System[j];
  				    for (k=j;k>0;k--)
				      m_curprg->CA_System[k]=m_curprg->CA_System[k-1] ;
                    m_curprg->CA_System[0]=tmpCA;
				  }
                  m_curprg->CA_System[0].EMM = pref_provs.CA_System[i].EMM ;

			  }
		  }
		  j++;
	  }
      while (j <m_count);
  }

  printf("-------------CA TABLE-------------\n");
  for (j=0;j<m_count;j++)
	printf("| System:%.4X PID:%.4X PRV:%.6X\n",m_curprg->CA_System[j].CA_Typ,m_curprg->CA_System[j].ECM,m_curprg->CA_System[j].Provider_Id);
  printf("----------------------------------\n");
  return m_count ;
}
