//
//  IGAHelper.cpp
//  Cocos2dxV3xPlugin
//
//  Created by Subhendu Sekhar Sahu on 29/03/17.
//
//

#include "IGAHelper.h"
#include "IGAData.h"

using namespace pokkt;

static std::map<std::string, IGAAssetInfo*> IGAAssets;
static std::map<std::string, IGAData*> _igaDataList;


void IGAHelper::insertAssetInfo(std::string key, IGAAssetInfo *assetInfo)
{
    IGAAssets.insert(std::pair<std::string, IGAAssetInfo *>(key,assetInfo));
}

std::map<std::string, IGAAssetInfo *> IGAHelper::getIGAAssets()
{
    return IGAAssets;
}

IGAAssetInfo * IGAHelper::getIGAAssetInfoWithKey(std::string assetKey)
{
    return IGAAssets[assetKey];
}

void IGAHelper::AddToPlaced(IGAAssetInfo *assetInfo, int count)
{
//    string size = assetInfo.Width + "x" + assetInfo.Height;
//    if (!_igaDataList.ContainsKey(size))
//        _igaDataList.Add(size, new IGAData());
//    
//    _igaDataList [size].assetInfo = assetInfo;
//    _igaDataList [size].Placed += count;
    
    SyncData ();
}

void IGAHelper::AddToSeen(IGAAssetInfo *assetInfo, int count)
{
//    string size = assetInfo.Width + "x" + assetInfo.Height;
//    if (!_igaDataList.ContainsKey(size))
//        _igaDataList.Add(size, new IGAData());
//    
//    _igaDataList [size].assetInfo = assetInfo;
//    _igaDataList [size].Seen += count;
    
    SyncData ();
}

void IGAHelper::AddToCollected(IGAAssetInfo *assetInfo, int count)
{
//    string size = assetInfo.Width + "x" + assetInfo.Height;
//    if (!_igaDataList.ContainsKey(size))
//        _igaDataList.Add(size, new IGAData());
//    
//    _igaDataList [size].assetInfo = assetInfo;
//    _igaDataList [size].Collected += count;
    
    SyncData ();
}

void IGAHelper::SyncData()
{
//    JSONObject jo = new JSONObject (JSONObject.Type.ARRAY);
//    
//    foreach (var entry in _igaDataList)
//    {
//        JSONObject igaData = new JSONObject ();
//        igaData.AddField ("offerId", entry.Value.assetInfo.OfferId);
//        igaData.AddField ("width", entry.Value.assetInfo.Width);
//        igaData.AddField ("height", entry.Value.assetInfo.Height);
//        igaData.AddField ("placed", entry.Value.Placed);
//        igaData.AddField ("seen", entry.Value.Seen);
//        igaData.AddField ("collected", entry.Value.Collected);
//        igaData.AddField ("error", entry.Value.assetInfo.ErrorInfo);
//        jo.Add (igaData);
//        
//        //jo.AddField ("size", entry.Key);
//        //jo.AddField ("data", igaData);
//    }
    
    // TODO: put a timer for syncing
//    PokktAds.Analytics.UpdateIGAData(jo.ToString ());

}
