#ifndef _xbCOOKIE_
#define _xbCOOKIE_
/*<<<***************************************************************************
The MIT License (MIT)

Copyright(C) 2013 - 2015 Marc Mirti Mancinelli, Christophe Lardau and Philippe Pamart (xdcube.org)

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.
////////////////////////////////////////////////////////////////////////////////
#Title: xbCookie
#Project: xdcBase
////////////////////////////////////////////////////////////////////////////////
#Author: Marc MIRTI MANCINELLI
#History:
<18/08/2013: creation.
12/02/2014: this notice inserted.
***************************************************************************>>>*/

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

//#ifndef _xbOBJECTS_
//#	include "xbObjects.h"
//#endif

#ifndef _xbENGINE_
#	include "xbEngine.h"
#endif

#ifndef _xbCONFIG_
#	include "xbConfig.h"
#endif

#ifndef _xbDATETIME_
#	include "xbDateTime.h"
#endif

#define xbCOOKIE_MAJ	0xCAACEEDF
#define xbCOOKIE_VER	0x00010001

#define xbCOOKIE_PATH	(xbXDCDATA_PATH xbPath_SLASH "Cookies")

class xbEXPORT __declspec(uuid("{AF28E7CA-E9A9-4597-9823-AA7FD2DF8941}"))
	xbCookieEngine:public xbEngine
{
public:
	xbCookieEngine(const char *pLabel,const xbEngines &engUsed = xbEngines())
		:xbEngine(xbEngineStart_OnUse,pLabel,engUsed)
	{
		m_bMScookieSupport=true;
//		m_pCookieNewest = m_pCookieOldest = NULL;
	}
	virtual ~xbCookieEngine()
	{
		Destroy();
	}
	virtual bool GetInterface(REFIID riid, void ** ppInterf)
	{
		xbImplInterface(xbCookieEngine);
		return xbEngine::GetInterface(riid,ppInterf);
	}

	void SetMScookieSupport(bool bFlg) {m_bMScookieSupport = bFlg;}
	bool GetMScookieSupport() const {return m_bMScookieSupport;}

	xbString GetValueList(const xbString &url,const xbDateTime *pDtNow = NULL)
	{
		xbLock lckStart(this);
		return getValueList(url,pDtNow);
	}
	void SetValue(const xbString &url, const xbString &val,const xbDateTime *pDtNow = NULL)
	{
		xbLock lckStart(this);
		setValue(url,val,pDtNow);
	}
	xbString GetCookiePath();
	void SetCookiePath(const xbString &ckPath);

	virtual bool isLockedByMe() const {return m_sect.IsLockedByMe();}
	mutable xbSectShare	m_sect;
protected:
	virtual void onStart()
	{
	}
	virtual bool onStop(bool) 
	{
		xbSectLock	lck(&m_sect);
		m_arrCookie.Empty();
		return true;
	}

	bool		m_bMScookieSupport;
	xbString	m_cookiePath;

	xbString getValueList(const xbString &url,const xbDateTime *pDtNow);
	void setValue(const xbString &url,const xbString &val,const xbDateTime *pDtNow);
	void getMScookieValueList(const xbString &url,xbString *pRet);
	void getValueListForPathRec(const xbString &domain,const xbString &path,const xbDateTime &dtNow,xbString *pRet);
	void getValueListForPath(const xbString &domain,const xbString &path,const xbDateTime &dtNow,xbString *pRet);

	void setMScookieValue(const xbString &url,const xbString &val);
	/*val syntax:
	<name>=<value>[; <name>=<value>]
	[; expires=<date>][; domain=<domain_name>]
	[; path=<some_path>][; secure][; HttpOnly]
	*/
public:
	struct CookieItem
	{
		xbString	m_name,m_value;
		xbDateTime		m_expire;
	};
	struct CookieSet
	{
		xbString	m_domain;
		xbString	m_path;
//		CookieSet	*m_pNext,*m_pPrev;

		xbArray<CookieItem> m_arrItem;
		int			m_cItemPersist;
		xbDateTime		m_fileDtModif;

		CookieSet()
		{
			m_cItemPersist=0;
//			m_pNext=NULL;
//			m_pPrev=NULL;
		}
		CookieSet(const xbString &domain,const xbString &path)
			:m_domain(domain),m_path(path)
		{
			m_cItemPersist=0;
//			m_pNext=NULL;
//			m_pPrev=NULL;
		}
		void ItemDel(int pos,bool bPersist)
		{
			m_arrItem.Delete(pos);
			if (!bPersist)
				return;
			xbRdbgAssert(m_cItemPersist>0);
			m_cItemPersist--;
		}
		void ItemAdd(const xbString &name, const xbString &val, const xbDateTime *pDtExpire = NULL,const xbDateTime *pDtNow = NULL,bool bLoad = false);

		xbHeapDnewImpl(CookieSet,g_xbEngineCenter.m_heap);
	};
protected:
	static int cookieItemFindCb(const CookieItem &item, const void *pName);
	static int cookieSetFindCb(CookieSet * const &pSet1, const void *pSet2);

	xbPtrArray<CookieSet> m_arrCookie;
//	CookieSet	*m_pCookieNewest,*m_pCookieOldest;

	CookieSet	*cookieSetFind(const xbString &domain,const xbString &path,xbSectLock *pLck,bool bCreate = false);
	bool cookieSetSave(CookieSet *pCookie,xbSectLock &lck);
	void cookieSetAdd(const CookieSet &cookieSet,const xbDateTime &dtNow);
	bool cookieSetUpdate(CookieSet *pCookie,xbSectLock &lck);
	bool cookieSetLoad(const CookieSet *pCookie,int insertPos,xbSectLock &lck,CookieSet **ppRetCookie);
	bool cookieSetDelPersist(CookieSet *pCookie,xbSectLock &lck);

	void cookieSetStreamLoad(CookieSet *pCookie,IxbInputStream *pStm);
	void cookieSetStreamSave(const CookieSet *pCookie,IxbOutputStream *pStm);

	xbString cookieGetPath(const CookieSet *pCookie,bool bCreatePath = false);
	bool cookieAlreadyInStr(const xbString &itemName,const xbString &ret);
};

extern xbCookieEngine	xbEXPORT g_xbCookieEngine;

#endif
