package de.sopa.scene.menu;

import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

import de.sopa.scene.BaseScene;

import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.modifier.AlphaModifier;
import org.andengine.entity.modifier.MoveXModifier;
import org.andengine.entity.modifier.MoveYModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.entity.text.Text;
import org.andengine.util.color.Color;

/**
 * @author David Schilling - davejs92@gmail.com
 * @author Raphael Schilling
 */
public class MainMenuScene extends BaseScene {

    private static final String SOPA_TWITTER_URL = "https://twitter.com/sopagame";
    private static final String LINK_TO_STORE = "https://play.google.com/store/apps/details?id=de.sopa";

    @Override
    public void createScene(Object o) {
        createBackground();
        createMenuChildScene();
        resourcesManager.musicService.playMusic();
    }


    @Override
    public void onBackKeyPressed() {
        System.exit(0);
    }


    @Override
    public void disposeScene() {
        final MainMenuScene mainMenuScene = this;

        engine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() {
            public void onTimePassed(final TimerHandler pTimerHandler) {
                engine.unregisterUpdateHandler(pTimerHandler);
                mainMenuScene.detachChildren();
            }
        }));
    }

    private void createBackground() {
        setBackground(new Background(Color.BLACK));
    }

    private void createMenuChildScene() {

        final ButtonSprite playItemSprite = new ButtonSprite(0, 0, resourcesManager.level_mode_region, vbom, new ButtonSprite.OnClickListener() {
            @Override
            public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
                sceneService.loadLevelChoiceSceneFromMenuScene();
            }
        });

        final ButtonSprite settingsSprite = new ButtonSprite(0, 0, resourcesManager.settingsRegion, vbom, new ButtonSprite.OnClickListener() {
            @Override
            public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
                sceneService.loadSettingsFromMenuScene();
            }
        });

        final ButtonSprite creditsButton = new ButtonSprite(0, 0, resourcesManager.creditsRegion, vbom, new ButtonSprite.OnClickListener() {
            @Override
            public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
                sceneService.loadCreditsFromMenuScene();
            }
        });

        final ButtonSprite twitterLogo = new ButtonSprite(0, 0, resourcesManager.twitterLogoRegion, vbom, new ButtonSprite.OnClickListener() {
            @Override
            public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
                final Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(SOPA_TWITTER_URL));
                activity.startActivity(intent);
            }
        });
        final ButtonSprite shareLogo = new ButtonSprite(0, 0, resourcesManager.shareLogoTexture, vbom, new ButtonSprite.OnClickListener() {
            @Override
            public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(activity.getApplicationContext(), "Feature disabled for beta tests.", Toast.LENGTH_LONG).show();
                    }
                });
            }
        });

        final Text sopaText = new Text(camera.getWidth()/2 - 595/2, camera.getHeight() * 0.07f, resourcesManager.sopaFont, "SOPA", vbom);

        engine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() {
            public void onTimePassed(final TimerHandler pTimerHandler) {
                engine.unregisterUpdateHandler(pTimerHandler);
                playItemSprite.registerEntityModifier(new MoveXModifier(1f, -camera.getWidth(), 0));
                settingsSprite.registerEntityModifier(new MoveXModifier(1f, camera.getWidth(), 0));
                creditsButton.registerEntityModifier(new MoveYModifier(1f, camera.getHeight(), camera.getHeight() * 0.4f + creditsButton.getHeight()));
                shareLogo.registerEntityModifier(new AlphaModifier(1f, 0f, 1f ));
                twitterLogo.registerEntityModifier(new AlphaModifier(1f, 0f, 1f ));
                shareLogo.registerEntityModifier(new MoveXModifier(1f, -shareLogo.getWidth(), camera.getWidth() * 0.05f));
                twitterLogo.registerEntityModifier(new MoveXModifier(1f, camera.getWidth(), camera.getWidth() * 0.65f));
                sopaText.registerEntityModifier(new ScaleModifier(1f, 0f, 1f));
            }
        }));

        playItemSprite.setPosition(1080, camera.getHeight() * 0.4f - playItemSprite.getHeightScaled());
        attachChild(playItemSprite);
        registerTouchArea(playItemSprite);

        settingsSprite.setPosition(1080, camera.getHeight() * 0.4f);
        attachChild(settingsSprite);
        registerTouchArea(settingsSprite);

        attachChild(creditsButton);
        registerTouchArea(creditsButton);

        twitterLogo.setPosition(1080, camera.getHeight() * 0.8f);
        attachChild(twitterLogo);
        registerTouchArea(twitterLogo);

        shareLogo.setPosition(1080, camera.getHeight() * 0.8f);
        attachChild(shareLogo);
        registerTouchArea(shareLogo);

        sopaText.setScale(0f);
        attachChild(sopaText);
    }
}

@@ -80,37 +80,8 @@
 Uri;
-%0Aimport android.widget.Toast;
 %0A%0Aim
@@ -3571,293 +3571,340 @@
     
-activity.runOnUiThread(new Runnable() %7B%0A                    @Override%0A                    public void run() %7B%0A                        Toast.makeText(activity.getApplicationContext(), %22Feature disabled for beta tests.%22, Toast.LENGTH_LONG).show();%0A                    %7D%0A                %7D
+Intent shareIntent = new Intent();%0A                shareIntent.setAction(Intent.ACTION_SEND);%0A                shareIntent.setType(%22text/plain%22);%0A                shareIntent.putExtra(Intent.EXTRA_TEXT, %22I played SOPA: %22 + LINK_TO_STORE);%0A                activity.startActivity(Intent.createChooser(shareIntent, %22Share your thoughts%22)
 );%0A 

Revert "disable feature for beta tests"