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;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;

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

public class OptionsActivity extends AppCompatActivity {

    private String sharedPreferencesKey, adsKey, higherQualityKey, fpsKey;

    private SharedPreferences sharedPreferences;

    private RelativeLayout layoutAds, layoutHigherQuality, layoutFPS;
    private CheckBox cbAds, cbHigherQuality, cbFPS;
    private boolean adsEnabled, 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);

        // Set up Ads
        adsKey = getString(R.string.adsKey);
        layoutAds = (RelativeLayout) findViewById(R.id.optionAdsLayout);
        cbAds = (CheckBox) findViewById(R.id.optionAdsCheckBox);
        layoutAds.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flipOption(adsKey);
            }
        });
        cbAds.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flipOption(adsKey);
            }
        });
        setAds();

        // 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(adsKey)){
            setAds();
        }
        else if (key.equals(higherQualityKey)){
            setHigherQuality();
        }
        else if (key.equals(fpsKey)){
            setFPS();
        }
    }

    private void setAds(){
        adsEnabled = sharedPreferences.getBoolean(adsKey, false);
        cbAds.setChecked(adsEnabled);
        toggleAds();
    }

    private void toggleAds(){
        // Enable ads?
        adsEnabled = sharedPreferences.getBoolean(adsKey, false);
        AdView mAdView = (AdView) findViewById(R.id.adViewOptions);
        if (adsEnabled) {
            mAdView.setVisibility(View.VISIBLE);
            MobileAds.initialize(getApplicationContext(), "ca-app-pub-3940256099942544~3347511713");
            AdRequest adRequest = new AdRequest.Builder()
                    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                    .addTestDevice("C6A494DC6E7C9AC29102694D48487084") // Nexus 7
                    .addTestDevice("6545BCC9B60A394005546783687339B7") // Moto G
                    .build();
            mAdView.loadAd(adRequest);
        }
        else{
            mAdView.setVisibility(View.GONE);
        }
    }

    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();
    }
}
