package com.darkdimension.ritle_run;

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

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

    //UI
    private DiPoint title;
    private DiPoint buttonStart;
    private DiPoint buttonExtra;
    private String textStart, textExtra;

	//Mute Button
	private DiPoint buttonMute;

	//Hard Start
	private boolean hardStart;

	//Tutorial
	private DiPoint buttonTutorial;
	private DiAnimation aTutorial;
	private int tutorial;
	private String sTutorial;

	private String purpose0, purpose1, jump0, jump1;

	private DiPoint tutCrystal;
	private String tutSCrystal0, tutSCrystal1;

	private DiPoint tutMana;
	private String tutSMana0, tutSMana1, tutSMana2, tutSMana3;

	private DiPoint tutDoubleScore;
	private String tutSDoubleScore0, tutSDoubleScore1;

	private DiPoint tutShield;
	private String tutSShield;

	private DiPoint tutSpike;
	private String tutSSpike0, tutSSpike1;

	private String tutDiff0, tutDiff1, tutTouchAnywhere;

    public MenuMain(Resources resources, boolean gHardStart)
    {
        //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());

        //Title
        title = new DiPoint(7);
        title.setCenterX(GameSurface.width /2);
        title.setCenterY(GameSurface.height/4);

        //Buttons
		buttonStart = new DiPoint(8);
        buttonStart.setCenterX(GameSurface.width    /3);
        buttonStart.setCenterY(GameSurface.height *2/3);

		buttonExtra = new DiPoint(8);
        buttonExtra.setCenterX(GameSurface.width  *2/3);
        buttonExtra.setCenterY(GameSurface.height *2/3);

        //Button Strings
        textStart = resources.getString(R.string.button_start);
        textExtra = resources.getString(R.string.button_extra);

		//Mute
		buttonMute = new DiPoint(GameSurface.bSound ? 26 : 27);
		buttonMute.setRight (GameSurface.width  -buttonMute.getWidth ()/2);
		buttonMute.setBottom(GameSurface.height -buttonMute.getHeight()/2);

		//Hard Start
		hardStart = gHardStart;

		//Tutorial
		buttonTutorial = new DiPoint(8);
		buttonTutorial.setLeft(buttonTutorial.getWidth()/2);
		buttonTutorial.setBottom(GameSurface.height -buttonTutorial.getHeight()/2);

		aTutorial = new DiAnimation(10);

		tutorial = -1;
		sTutorial = resources.getString(R.string.tutorial);

		purpose0 = resources.getString(R.string.tutorial_purpose_0);
		purpose1 = resources.getString(R.string.tutorial_purpose_1);

		jump0 = resources.getString(R.string.tutorial_jump_0);
		jump1 = resources.getString(R.string.tutorial_jump_1);

		tutCrystal = new DiPoint(21);
		tutCrystal.setCenterX(GameSurface.width/2);
		tutCrystal.setCenterY(GameSurface.height/3);
		tutSCrystal0 = resources.getString(R.string.tutorial_crystal_0);
		tutSCrystal1 =resources.getString(R.string.tutorial_crystal_1);

		tutMana = new DiPoint(22);
		tutMana.setCenterX(GameSurface.width/2);
		tutMana.setCenterY(GameSurface.height/3);
		tutSMana0 = resources.getString(R.string.tutorial_mana_0);
		tutSMana1 = resources.getString(R.string.tutorial_mana_1);
		tutSMana2 = resources.getString(R.string.tutorial_mana_2);
		tutSMana3 = resources.getString(R.string.tutorial_mana_3);

		tutDoubleScore = new DiPoint(24);
		tutDoubleScore.setCenterX(GameSurface.width/2);
		tutDoubleScore.setCenterY(GameSurface.height/3);
		tutSDoubleScore0 = resources.getString(R.string.tutorial_double_score_0);
		tutSDoubleScore1 = resources.getString(R.string.tutorial_double_score_1);

		tutShield = new DiPoint(23);
		tutShield.setCenterX(GameSurface.width/2);
		tutShield.setCenterY(GameSurface.height/3);
		tutSShield = resources.getString(R.string.tutorial_shield);

		tutSpike = new DiPoint(25);
		tutSpike.setCenterX(GameSurface.width / 2);
		tutSpike.setCenterY(GameSurface.height/3);
		tutSSpike0 = resources.getString(R.string.tutorial_spikes_0);
		tutSSpike1 = resources.getString(R.string.tutorial_spikes_1);

		tutDiff0 = resources.getString(R.string.tutorial_end_0);
		tutDiff1 = resources.getString(R.string.tutorial_end_1);

		tutTouchAnywhere = resources.getString(R.string.touch_anywhere);
    }

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

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

            //Touch Button
			if (tutorial > -1)
			{
				tutorial--;

				if (tutorial == -1)
					aTutorial.startFromEnd();
			} else
            if (buttonStart.isTouched(touchX, touchY))
                destroy(Action.TO_GAME); else
            if (buttonExtra.isTouched(touchX, touchY))
                destroy(Action.TO_EXTRA); else
			if (buttonMute.isTouched(touchX, touchY))
			{
				if (GameSurface.bSound)
				{
					GameSurface.bSound = false;
					buttonMute.setFrame(27);
				}
				else
				{
					GameSurface.bSound = true;
					buttonMute.setFrame(26);
				}
			} else
			if (buttonTutorial.isTouched(touchX, touchY))
				aTutorial.startFromBeginning();

        }

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

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

		//Tutorial animation
		if (aTutorial.update())
		{
			invisible.setAlpha(255 -aTutorial.getDistance(255));

			if (aTutorial.isAtEnd())
				tutorial = 7;

			bDraw = true;
		}

        //Return Nothing
        return Action.NO_ACTION;
    }

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

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

        //Hard Start
		if (aBasic.isPositive() && hardStart)
		{
			//Ground
			ground.draw(canvas, 0, 0, invisible);

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

			if (aBasic.isAtEnd()) hardStart = false;
		}
		else
		{
			//Ground
			ground.draw(canvas, 0, 0, null);

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

		//UI
		if (tutorial == -1)
		{
			//Title
			title.draw(canvas, invisible);

			//Buttons
			buttonStart.draw(canvas, invisible);
			canvas.drawText(textStart, buttonStart.getCenterX(), buttonStart.getCenterY() + (10 * GameSurface.density), GameSurface.font);

			buttonExtra.draw(canvas, invisible);
			canvas.drawText(textExtra, buttonExtra.getCenterX(), buttonExtra.getCenterY() +(10 *GameSurface.density), GameSurface.font);

			buttonMute.draw(canvas, invisible);

			buttonTutorial.draw(canvas, invisible);
			canvas.drawText(sTutorial, buttonTutorial.getCenterX(), buttonTutorial.getCenterY() +(10 *GameSurface.density), GameSurface.font);
		}
		//Tutorial
		else
		{
			switch (tutorial)
			{
				case 7:
					canvas.drawText(purpose0, GameSurface.width/2, GameSurface.height/2, GameSurface.font);
					canvas.drawText(purpose1, GameSurface.width/2, GameSurface.height/2 +(20 *GameSurface.density), GameSurface.font);
					break;
				case 6:
					canvas.drawText(jump0, GameSurface.width/2, GameSurface.height/2, GameSurface.font);
					canvas.drawText(jump1, GameSurface.width/2, GameSurface.height/2 +(20 *GameSurface.density), GameSurface.font);
					break;
				case 5:
					tutCrystal.draw(canvas);
					canvas.drawText(tutSCrystal0, GameSurface.width/2, GameSurface.height/2, GameSurface.font);
					canvas.drawText(tutSCrystal1, GameSurface.width/2, GameSurface.height/2 +(20 *GameSurface.density), GameSurface.font);
					break;
				case 4:
					tutMana.draw(canvas);
					canvas.drawText(tutSMana0, GameSurface.width/2, GameSurface.height/2, GameSurface.font);
					canvas.drawText(tutSMana1, GameSurface.width/2, GameSurface.height/2 +(20 *GameSurface.density), GameSurface.font);
					canvas.drawText(tutSMana2, GameSurface.width/2, GameSurface.height/2 +(20 *GameSurface.density)*2, GameSurface.font);
					canvas.drawText(tutSMana3, GameSurface.width/2, GameSurface.height/2 +(20 *GameSurface.density)*3, GameSurface.font);
					break;
				case 3:
					tutDoubleScore.draw(canvas);
					canvas.drawText(tutSDoubleScore0, GameSurface.width/2, GameSurface.height/2, GameSurface.font);
					canvas.drawText(tutSDoubleScore1, GameSurface.width/2, GameSurface.height/2 +(20 *GameSurface.density), GameSurface.font);
					break;
				case 2:
					tutShield.draw(canvas);
					canvas.drawText(tutSShield, GameSurface.width/2, GameSurface.height/2, GameSurface.font);
					break;
				case 1:
					tutSpike.draw(canvas);
					canvas.drawText(tutSSpike0, GameSurface.width/2, GameSurface.height/2, GameSurface.font);
					canvas.drawText(tutSSpike1, GameSurface.width/2, GameSurface.height/2 +(20 *GameSurface.density), GameSurface.font);
					break;
				case 0:
					canvas.drawText(tutDiff0, GameSurface.width/2, GameSurface.height/2, GameSurface.font);
					canvas.drawText(tutDiff1, GameSurface.width/2, GameSurface.height/2 +(20 *GameSurface.density)*2, GameSurface.font);
					break;
			}

			canvas.drawText(tutTouchAnywhere, GameSurface.width / 2, GameSurface.height - (20 * GameSurface.density), GameSurface.font);
		}
    }
}