package com.hyperling.apps.games;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RelativeLayout;

/**
 * Created by ling on 12/22/16.
 */

public class OptionsActivity extends AppCompatActivity {

    private String sharedPreferencesKey, higherQualityKey, fpsKey;

    private SharedPreferences sharedPreferences;

    private RelativeLayout layoutHigherQuality, layoutFPS;
    private CheckBox cbHigherQuality, cbFPS;
    private boolean higherQualityEnabled, fpsEnabled;

    private Button btnDeletePreferences, btnConfirmDeletePreferences;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_options);

        // Prevent the keyboard from popping up automatically
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        // Get SharedPreferences
        sharedPreferencesKey = getString(R.string.sharedPreferencesKey);
        sharedPreferences = getSharedPreferences(sharedPreferencesKey, MODE_PRIVATE);

        // Higher Quality Content
        higherQualityKey = getString(R.string.higherQualityKey);
        layoutHigherQuality = (RelativeLayout) findViewById(R.id.optionHigherQualityLayout);
        cbHigherQuality = (CheckBox) findViewById(R.id.optionHigherQualityCheckBox);
        layoutHigherQuality.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flipOption(higherQualityKey);
            }
        });
        cbHigherQuality.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flipOption(higherQualityKey);
            }
        });
        setHigherQuality();

        // In-Game FPS
        fpsKey = getString(R.string.fpsKey);
        layoutFPS = (RelativeLayout) findViewById(R.id.optionFPSLayout);
        cbFPS = (CheckBox) findViewById(R.id.fpsCheckBox);
        layoutFPS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flipOption(fpsKey);
            }
        });
        cbFPS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flipOption(fpsKey);
            }
        });
        setFPS();

        // Reset buttons
        btnDeletePreferences = (Button) findViewById(R.id.btnDeletePreferences);
        btnConfirmDeletePreferences = (Button) findViewById(R.id.btnConfirmDeletePreferences);

        btnDeletePreferences.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showConfirmDeletePreferencesButton((Button) v);
            }
        });
        btnConfirmDeletePreferences.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deletePreferences();
            }
        });
    }

    private void flipOption(String key){
        boolean option = sharedPreferences.getBoolean(key, false);

        // Save the setting
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, !option);
        editor.apply();

        // Change the box
        if (key.equals(higherQualityKey)){
            setHigherQuality();
        }
        else if (key.equals(fpsKey)){
            setFPS();
        }
    }

    public void setHigherQuality() {
        higherQualityEnabled = sharedPreferences.getBoolean(higherQualityKey, false);
        cbHigherQuality.setChecked(higherQualityEnabled);
    }

    public void setFPS() {
        fpsEnabled = sharedPreferences.getBoolean(fpsKey, false);
        cbFPS.setChecked(fpsEnabled);
    }

    private void showConfirmDeletePreferencesButton(Button callButton){
        callButton.setEnabled(false);
        btnConfirmDeletePreferences.setVisibility(View.VISIBLE);
        btnConfirmDeletePreferences.setEnabled(true);
    }

    private void deletePreferences(){
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.apply();

        Intent resetScreen = new Intent(OptionsActivity.this, OptionsActivity.class);
        startActivity(resetScreen);
        finish();
    }
}
