#include "DemoSelectorView.h"


/**
 * HELPER: getAdConfigForScreen
 *
 * this is just for this Demo-purpose, you should have
 * your own adConfig properly mainatined
 **/

static DemoSelectorView* viewInstance = NULL;

bool DemoSelectorView::init()
{
    if (!BaseView::init())
        return false;
    
    backButton->setVisible(false);
    
    int yPos = logo->getPosition().y - logo->getBoundingBox().size.height / 2 + 50;
    
    
    // create new UI
    
    _videoAds = createButton("Video Ads", CC_CALLBACK_1(DemoSelectorView::handleVideoAdsPokkt, this));
    
    _videoAds->setPosition(Vec2(visibleSize.width / 2, yPos));
    _videoAds->setScale(1.0f);
    this->addChild(_videoAds);
    
    yPos -= 100;
    
    _interstitialAds = createButton("Interstitial Ad", CC_CALLBACK_1(DemoSelectorView::handleInterstitialAdsPokkt, this));
    _interstitialAds->setPosition(Vec2(visibleSize.width / 2, yPos));
    this->addChild(_interstitialAds);
    
    yPos -= 100;
    
    _bannerAds = createButton("Banner Ad", CC_CALLBACK_1(DemoSelectorView::handleBannerAdsPokkt, this));
    _bannerAds->setPosition(Vec2(visibleSize.width / 2, yPos));
    this->addChild(_bannerAds);
    
    yPos -= 100;
    
    _btnExportLog = createButton("Export Log", CC_CALLBACK_1(DemoSelectorView::handleExportLog, this));
    _btnExportLog->setPosition(Vec2(visibleSize.width / 2, yPos));
    this->addChild(_btnExportLog);
    
    yPos -= 100;

    _appIdEditBox = EditBox::create(Size(3 * visibleSize.width / 4, 70), "whiteButton.png");
    _appIdEditBox->setPlaceHolder("app id");
    _appIdEditBox->setFontColor(Color3B::BLACK);
    _appIdEditBox->setTextHorizontalAlignment(cocos2d::TextHAlignment::CENTER);
    _appIdEditBox->setPosition((Vec2(visibleSize.width / 2, yPos)));
    _appIdEditBox->setFontSize(30);
    _appIdEditBox->setInputMode(EditBox::InputMode::ANY);
    _appIdEditBox->setDelegate(this);
    _appIdEditBox->setReturnType(EditBox::KeyboardReturnType::DONE);
    this->addChild(_appIdEditBox);

    yPos -= 80;

    _secKeyEditBox = EditBox::create(Size(3 * visibleSize.width / 4, 70), "whiteButton.png");
    _secKeyEditBox->setPlaceHolder("security key");
    _secKeyEditBox->setFontColor(Color3B::BLACK);
    _secKeyEditBox->setTextHorizontalAlignment(cocos2d::TextHAlignment::CENTER);
    _secKeyEditBox->setPosition((Vec2(visibleSize.width / 2, yPos)));
    _secKeyEditBox->setFontSize(30);
    _secKeyEditBox->setInputMode(EditBox::InputMode::ANY);
    _secKeyEditBox->setDelegate(this);
    _secKeyEditBox->setReturnType(EditBox::KeyboardReturnType::DONE);
    this->addChild(_secKeyEditBox);
    
    yPos -= 100;
    
    _btnExportLog = createButton("SAVE", CC_CALLBACK_1(DemoSelectorView::handleSaveConfig, this));
    _btnExportLog->setPosition(Vec2(visibleSize.width / 2, yPos));
    this->addChild(_btnExportLog);
    
    yPos -= 100;
    
    Text* label = BaseView::createLabel("GDPR Enabled", 35, Color3B::BLACK, Vec2(visibleSize.width / 2, yPos));
    label->setTextHorizontalAlignment(TextHAlignment::LEFT);
    this->addChild(label);

    _gdprApplicableCheck = CheckBox::create("check_box_normal.png", "check_box_active_press.png");
    _gdprApplicableCheck->setPosition((Vec2(visibleSize.width / 2 - 200, yPos)));
    _gdprApplicableCheck->addEventListener(CC_CALLBACK_1(DemoSelectorView::handleGDPRSettingsChanged, this));
    this->addChild(_gdprApplicableCheck);
    
    yPos -= 100;
    
    label = BaseView::createLabel("GDPR Consent Available", 35, Color3B::BLACK, Vec2(visibleSize.width / 2 + 75, yPos));
    label->setTextHorizontalAlignment(TextHAlignment::LEFT);
    this->addChild(label);
    
    _gdprConsentAvailableCheck = CheckBox::create("check_box_normal.png", "check_box_active_press.png");
    _gdprConsentAvailableCheck->setPosition((Vec2(visibleSize.width / 2 - 200, yPos)));
    _gdprConsentAvailableCheck->addEventListener(CC_CALLBACK_1(DemoSelectorView::handleGDPRSettingsChanged, this));
    this->addChild(_gdprConsentAvailableCheck);

    yPos -= 100;
    
    _btnExportLog = createButton("Set User Details", CC_CALLBACK_1(DemoSelectorView::handleSetUserDetails, this));
    _btnExportLog->setPosition(Vec2(visibleSize.width / 2, yPos));
    this->addChild(_btnExportLog);

    return true;
}

DemoSelectorView::~DemoSelectorView()
{
    
}


DemoSelectorView* DemoSelectorView::getView()
{
    if (viewInstance == NULL)
    {
        viewInstance = DemoSelectorView::create();
        viewInstance->retain();
    }
    return viewInstance;
}



void DemoSelectorView::handleBackPress(cocos2d::Ref *pSender)
{
    // exit the app
    Director::getInstance()->end();
}

void DemoSelectorView::handleVideoAdsPokkt(Ref* pSender)
{
    ChangeViewParams params;
    params.viewToChange = VideoAdsView;
    changeView(params);
}

void DemoSelectorView::handleInterstitialAdsPokkt(Ref* pSender)
{
    ChangeViewParams params;
    params.viewToChange = InterstitialAdsView;
    changeView(params);
}

void DemoSelectorView::handleBannerAdsPokkt(Ref* pSender)
{
    ChangeViewParams params;
    params.viewToChange = BannerAdsView;
    changeView(params);
}

void DemoSelectorView::handleGDPRSettingsChanged(Ref* pSender)
{
    CCLOG("gdpr settings changed!");
    
    pokkt::PCPokktConsentInfo info;
    info.GDPRApplicable = this->_gdprApplicableCheck->isSelected();
    info.GDPRConsentAvailable = this->_gdprConsentAvailableCheck->isSelected();
    PCPokktAds::setPokktConsentInfo(info);
}

void DemoSelectorView::handleExportLog(cocos2d::Ref* pSender)
{
    PCPokktAds::exportLog();
}

void DemoSelectorView::handleSaveConfig(Ref* pSender)
{
    std::string appId = _appIdEditBox->getText();
    std::string secKey = _secKeyEditBox->getText();
    CCLOG("updating appId: %s , secKey: %s", appId.c_str(), secKey.c_str());
    PCPokktAds::setPokktConfig(appId, secKey);
}

void DemoSelectorView::handleSetUserDetails(Ref* pSender)
{
    pokkt::PCPokktUserDetails details;
    details.age = "34";
    details.birthday = "01-03-1983";
    details.education = "Masters";
    details.emailAddress = "xyz@abc.com";
    details.employment = "Salaried";
    details.facebookId = "@xyzabc123";
    details.location = "jahannum";
    details.maritalStatus = "single";
    details.maturityRating = "18+";
    details.mobileNo = "111111111";
    details.name = "The Xtreme";
    details.nationality = "Universal";
    details.sex = "Male";
    details.twitterHandle = "@TwitterHandle";
    PCPokktAds::setUserDetails(details);
}

void DemoSelectorView::editBoxEditingDidBegin(EditBox* editBox)
{
    editBox->attachWithIME();
}

void DemoSelectorView::editBoxEditingDidEnd(EditBox* editBox)
{
    
}

void DemoSelectorView::editBoxTextChanged(EditBox* editBox, const std::string& text)
{
    
}

void DemoSelectorView::editBoxReturn(EditBox* editBox)
{
    
}
