package com.draga.spaceTravels3.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.draga.spaceTravels3.Constants;
import com.draga.spaceTravels3.SpaceTravels3;
import com.draga.spaceTravels3.manager.ScreenManager;
import com.draga.spaceTravels3.manager.SettingsManager;
import com.draga.spaceTravels3.manager.UIManager;
import com.draga.spaceTravels3.ui.BeepingImageTextButton;
import com.draga.spaceTravels3.ui.Screen;

public class ShareScreen extends Screen
{
    private Stage stage;

    public ShareScreen()
    {
        super(true, false);

        this.stage = new Stage(SpaceTravels3.menuViewport, SpaceTravels3.overlaySpriteBath);


        this.stage.addListener(new ClickListener()
        {
            @Override
            public void clicked(InputEvent event, float x, float y)
            {
                if (!event.isStopped())
                {
                    ScreenManager.removeScreen(ShareScreen.this);
                }
            }
        });


        Table table = UIManager.addDefaultTableToStage(this.stage);

        table.setBackground(UIManager.getTiledDrawable(Constants.Visual.SCREEN_FADE_COLOUR));
        table.addAction(Actions.sequence(
            Actions.fadeOut(0),
            Actions.fadeIn(Constants.Visual.SCREEN_FADE_DURATION, Interpolation.pow2In)));


        table
            .add("Share", "large", Color.WHITE)
            .row();
        table
            .add(getMessageButton())
            .row();
        table
            .add(getFacebookShareButton())
            .row();
        table
            .add(getBackTextButton())
            .bottom();

        this.stage.setDebugAll(SettingsManager.getDebugSettings().debugDraw);
    }

    private Actor getMessageButton()
    {
        BeepingImageTextButton button =
            new BeepingImageTextButton("email/SMS", UIManager.skin, "message");
        button.addListener(new ClickListener()
                           {
                               @Override
                               public void clicked(InputEvent event, float x, float y)
                               {
                                   SpaceTravels3.services.googleInvite();
                                   event.stop();
                               }
                           }
        );

        return button;
    }

    private Actor getFacebookShareButton()
    {
        BeepingImageTextButton button =
            new BeepingImageTextButton("Share on Facebook", UIManager.skin, "facebook");
        button.addListener(new ClickListener()
                           {
                               @Override
                               public void clicked(InputEvent event, float x, float y)
                               {
                                   SpaceTravels3.services.facebookShare();
                                   event.stop();
                               }
                           }
        );

        return button;
    }

    @Override
    public void show()
    {
        Gdx.input.setInputProcessor(this.stage);
    }

    @Override
    public void render(float delta)
    {
        if (Gdx.input.isKeyJustPressed(Input.Keys.ESCAPE)
            || Gdx.input.isKeyJustPressed(Input.Keys.BACK))
        {
            ScreenManager.removeScreen(ShareScreen.this);
        }

        this.stage.getViewport().apply();

        this.stage.act(delta);
        this.stage.draw();
    }

    @Override
    public void resize(int width, int height)
    {
        this.stage.getViewport().update(width, height);
    }

    @Override
    public void pause()
    {
    }

    @Override
    public void resume()
    {
    }

    @Override
    public void hide()
    {

    }

    @Override
    public void dispose()
    {
        this.stage.dispose();
    }
}

@@ -377,16 +377,65 @@
 ctions;%0A
+import com.badlogic.gdx.scenes.scene2d.ui.Label;%0A
 import c
@@ -475,16 +475,16 @@
 .Table;%0A
-
 import c
@@ -1102,25 +1102,113 @@
 riteBath);%0A%0A
+        // If the stage is clicked then close this screen, unless the event was stopped.
 %0A
-
         this
@@ -1538,16 +1538,22 @@
         
+final 
 Table ta
@@ -1601,24 +1601,317 @@
 is.stage);%0A%0A
+        // If the table is clicked stop the event propagating to the stage.%0A        table.addListener(new ClickListener()%0A        %7B%0A            @Override%0A            public void clicked(InputEvent event, float x, float y)%0A            %7B%0A                event.stop();%0A            %7D%0A        %7D);%0A%0A
         tabl
@@ -2395,32 +2395,109 @@
 ;%0A        table%0A
+            .add(getFacebookInviteActor())%0A            .row();%0A        table%0A
             .add
@@ -3105,57 +3105,8 @@
 ();%0A
-                                   event.stop();%0A
     
@@ -3699,57 +3699,8 @@
 ();%0A
-                                   event.stop();%0A
     
@@ -3761,32 +3761,32 @@
   %7D%0A        );%0A%0A
-
         return b
@@ -3791,32 +3791,959 @@
  button;%0A    %7D%0A%0A
+    private Actor getFacebookInviteActor()%0A    %7B%0A        if (SpaceTravels3.services.facebookCanInvite())%0A        %7B%0A            BeepingImageTextButton button =%0A                new BeepingImageTextButton(%22Invite Facebook friend%22, UIManager.skin, %22facebook%22);%0A            button.addListener(new ClickListener()%0A                               %7B%0A                                   @Override%0A                                   public void clicked(InputEvent event, float x, float y)%0A                                   %7B%0A                                       SpaceTravels3.services.facebookInvite();%0A                                   %7D%0A                               %7D%0A            );%0A%0A            return button;%0A        %7D%0A        else%0A        %7B%0A            Label label = new Label(%0A                %22Download the facebook app to invite friends directly!%22,%0A                UIManager.skin);%0A%0A            return label;%0A        %7D%0A    %7D%0A%0A
     @Override%0A  

add facebook invite if facebook app is installed, otherwise notify; improved clicking behaviour to dismiss share screen