package com.hyperling.apps.wheretheriverfrowns;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.Map;
import java.util.TimerTask;

public class DebugActivity extends AppCompatActivity {

    private ActionBar actionBar;

    private SharedPreferences sharedPreferences;

    private String sharedPreferencesKey, debugKey,
                   TAG;

    private LinearLayout debugLayout;

    private Button btnDebug, btnResetPreferences;

    private boolean DEBUG;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_debug);

        // Load Strings
        sharedPreferencesKey = getString(R.string.sharedPreferencesKey);
        debugKey = getString(R.string.debugKey);

        TAG = getString(R.string.TAG);

        // Get Preferences
        sharedPreferences = getSharedPreferences(sharedPreferencesKey, MODE_PRIVATE);
        DEBUG = sharedPreferences.getBoolean(debugKey, false);

        Map<String, ?> sharedPreferencesAll = sharedPreferences.getAll();

        // Initialize UI
        actionBar = DebugActivity.this.getSupportActionBar();
        actionBar.setTitle(getString(R.string.debugActivityTitle));
        actionBar.setDisplayHomeAsUpEnabled(true);

        btnDebug = (Button) findViewById(R.id.btnDebugExit);
        btnResetPreferences = (Button) findViewById(R.id.btnResetPreferences);

        // Disable buttons so they aren't accidentally clicked
        setButtonsEnabled(false);

        if (DEBUG){
            btnDebug.setBackgroundColor(Color.GREEN);
        }
        else{
            btnDebug.setBackgroundColor(Color.RED);
        }
        btnDebug.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean(debugKey, !DEBUG);
                editor.apply();

                resetScreen();
            }
        });

        btnResetPreferences.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.clear();
                editor.apply();

                resetScreen();
            }
        });

        debugLayout = (LinearLayout) findViewById(R.id.debugLayout);

        for (String key : sharedPreferencesAll.keySet()){
            if (DEBUG){
                Log.d(TAG, "DebugActivity:  key=" + key + " value=" + sharedPreferencesAll.get(key));
            }
            TextView textView = new TextView(btnDebug.getContext());
            textView.setText(key + "=" + sharedPreferencesAll.get(key));

            debugLayout.addView(textView);
        }

        Runnable runnable = new TimerTask() {
            @Override
            public void run() {
                if (DEBUG) Log.d(TAG, "DebugActivity:  Waiting 2 seconds to enable buttons.");
                synchronized (this){
                    try{
                        wait(2000);
                        if (DEBUG) Log.d(TAG, "DebugActivity:  Done waiting.");
                    }
                    catch (Exception e){
                        e.printStackTrace();
                        if (DEBUG) Log.d(TAG, "DebugActivity:  Failed to wait.");
                    }
                }
                enableButtonsHandler.sendEmptyMessage(0);
            }
        };

        Thread thread = new Thread(runnable);
        thread.start();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        switch (id){
            case android.R.id.home:
                finish();
        }

        return super.onOptionsItemSelected(item);
    }

    private void resetScreen(){
        finish();
        Intent refreshDebugActivity = new Intent(DebugActivity.this, DebugActivity.class);
        startActivity(refreshDebugActivity);
    }

    private void setButtonsEnabled(boolean enabled){
        btnDebug.setEnabled(enabled);
        btnResetPreferences.setEnabled(enabled);
    }

    private Handler enableButtonsHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            setButtonsEnabled(true);
        }
    };
}
