package de.frajul.endlessroll.main.screens;

import android.support.annotation.IdRes;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import de.frajul.endlessroll.R;
import de.frajul.endlessroll.main.GameActivity;
import de.frajul.endlessroll.views.ConfirmDialog;
import de.frajul.endlessroll.views.CreditsDialog;
import de.frajul.endlessroll.views.TopBar;

/**
 * Created by Julian on 10.09.2017.
 */

public class SettingsScreen extends Screen<LinearLayout> implements View.OnClickListener, ConfirmDialog.ConfirmDialogListener {

    private final int CALL_ID_CONFIRM_RESET = 1;

    private TopBar topBar;
    private ToggleButton musicToggle;
    private ToggleButton soundToggle;
    private CheckBox fpsCheckbox;
    private CheckBox performanceCheckbox;
    private Button creditsButton;
    private Button resetButton;

    private CreditsDialog creditsDialog;
    private ConfirmDialog resetConfirmDialog;

    public SettingsScreen(GameActivity gameActivity) {
        super(ScreenType.SETTINGS, gameActivity, R.layout.settings);
        topBar = super.createTopBar(R.id.settings_topbar);
        setTypefaceToTextView(R.id.settings_music_label);
        setTypefaceToTextView(R.id.settings_sounds_label);
        musicToggle = (ToggleButton) layout.findViewById(R.id.settings_musictoggle);
        musicToggle.setOnClickListener(this);
        soundToggle = (ToggleButton) layout.findViewById(R.id.settings_soundtoggle);
        soundToggle.setOnClickListener(this);
        fpsCheckbox = (CheckBox) layout.findViewById(R.id.settings_fps_checkbox);
        fpsCheckbox.setTypeface(gameActivity.getTypeface());
        performanceCheckbox = (CheckBox) layout.findViewById(R.id.settings_performance_checkbox);
        performanceCheckbox.setTypeface(gameActivity.getTypeface());
        creditsButton = (Button) layout.findViewById(R.id.settings_credits);
        creditsButton.setOnClickListener(this);
        resetButton = (Button) layout.findViewById(R.id.settings_reset);
        resetButton.setOnClickListener(this);

        creditsDialog = new CreditsDialog(gameActivity);
        resetConfirmDialog = new ConfirmDialog(gameActivity, this);
    }

    private void setTypefaceToTextView(@IdRes int id) {
        TextView textView = (TextView) layout.findViewById(id);
        textView.setTypeface(gameActivity.getTypeface());
    }

    @Override
    public void prepareToBeShown() {
        gameActivity.getSoundManager().menuMusic.start();

        topBar.update();
        topBar.setCustomCallerForScreenSwitch(super.caller);
        topBar.setShopsEnabled(!gameActivity.isShortMenuInGameActive());

        musicToggle.setChecked(!gameActivity.getSoundManager().isMusicMuted());
        soundToggle.setChecked(!gameActivity.getSoundManager().isSoundsMuted());
        //TODO: set fps/performance checkboxes selected
        resetButton.setEnabled(super.caller != ScreenType.GAME);
    }

    @Override
    public void onBackKeyDown() {
        gameActivity.getDataStorageHandler()
                .writeSoundsMuted(gameActivity.getSoundManager().isSoundsMuted());
        gameActivity.getDataStorageHandler()
                .writeMusicMuted(gameActivity.getSoundManager().isMusicMuted());

        //TODO: write fps/performance
        flipToCaller();
    }

    @Override
    public void onClick(View v) {
        if (v.equals(musicToggle)) {
            gameActivity.getSoundManager().setMusicMuted(!musicToggle.isChecked());
        } else if (v.equals(soundToggle)) {
            gameActivity.getSoundManager().setSoundsMuted(!soundToggle.isChecked());
        } else if (v.equals(creditsButton)) {
            creditsDialog.show();
        } else if (v.equals(resetButton)) {
            resetConfirmDialog.show(CALL_ID_CONFIRM_RESET, R.string.confirm_dialog_reset);
        }
    }

    @Override
    public void onConfirmed(int callId) {
        if (callId == CALL_ID_CONFIRM_RESET) {
            gameActivity.resetData();
            prepareToBeShown();
            //ResetButton is disabled if caller is GAME, because shortMenu or GoalMessage would still be visible on next level start
            if (super.caller == ScreenType.LEVELS || super.caller == ScreenType.GAME) {
                super.caller = ScreenType.WORLDS;
                topBar.setCustomCallerForScreenSwitch(super.caller);
            }
        }
    }
}
