#include "NameEntryScene.h"
#include "ChatScene.h"

USING_NS_CC;

std::string NameEntry::m_MyName = "";

Scene* NameEntry::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();

    // 'layer' is an autorelease object
    auto layer = NameEntry::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool NameEntry::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Point origin = Director::getInstance()->getVisibleOrigin();

    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png",
                                           "CloseSelected.png",
                                           CC_CALLBACK_1(NameEntry::menuCloseCallback, this));

    closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));

    // create menu, it's an autorelease object
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Point::ZERO);
    this->addChild(menu, 1);

    // GUI
    // 文字列ラベル
    auto label = Label::createWithSystemFont("名前", "arial", 24);
    label->setAnchorPoint(Point(0.0f, 1.0f));
    label->setPosition(Point(0, visibleSize.height));
    label->setHorizontalAlignment(cocos2d::TextHAlignment::LEFT);
    label->setVerticalAlignment(cocos2d::TextVAlignment::TOP);
    this->addChild(label);

    // 入力欄(枠)
    auto draw = CCDrawNode::create();
    draw->setPosition(Point(0,0));
    this->addChild(draw);
    static Point points1[] = { Point(0,visibleSize.height-32), Point(visibleSize.width, visibleSize.height-32), Point(visibleSize.width, visibleSize.height-64), Point(0, visibleSize.height-64) };
    draw->drawPolygon(points1, 4, ccc4FFromccc3B(ccWHITE), 1, ccc4FFromccc3B(ccWHITE));

    // 入力欄(テキストボックス)
    m_pEditBox = cocos2d::extension::EditBox::create(Size(visibleSize.width, 96), cocos2d::extension::Scale9Sprite::create("image.png"));
    m_pEditBox->setAnchorPoint(Point(0.0f, 1.0f));
    m_pEditBox->setPosition(Point(0,visibleSize.height));
    m_pEditBox->setFont("Arial", 24);
    m_pEditBox->setPlaceHolder("名前を入力してください");
    m_pEditBox->setPlaceholderFontColor(Color3B::BLACK);
    m_pEditBox->setFontColor(Color3B::BLACK);
    m_pEditBox->setMaxLength(100);
    m_pEditBox->setReturnType(cocos2d::extension::EditBox::KeyboardReturnType::DONE);
    m_pEditBox->setInputMode(cocos2d::extension::EditBox::InputMode::ANY);
    this->addChild(m_pEditBox);

    // チャット開始ボタン(枠)
    auto draw2 = CCDrawNode::create();
    draw2->setPosition(Point(0,0));
    this->addChild(draw2);
    static Point points2[] = { Point(visibleSize.width/2-100,visibleSize.height-128), Point(visibleSize.width/2+100, visibleSize.height-128), Point(visibleSize.width/2+100, visibleSize.height-160), Point(visibleSize.width/2-100, visibleSize.height-160) };
    draw2->drawPolygon(points2, 4, ccc4FFromccc3B(ccGRAY), 1, ccc4FFromccc3B(ccGRAY));

    // チャット開始ボタン(本体)
    auto chatStartLabel = Label::createWithSystemFont("チャット開始", "Arial", 24);
    chatStartLabel->setColor(Color3B::BLACK);
    auto chatStartItem = MenuItemLabel::create(chatStartLabel, CC_CALLBACK_1(NameEntry::menuStartChatCallback, this));
    chatStartItem->setPosition(Point(visibleSize.width/2, visibleSize.height-144));
    auto menu2 = Menu::create(chatStartItem, NULL);
    menu2->setPosition(Point::ZERO);
    this->addChild(menu2, 1);


#if defined( _IOS ) || defined( _ANDROID )
    // タッチイベントの有効化
    auto listener = EventListenerTouchOneByOne::create();
    listener->setSwallowTouches( true );

    listener->onTouchBegan = CC_CALLBACK_2( NameEntry::onTouchBegan, this );
    listener->onTouchEnded = CC_CALLBACK_2( NameEntry::onTouchEnded, this );

    auto dispatcher = Director::getInstance()->getEventDispatcher();
    dispatcher->addEventListenerWithSceneGraphPriority( listener, this );
#endif

    //  ゲームループの開始
    scheduleUpdate();

    return true;
}

/**
 *  ゲームループです。タイマー割り込みで周期的に実行します。
 *
 *  @param  delta   変化時間
 */
void NameEntry::update(float delta)
{

}

void NameEntry::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
    MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    return;
#endif

    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

void NameEntry::menuStartChatCallback(Ref* pSender)
{
    m_MyName = m_pEditBox->getText();
    if( m_MyName.length() > 0 )
    {
        Director::getInstance()->replaceScene(Chat::createScene());
    }
}