package de.frajul.endlessroll.views;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import de.frajul.endlessroll.R;
import de.frajul.endlessroll.levels.Level;
import de.frajul.endlessroll.levels.LevelPack;
import de.frajul.endlessroll.main.GameActivity;
import de.frajul.endlessroll.main.GameHandler;
import de.frajul.endlessroll.main.game.Game;
import de.frajul.endlessroll.main.game.Timer;
import de.frajul.endlessroll.user.User;

/**
 * Created by Julian on 11.12.2015.
 */
public class ViewManager implements View.OnClickListener {

    private GameHandler gameViewHandler;
    private Game game;

    private TextView fpsView;
    private TextView playerProgress;
    private TextView playerSpeed;
    private ImageView pauseButton;
    private GameOverMessage gameOverMessage;
    private GoalMessage goalMessage;
    public ToolButtonBar toolButtonBar;
    public ShortMenu shortMenu;
    private Countdown countdown;

    private String fpsFormat, playerProgressFormat, playerSpeedFormat;

    public ViewManager(final Game game, final GameHandler gameViewHandler, final GameActivity gameActivity) {
        this.game = game;
        this.gameViewHandler = gameViewHandler;

        final RelativeLayout layout = gameViewHandler.getRootLayout();
        gameViewHandler.startInUiThread(new Runnable() {
            @Override
            public void run() {
                toolButtonBar = new ToolButtonBar(game,
                        gameActivity.getUser().getToolSlotSettings(),
                        (LinearLayout) layout.findViewById(R.id.game_toolbuttonbar));
                shortMenu = new ShortMenu(game, gameActivity,
                        layout.findViewById(R.id.game_shortmenu));
                gameOverMessage = new GameOverMessage(game, gameActivity,
                        layout.findViewById(R.id.game_game_over_message));
                goalMessage = new GoalMessage(game, gameActivity, layout.findViewById(R.id.game_goal_message));
                countdown = new Countdown(game, gameActivity.getTypeface(),
                        (TextView) layout.findViewById(R.id.game_countdown));
            }
        });
        pauseButton = (ImageView) layout.findViewById(R.id.game_pausebutton);
        pauseButton.setOnClickListener(this);
        playerProgress = (TextView) layout.findViewById(R.id.game_playerprogress);
        playerSpeed = (TextView) layout.findViewById(R.id.game_playerspeed);
        fpsView = (TextView) layout.findViewById(R.id.game_fps);

        fpsFormat = game.getContext().getString(R.string.game_fps_format_d);
        playerProgressFormat = game.getContext().getString(R.string.game_playerprogress_format_f);
        playerSpeedFormat = game.getContext().getString(R.string.game_playerspeed_format_f);
    }

    public void prepareToBeShown(){
        if(shortMenu.isVisible())
            shortMenu.prepareToBeShown();
        if(gameOverMessage.isVisible())
            gameOverMessage.prepareToBeShown();
        if(goalMessage.isVisible())
            goalMessage.prepareToBeShown();
    }

    @Override
    public void onClick(View v) {
        game.tryToPause();
    }

    public void resetViews(final User user) {
        gameViewHandler.startInUiThread(new Runnable() {
            @Override
            public void run() {
                toolButtonBar.reset(user.getToolSlotSettings());
                toolButtonBar.update(0);
                playerProgress.setText(R.string.game_playerprogress_placeholder);
                playerSpeed.setText(R.string.game_playerspeed_placeholder);
            }
        });
    }

    public void onGoalMessageKeyBack(){
        goalMessage.onKeyBack();
    }

    public void showGameOverMessage() {
        gameViewHandler.startInUiThread(new Runnable() {
            @Override
            public void run() {
                gameOverMessage.fadeIn();
            }
        });
    }

    public void showGoalMessage(final LevelPack levelPack, final Level level){
        gameViewHandler.startInUiThread(new Runnable() {
            @Override
            public void run() {
                goalMessage.fadeInWithDelay(levelPack, level);
            }
        });
    }

    public void showShortMenu() {
        shortMenu.startAnims();
        shortMenu.setVisible(true);
    }

    public void hideShortMenu() {
        shortMenu.setVisible(false);
    }

    public void startCountdown() {
        countdown.start();
    }

    public void stopCountdown() {
        countdown.stop();
    }

    public void update(final boolean gameRunning, final Timer timer, final float playerX, final float playerXMov) {
        gameViewHandler.startInUiThread(new Runnable() {
            @Override
            public void run() {
                fpsView.setText(String.format(fpsFormat, timer.getFps()));
                if (gameRunning) {
                    playerProgress.setText(String.format(playerProgressFormat, toMeters(playerX)));
                    playerSpeed
                            .setText(String.format(playerSpeedFormat, toMeters(playerXMov * 1000)));
                    toolButtonBar.update(timer.getFrameTimeSeconds());
                }
            }
        });
    }

    private float toMeters(float value) {
        return ((int) (value * 20)) / 10f;
    }

}
