using System;
using System.Collections;
using System.Xml;
using System.IO;
using System.Collections.Specialized;
using Novell.CASA.DataEngines.Common;
using Novell.CASA.CASAPolicy;
#if LINUX
using Novell.CASA.GUI;
#endif

namespace Novell.CASA.DataEngines 
{

    public class AD 
    {

       private miCASAEngine micasaengine;
#if LINUX
       private GKEngine gkEngine = null;
       private KWalletEngine  kwEngine = null; 
#endif
       private AggregationPol aggPol;
   

       public AD()
       {
         // Always Aggregate miCASA. 
          micasaengine =  new miCASAEngine();

/*
   
         // Reading Policy to see what else needs to be Aggregated.
         aggPol = (AggregationPol) ICASAPol.GetPolicy(CASAPolType.AGGREGATION_POL);
         if (aggPol != null ) 
         { 
        	ArrayList stores = aggPol.StoreList;
        	IEnumerator enumerator = stores.GetEnumerator();
  
        	while(enumerator.MoveNext())
         	{
                	string storeID = (((Store)(enumerator.Current)).StoreName);

                	if(storeID.Equals(ConstStrings.KW))
                	{ 
                        	Logger.DbgLog("A-D Lib: KWallet Set up for Aggregation");
                     		kwEngine = new KWalletEngine(); 
                	} 
               		else if(storeID.Equals(ConstStrings.GK))
                	{
                     		Logger.DbgLog("A-D Lib:Gnome Keyring Set up for Aggregation");
                    		 gkEngine = new GKEngine(); 
                	} 
         	// Console.WriteLine("StoreName = " + ((Store)(enumerator.Current)).StoreName + "StoreId = " + ((Store)(enumerator.Current)).StoreId);
         	}
        }  
 */
 
      }
     
       public XmlDocument Aggregate()
       {
#if LINUX
            gkEngine = null;
            kwEngine = null;
#endif
           //Read the Policy Just before you aggregate
           
           // Reading Policy to see what else needs to be Aggregated.
           aggPol = (AggregationPol) ICASAPol.GetPolicy(CASAPolType.AGGREGATION_POL);
           if (aggPol != null )
           {
                ArrayList stores = aggPol.StoreList;
                IEnumerator enumerator = stores.GetEnumerator();

                while(enumerator.MoveNext())
                {
                        string storeID = (((Store)(enumerator.Current)).StoreName);

#if LINUX
                        if(storeID.Equals(ConstStrings.KW))
                        {
                                Logger.DbgLog("A-D Lib: KWallet Set up for Aggregation");
                                kwEngine = new KWalletEngine();
                        }
                        else if(storeID.Equals(ConstStrings.GK))
                        {
                                Logger.DbgLog("A-D Lib:Gnome Keyring Set up for Aggregation");
                                 gkEngine = new GKEngine();
                        }
#endif
                // Console.WriteLine("StoreName = " + ((Store)(enumerator.Current)).StoreName + "StoreId = " + ((Store)(enumerator.Current)).StoreId);
                }
          }
  
        

           XmlDocument ccf = new XmlDocument();    
           XmlElement elem = ccf.CreateElement("CCF");
           ccf.AppendChild(elem);

           // TBD: Lookup Policy here and maybe send it via constructors
           XmlNode micasaEnum  =  micasaengine.Aggregate();  
           if (micasaEnum != null) //Atleast <miCASA> should come incase of no secrets
           {
           	XmlNode gotit = ccf.ImportNode(micasaEnum,true);
           	ccf.DocumentElement.AppendChild(gotit);
           }
           else
           {
                // Null comes only when it failed to talk to miCASA.   
#if LINUX
				Logger.DbgLog("A-D Lib:Failed to Connect to miCASA");
#endif
           }

#if LINUX
           if (gkEngine != null)
           {
          	 XmlNode gkSecrets = gkEngine.Aggregate();
	         if( null != gkSecrets )
	         {
        	     XmlNode gkImportedNode = ccf.ImportNode(gkSecrets,true);
	             ccf.DocumentElement.AppendChild(gkImportedNode);
	         }
	         else
        	 {
		      Logger.DbgLog("A-D Lib:Failed to Connect to Gnome Keyring");

	         }
           }


           if(kwEngine != null )
           {
           	XmlNode KwEnum  =  kwEngine.Aggregate();
	        if (KwEnum != null) //Atleast <KWallet> should come incase of no secrets
	        {
	             XmlNode kwImported = ccf.ImportNode(KwEnum,true);
	             ccf.DocumentElement.AppendChild(kwImported);
        	}
	        else
        	{
	             // Null comes only when it failed to talk to Kwallet.
		      Logger.DbgLog("A-D Lib:Failed to Connect to KWallet");
	        }
           }

#endif


           return ccf;
                    
       }


       public int SetSecret(XmlNode secret, int StoreID)
       {
           //TBD: Check for Store ID and call the right DataEngine.
            if (StoreID == ConstStrings.CASA_STORE_MICASA)
                   return micasaengine.SetSecret(secret);
            else
            {
#if LINUX
                Logger.DbgLog("A-D Lib:Failed to Set Secret in to  miCASA");
#endif
                return -1;

            }

       }

       public  int GetSecret(XmlNode secret, int StoreID)
       {
           
           //TBD: Check for Store ID and call the right DataEngine.
           if (StoreID == ConstStrings.CASA_STORE_MICASA)
	           return micasaengine.GetSecret(secret);
           else
           { 
#if LINUX
              Logger.DbgLog("A-D Lib: Failed to Get Secret in to  miCASA");
#endif
               return -1;
           }
       }



      
       public int Remove(XmlNode secret, int StoreID)
       {
           
           //TBD: Check for Store ID and call the right DataEngine.
          return  micasaengine.Remove(secret);

       }  
         
       public int AggregateStore(XmlDocument outDoc, int StoreID)
       {
           // This need not be policy aware. GUI knows what its doing.

           XmlNode secEnum;
           DataEngine engine;         

           XmlNode toproot =  outDoc.DocumentElement;
           if (toproot == null)
           {
                XmlElement elem = outDoc.CreateElement("CCF");
                outDoc.AppendChild(elem);
           }

           if ( StoreID  == ConstStrings.CASA_STORE_MICASA )    // If its miCASA
           { 
               engine =  micasaengine;
               secEnum = engine.Aggregate();
               if (secEnum != null )
               {
                   XmlNode root = outDoc.DocumentElement;
                   XmlNodeList miCASANodes = root.SelectNodes("descendant::miCASA");
                  // Console.WriteLine("ADLIB: Count is " + miCASANodes.Count);
                   if (miCASANodes.Count !=  0) // If there is something remove it
                   {
                        root.RemoveChild(miCASANodes[0]);
                   }
                   XmlNode gotit = outDoc.ImportNode(secEnum,true);
                   root.AppendChild(gotit);
                   return  ConstStrings.CASA_SUCCESS;
               }
               else
                   return  ConstStrings.CASA_STORE_NOT_AVAILABLE;
           } 
#if LINUX
           else if( StoreID == ConstStrings.CASA_STORE_GK )
           {
                  gkEngine = new GKEngine();
               	  secEnum = gkEngine.Aggregate();
              	  if (secEnum != null )
               	  {
                     	  XmlNode root = outDoc.DocumentElement;
	                  XmlNodeList gkNode = root.SelectNodes("descendant::GK");
        	          if (gkNode.Count !=  0) // If there is something remove it
                	  {
	                  	root.RemoveChild(gkNode[0]); 
        	          }
	                  XmlNode gkImportNode = outDoc.ImportNode(secEnum,true);
        	          root.AppendChild(gkImportNode);
                	  return  ConstStrings.CASA_SUCCESS;
                 }
                 else
                   return  ConstStrings.CASA_STORE_NOT_AVAILABLE;
              
           }
           else if(StoreID == ConstStrings.CASA_STORE_KWALLET)
           {
                      kwEngine = new KWalletEngine();
	       	       secEnum = kwEngine.Aggregate();
	               if (secEnum != null )
        	       {
                   		XmlNode root = outDoc.DocumentElement;
	                	XmlNodeList gkNode = root.SelectNodes("descendant::KWallet");
                                if (gkNode.Count !=  0) // If there is something remove it
  	                        {
        		        	root.RemoveChild(gkNode[0]);
                                } 
 		                XmlNode kwImportNode = outDoc.ImportNode(secEnum,true);
 		                root.AppendChild(kwImportNode);
 		                return  ConstStrings.CASA_SUCCESS;
               	       }
                       else
                       {
                          Console.WriteLine("KWallet some issue");
 	                  return  ConstStrings.CASA_STORE_NOT_AVAILABLE;
                       }

           } 

           Logger.DbgLog("A-D Lib: Unknown Operation Requested");
#endif           
           return ConstStrings.CASA_OPERATION_FAILED;
       }

       public int InitAD()
       {

           return ConstStrings.CASA_SUCCESS;
       }


       public int CleanUP_AD()
       {

           return ConstStrings.CASA_SUCCESS;
       }		
    }
}