//
//  PokktIGAContainer.cpp
//  Cocos2dxV3xPlugin
//
//  Created by Subhendu Sekhar Sahu on 30/03/17.
//
//

#include "PokktIGAContainer.h"
#include "IGAHelper.h"


using namespace pokkt;

void PokktIGAContainer::Start()
{
    
}
void PokktIGAContainer::Update()
{
//    // TODO: check with any other active camera
//    PlayerCamera = PlayerCamera ?? Camera.main;
//    if (_isPlaced && !_isSeen && PlayerCamera.isActiveAndEnabled)
//    {
//        Vector3 screenPoint = PlayerCamera.WorldToViewportPoint(transform.position);
//        _isSeen = screenPoint.z > 0 && screenPoint.x > 0 && screenPoint.x < 1 && screenPoint.y > 0 && screenPoint.y < 1;
//        if (_isSeen)
//        {
//            Debug.Log ("object is seen, object: " + name);
//            IGAHelper.AddToSeen (_assetInfo, 1);
//        }
//    }

}

void PokktIGAContainer::notifyCollected()
{
//    if (!_isPlaced)
//        return;
//    
//    if (_isCollected)
//        return;
//    
//    Debug.Log ("object is collected, object: " + name);
//    _isCollected = true;
//    IGAHelper.AddToCollected (_assetInfo, 1);
}

void PokktIGAContainer::AssignAsset()
{
    
    //    _isPlaced = false;
    //
    //    string assetKey = "";
    //    switch (IGAObject.AdType)
    //    {
    //        case PokktIGAType.Hoarding:
    //            assetKey = "256x256";
    //            break;
    //
    //        case PokktIGAType.Banner:
    //            assetKey = "128x128";
    //            break;
    //
    //        case PokktIGAType.Collectible:
    //        case PokktIGAType.Boost:
    //        default:
    //            Debug.Log("failed to get IGA for container of type: " + IGAObject.AdType + ", using default!");
    //            assetKey = "32x32";
    //            break;
    //    }

    
    std::string assetKey = "";
    
    switch (IGAObject->adType)
    {
        case Hoarding:
            assetKey = "512x512";
            break;
            
        case Banner:
            assetKey = "128x128";
            break;
            
        case Collectible:
            
            break;
            
        case Boost:
            
            break;
            
        default:
            break;
    }
    
    if (assetKey == "")
    {
        CCLOG("Asset key marker is invalid");
    }
    
    
    _assetInfo = IGAHelper::getIGAAssetInfoWithKey(assetKey);

    
    
//
//    txture
//   // cocos2d::Texture2D *texture = cocos2d::TextureCache::getInstance()->addImage(_assetInfo->Path);
//    //(_assetInfo->Path);
//
//    
//   // cocos2d::TextureCache::getInstance()->getTextureFilePath(<#cocos2d::Texture2D *texture#>)
//    
////    cocos2d::Texture2D *texture = cocos2d::TextureCache::getInstance()->getT
////    texture->initWithImage(textureImage);
////    
//    this->setTexture(texture);
//
 //   setTexture(texture);
    
    
    
    
//    fileUtils~~>addSearchPath;
//    fileUtils~~>addSearchPath(“data”);
//    std::string filePath = “images” + Seperator + “logo”;
//    fileUtils->addSearchPath(filePath.c_str());@
    //Sprite *imageSprite = Sprite::sp
    
    
    
    //
    //    if (String.IsNullOrEmpty(assetKey))
    //    {
    //        Debug.Log("asset key marker is invalid: " + assetKey);
    //        return;
    //    }
    //
    //    if (IGAHelper.IGAAssets == null || IGAHelper.IGAAssets.Count < 1)
    //    {
    //        Debug.Log("IGA Assets not available!");
    //        return;
    //    }
    //
    //    if (!IGAHelper.IGAAssets.ContainsKey(assetKey))
    //    {
    //        Debug.Log("asset not available for: " + assetKey);
    //        return;
    //    }

    
    
//TODO: Need to check asset is available or not
    
//    if (IGAHelper::getIGAAssets() == NULL)
//    {
//        
//    }
    
    
    LoadImageInContainer();
    
    
    
    
//
//    _assetInfo = IGAHelper.IGAAssets [assetKey];
//    Debug.Log("filling container for: " + assetKey + ", image path: " + _assetInfo.Path);
//    
//    if (String.IsNullOrEmpty(_assetInfo.Path))
//    {
//        Debug.Log("asset path is invalid: " + assetKey);
//        return;
//    }
//    
//    StartCoroutine(LoadImageInContainer());

}

//public IEnumerator LoadImageInContainer()

void PokktIGAContainer::LoadImageInContainer() //TODO: Need to know Load Image In Container
{
    this->initWithFile(_assetInfo->Path);
    
//    cocos2d::Image *textureImage = new cocos2d::Image();
//    textureImage->initWithImageFile(_assetInfo->Path);
//    
//    
//    cocos2d::FileUtils *fullPath = cocos2d::FileUtils::sharedFileUtils()->fullPathFromRelativePath(s_LanguagesPath);
//    
//    fileData = CCFileUtils::sharedFileUtils()->getFileData(fullPath.c_str(), "r", &filesize);
//    content.append((char*)fileData);
//    CCLog(content.c_str());
//    
//    cocos2d::FileUtils *fileUtils = cocos2d::FileUtils::getInstance();
//    fileUtils->addSearchPath(_assetInfo->Path.c_str());

    
    
    //    fileUtils->addSearchPath(assetKey);
    //
//    cocos2d::Image *textureImage = new cocos2d::Image();
//    textureImage->initWithImageFile(_assetInfo->Path);

    
//    cocos2d::Texture2D *texture = new cocos2d::Texture2D();
//    texture->initWithImage(textureImage);
//  //  this->createWithTexture(<#cocos2d::Texture2D *texture#>)
//    this->setTexture(texture);
    
//    cocos2d::FileUtils *fileUtils = cocos2d::FileUtils::getInstance();
//    fileUtils->addSearchPath(_assetInfo->Path);
//    
//    cocos2d::Texture2D *texture = cocos2d::TextureCache::getInstance()->addImage(_assetInfo->Path);
//    
//    setTexture(texture);


//    fileUtils~~>addSearchPath;
//    fileUtils~~>addSearchPath(“data”);
//    std::string filePath = “images” + Seperator + “logo”;
//    fileUtils->addSearchPath(filePath.c_str());@
    
//    string prefix = Application.platform == RuntimePlatform.Android ? "file://" : "file://";
//    string filePath = prefix + _assetInfo.Path.Replace(@"\/", "/");
//    
//    WWW w = new WWW(filePath);
//    if (w.error != null && w.error.Length > 0)
//    {
    
//        Debug.Log("www error: " + w.error);
//        _assetInfo.ErrorInfo = w.error;
//        yield break;
//    }
//    
//    yield return w;
//    Texture2D texture = w.texture;
//    Debug.Log(
//              "texture is: " + w.texture + " (" + w.texture.dimension + ") " +
//              ",\n texture dimensions: " + w.texture.width + "x" + w.texture.height +
//              ",\n size in bytes: " + w.bytes.Length);
//    GetComponent<Renderer>().material.mainTexture = texture;
//    
//    _isPlaced = true;
//    
//    // as this is new, mark it not seen and not yet collected(if needed)
//    _isSeen = false;
//    _isCollected = false;
//    _assetInfo.ErrorInfo = "";
//    
//    // update count
//    IGAHelper.AddToPlaced(_assetInfo, 1);
}

PokktIGAContainer* PokktIGAContainer::AddIGAContainer(cocos2d::Node * go)
{
    
    
    PokktIGAContainer * container  = (PokktIGAContainer *) go;
    container->AssignAsset();
    
    return container;
    
    
//    if (go == null)
//        return null;
//    
//    if (go.GetComponent<PokktIGAContainer>() != null)
//    {
//        Debug.Log("IGA Container already added to gameObject: " + go.name);
//        return go.GetComponent<PokktIGAContainer>();
//    }
//    
//    PokktIGAContainer container = go.AddComponent<PokktIGAContainer>();
//    if (container.IGAObject == null)
//        container.IGAObject = new PokktIGAProperties();
//    
//    // add a default Pokkt Logo to it
//    //Material mat = new Material(Shader.Find("Diffuse"));
//    //mat.SetTexture("_MainTex",Resources.Load("logo") as  Texture);
//    //go.GetComponent<Renderer>().sharedMaterial = Resources.Load<Material>("logo");
//    
//    // this will try to assign an available asset to this container
//    container.AssignAsset();
//    
//    return container;
}

PokktIGAContainer* PokktIGAContainer::AddIGAHoarding(cocos2d::Node * go)
{
    if (go == NULL)
        return NULL;
    
    PokktIGAContainer *container = (PokktIGAContainer *) go;
    container->IGAObject = new PokktIGAProperties();
    container->IGAObject->adType = Hoarding;

    PokktIGAContainer *container1 = AddIGAContainer(container);

    
    return container1;
}

PokktIGAContainer* PokktIGAContainer::AddIGABanner(cocos2d::Node * go)
{
    if (go == NULL)
        return NULL;
    
    PokktIGAContainer *container = AddIGAContainer(go);
    container->IGAObject->adType = Banner;
    
    return container;
}

PokktIGAContainer* PokktIGAContainer::AddIGACollectible(cocos2d::Node * go)
{
    if (go == NULL)
        return NULL;
    
    PokktIGAContainer *container = AddIGAContainer(go);
    container->IGAObject->adType = Collectible;
    
    return container;
}

PokktIGAContainer* PokktIGAContainer::AddIGABoost(cocos2d::Node * go)
{
    if (go == NULL)
        return NULL;
    
    PokktIGAContainer *container = AddIGAContainer(go);
    container->IGAObject->adType = Boost;
    
    return container;
}
