package com.darkdimension.ritle_run;

import android.content.res.Resources;
import android.graphics.Canvas;

public class MenuExtra extends Menu
{
    //Environment
    private Ground ground;
    private DiPoint tree0, tree1;

    //Dark Dimension
    private DiPoint darkDimensionLogo;
	private String topText, bottomText;

    //Shares
    private DiPoint facebook, twitter, tumblr, googlePlayStore;

	//Change Character
	private int charState;
	private DiAnimation aChangeChar;
	private Character character;
	private DiPoint buttonChangeChar;
	private String stringChangeChar;

    //Back Button
    private DiPoint buttonBack;
	private String textBack;

    //Constructor
    public MenuExtra(Resources resources)
    {
        //Super Constructor
        super();

        //Current Ground
		ground = new Ground();
        ground.setWidth(GameSurface.width /ground.getWidth() +2);
        ground.setCenterX(GameSurface.width/2);
        ground.setBottom(GameSurface.height);

        //Trees
        tree0 = new DiPoint(5);
        tree0.setCenterX(GameSurface.width /4);
        tree0.setBottom (ground.getGroundLevel());

        tree1 = new DiPoint(6);
        tree1.setCenterX(GameSurface.width *5/6);
        tree1.setBottom (ground.getGroundLevel());

        //Dark Dimension
		darkDimensionLogo = new DiPoint(10);
        darkDimensionLogo.setCenterX(GameSurface.width/2);
        darkDimensionLogo.setCenterY(GameSurface.height/2);

		topText = resources.getString(R.string.made_by);
		bottomText = resources.getString(R.string.dark_dimension);

        //Shares
		facebook = new DiPoint(11);
        facebook.setLeft(facebook.getWidth()/2);
        facebook.setBottom(GameSurface.height -facebook.getHeight()/2);

		twitter = new DiPoint(12);
        twitter.setLeft(facebook.getRight() +twitter.getWidth()/2);
        twitter.setBottom(GameSurface.height -twitter.getHeight()/2);

		tumblr = new DiPoint(13);
        tumblr.setLeft(twitter.getRight() +tumblr.getWidth()/2);
        tumblr.setBottom(GameSurface.height -tumblr.getHeight()/2);

		googlePlayStore = new DiPoint(14);
        googlePlayStore.setLeft(tumblr.getRight() +googlePlayStore.getWidth()/2);
        googlePlayStore.setBottom(GameSurface.height -googlePlayStore.getHeight()/2);

		//Change Character
		charState = 0;
		aChangeChar = new DiAnimation(10);

		character = new Character();
		character.setLeft(GameSurface.width * 13/16);
		character.setBottom(ground.getGroundLevel());

		buttonChangeChar = new DiPoint(8);
		buttonChangeChar.setCenterX(character.getCenterX());
		buttonChangeChar.setTop(character.getBottom());

		stringChangeChar = resources.getString(R.string.change_character);

        //Back Button
		buttonBack = new DiPoint(8);
        buttonBack.setRight(GameSurface.width  - buttonBack.getWidth()/2);
        buttonBack.setTop(buttonBack.getHeight()/2);

		textBack = resources.getString(R.string.button_back);
    }

    public Action update()
    {
        //Super Update
        super.update();

        //Process input
        if (bTouched)
        {
            //Cancel Input
            bTouched = false;

            if (facebook		.isTouched(touchX, touchY)) return Action.TO_FACEBOOK	; else
            if (twitter 		.isTouched(touchX, touchY)) return Action.TO_TWITTER	; else
            if (tumblr  		.isTouched(touchX, touchY)) return Action.TO_TUMBLR		; else
            if (googlePlayStore .isTouched(touchX, touchY)) return Action.TO_PLAY_STORE ; else
            //Touch Back Button
            if (buttonBack.isTouched(touchX, touchY))
            {
            	destroy(Action.TO_MAIN_MENU);
               	return Action.NO_ACTION;
            } else
			if (buttonChangeChar.isTouched(touchX, touchY) && !aChangeChar.isRunning())
			{
				charState = 0;
				aChangeChar.startFromBeginning();
			}
        }

        //Background
        if (Background.animate(null, 0))
            bDraw = true;

		//Character
		if (character.animate())
			bDraw = true;

		if (aChangeChar.update())
		{
			if (charState == 0)
			{
				character.setLeft(GameSurface.width * 13/16 +(int)(GameSurface.width *3/16 *Math.cos(Math.toRadians(90 -aChangeChar.getDistance(180)))));
				character.setBottom(ground.getGroundLevel() +character.getHeight() +(int)(character.getHeight() *Math.sin(Math.toRadians(270 +aChangeChar.getDistance(180)))));
			} else
			if (charState == 1)
			{
				character.setLeft(GameSurface.width * 13/16 +(int)(GameSurface.width *3/16 *Math.cos(Math.toRadians(90 +aChangeChar.getDistance(180)))));
				character.setBottom(ground.getGroundLevel() +character.getHeight() +(int)(character.getHeight() *Math.sin(Math.toRadians(90 -aChangeChar.getDistance(180)))));
			}

			if (aChangeChar.isAtEnd() && charState == 0)
			{
				charState = 1;
				aChangeChar.startFromBeginning();
				return Action.CHANGE_CHAR;
			}

			bDraw = true;
		}

        //If it's destroyed, return next action
        if (aBasic.isAtStart() && !aBasic.isPositive())
                return nMenu;

        return Action.NO_ACTION;
    }

    public void draw(Canvas canvas)
    {
        //Super Draw
        super.draw(canvas);

        //Background
        Background.Draw(canvas, 0);

        //Ground
        ground.draw(canvas, 0, 0, null);

        //Trees
        tree0.draw(canvas);
        tree1.draw(canvas);

        //Dark Dimension
        darkDimensionLogo.draw(canvas, invisible);
		canvas.drawText(topText, GameSurface.width/2, darkDimensionLogo.getTop() -10 *GameSurface.density, GameSurface.font);
        canvas.drawText(bottomText, GameSurface.width/2, darkDimensionLogo.getBottom() +20 *GameSurface.density, GameSurface.font);

        //Shares
        facebook.draw(canvas, invisible);
        twitter.draw(canvas, invisible);
        tumblr.draw(canvas, invisible);
        googlePlayStore.draw(canvas, invisible);

		character.drawSimple(canvas, invisible);
		buttonChangeChar.draw(canvas, invisible);
		canvas.drawText(stringChangeChar, buttonChangeChar.getCenterX(), buttonChangeChar.getCenterY() +(10 *GameSurface.density), GameSurface.font);

        //Back Button
        buttonBack.draw(canvas, invisible);
        canvas.drawText(textBack, buttonBack.getCenterX(), buttonBack.getCenterY() + (10 * GameSurface.density), GameSurface.font);
    }
}