// on "init" you need to initialize your instance
bool GameWin::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
   
    Size visiableSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
	

	AppDelegate *app = (AppDelegate*)Application::getInstance();
	  std::string sCSD=app->GetLayoutString();
	 _rootNode = CSLoader::createNode("res/game_passed/GamePassLayer"+sCSD+".csb");

	 //loginLayer=Layer::create();
	 addChild(_rootNode,1);

	 /*
	LayerColor *layer1 = LayerColor::create(Color4B(0, 0, 0, 180));
	TurntableSystem* price =new TurntableSystem(layer1);
	layer1->setTag(CHILD_TURNTABLE);
	addChild(layer1,99);*/
	

	/*
	cocostudio::ArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("Animation0.png", "Animation0.plist", "Animation.ExportJson");
	cocostudio::Armature *armature = cocostudio::Armature::create("Animation");
	armature->getAnimation()->playByIndex(0);
	armature->setScale(0.5f);
	armature->setPosition(ccp(visibleSize.width * 0.5, visibleSize.height * 0.5));
	this->addChild(armature);
	
	*/

	 /*
	cocostudio::Armature* armature;
	cocostudio::ArmatureDataManager::getInstance()->addArmatureFileInfo("res/animations/coin/coin.ExportJson");
	armature = cocostudio::Armature::create("coin");
	if (armature){
		armature->setPosition(ccp(visiableSize.width * 0.8, visiableSize.height * 0.5));
		armature->getAnimation()->playByIndex(0);
		armature->setScale(0.25);
		this->addChild(armature);
	}
	*/
	


	//Get Promotion 
	// Setting HTTP Request Header
 //   if(m_iIsLoad==0){
 //       m_iIsLoad=1;
	//HttpRequest* request = new HttpRequest();
 //   request->setUrl(PromotionWebGetURL);
 //   request->setRequestType(HttpRequest::Type::GET);
 //   request->setResponseCallback(CC_CALLBACK_2(GameWin::onHttpRequestCompleted,this));
 //   request->setTag("GET test");
 //   cocos2d::network::HttpClient::getInstance()->send(request);
 //   request->release();
 //   }

	//ImageView* imgPromotion = dynamic_cast<ImageView*>(_rootNode->getChildByName("ivPromotionImg")); 
	//	 auto card1 = Sprite::create("match_game_img_1_b.jpg");
	// //card1->setPosition(Vec2(0, imgPromotion->getPositionY()+ imgPromotion->getContentSize().height/2));
	//	log("1=%f,%f,%f,%f",imgPromotion->getPositionX(),imgPromotion->getPositionY(),imgPromotion->getContentSize().width,imgPromotion->getContentSize().height);
	//	card1->setAnchorPoint(Vec2(0,0)); 
	//card1->setPosition(Vec2(0, 0));
	// //float zoom_x = visiableSize.width *0.5 / card1->getContentSize().width;
	//// float zoom_y = visiableSize.height * 0.5 / card1->getContentSize().height;
	// //card1->setScale(0.055f);
	// //card1->setScaleY(zoom_y);
	//card1->setScale(0.38f);
	//  imgPromotion->addChild(card1,2,1001);

	m_PrmtPrdct = app->getPromoteProduct();

	if(m_PrmtPrdct.m_iIsLoad == 0){
        
		m_PrmtPrdct.m_iIsLoad=1;
		HttpRequest* request = new HttpRequest();
		request->setUrl(PromotionWebGetURL);
		request->setRequestType(HttpRequest::Type::GET);
		request->setResponseCallback(CC_CALLBACK_2(GameWin::onHttpRequestCompleted,this));
		request->setTag("GET test");
		cocos2d::network::HttpClient::getInstance()->send(request);
		request->release();
    }

	if(_rootNode){
        auto img=_rootNode->getChildByName("ivPromotionImg");
        if(img){
            ImageView* imgPromotion = dynamic_cast<ImageView*>(_rootNode->getChildByName("ivPromotionImg"));
			auto card1 = Sprite::create(m_PrmtPrdct.m_strImage.c_str());
			card1->setAnchorPoint(Vec2(0,0)); 
			card1->setPosition(Vec2(0, 0));
			card1->setScale((imgPromotion->getBoundingBox().size.width / card1->getContentSize().width));
			imgPromotion->addChild(card1,2,1001);
        }
    }
    if(_rootNode && _rootNode->getChildByName("lblProductName")){
        Text* lblProductName=dynamic_cast<Text*>(_rootNode->getChildByName("lblProductName"));// (CCLabelTTF*)this->getChildByTag(2001);
        lblProductName->setString(m_PrmtPrdct.m_strProductName);
    }


	auto btnContine = dynamic_cast<Button*>(_rootNode->getChildByName("btnContine"));
	btnContine->setTag(1001);
	btnContine->addTouchEventListener(CC_CALLBACK_2(GameWin::touchButton, this));	

	auto btnQuit = dynamic_cast<Button*>(_rootNode->getChildByName("btnQuit"));
	btnQuit->setTag(1002);
	btnQuit->addTouchEventListener(CC_CALLBACK_2(GameWin::touchButton, this));	

	auto btnBuyNow = dynamic_cast<Button*>(_rootNode->getChildByName("btnBuyNow"));
	btnBuyNow->setTag(1003);
	btnBuyNow->addTouchEventListener(CC_CALLBACK_2(GameWin::touchButton, this));	

	auto btnShare = dynamic_cast<Button*>(_rootNode->getChildByName("btnShare"));
	btnShare->setTag(1004);
	btnShare->addTouchEventListener(CC_CALLBACK_2(GameWin::touchButton, this));	


	//Show Total User Score
	long l_coins = app->GetUserTotalScore();
	char p[20];
	sprintf(p, "%ld", l_coins);
	string sTotal = p;
	
	iReward = app->getReward();
	iStar = app->getStar();
	char p1[20];
	sprintf(p1, "%ld", iReward);
	string sReward = p1;
	app->setReward(0,0);

	auto lblReward = dynamic_cast<::ui::Text*>(_rootNode->getChildByName("lblReward"));
	if (lblReward)
	{
		lblReward->setString(sReward);
	}
	auto lblScore = dynamic_cast<::ui::Text*>(_rootNode->getChildByName("lblScore"));
	if (lblReward)
	{
		lblScore->setString(sTotal);
	}


	//_rootNode->getChildByName("Img_Box")->setVisible(false);
	auto imgStar = dynamic_cast<ImageView*>(_rootNode->getChildByName(WIN_STAR_FRAME));	
	if (imgStar != nullptr) {
		_rootNode->getChildByName(WIN_STAR_BOX)->setVisible(false);
		if (iStar == 3){
			imgStar->loadTexture(WIN_STAR_3);
		}
		else if (iStar == 2){
			imgStar->loadTexture(WIN_STAR_2);
		}
		else if (iStar == 1){
			imgStar->loadTexture(WIN_STAR_1);
		}
	}
	if (app->getBGMstatus() == BGMusic_flag::ON)
	{
		app->PlayEffect(SOUND_WIN);
	}


		// start ansyc method load the atlas.png
	//Director::getInstance()->getTextureCache()->addImageAsync("MatchBg.png", CC_CALLBACK_1(GameWin::loadingCallBack, this));
	
	/*auto card1 = Sprite::create("match_game_img_1_b.jpg");
	// card1->setPosition(Vec2(visiableSize.width / 2 +88 * generalscalefactor,   visiableSize.height/2 +50 *generalscalefactor));
	 //float zoom_x = visiableSize.width *0.5 / card1->getContentSize().width;
	// float zoom_y = visiableSize.height * 0.5 / card1->getContentSize().height;
	 card1->setScale(150/800);
	 //card1->setScaleY(zoom_y);
	 
	 Image* image = new Image();
	 image->s
	 Texture2D* texture = new Texture2D();
    
	Sprite* sprite = dynamic_cast<Sprite*>(_rootNode->getChildByName("spPromotionImg"));  //spPromotionImg
	sprite->initWithFile("match_game_img_1_b.jpg");

	  this->addChild(card1,1,1001);
	*/


	Sprite* imgBackground = Sprite::create(WIN_BACKGROUND);

	imgBackground->setAnchorPoint(Vec2(0, 0));
	imgBackground->setPosition(Vec2(0, 0));
	this->addChild(imgBackground, -1, 222); 


	syncFlow();//sync with server
	app->PlayEffect(SOUND_GAME_PASS);

    return true;
}