package pl.edu.pwr.wiz.wzorlaboratorium4;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import static android.preference.PreferenceManager.getDefaultSharedPreferences;

public class MainActivity extends AppCompatActivity {
    private SharedPreferences pref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        /* Resetowanie ustawień */
        Button btn = (Button) findViewById(R.id.btn_reset);

        Button btnTw = (Button) findViewById(R.id.btn_twitter);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(view.getContext());

                // Tytul
                alertDialogBuilder.setTitle("Reset ustawień");

                // Ustawiamy dialog
                alertDialogBuilder
                        .setMessage("Kliknij tak, aby potwierdzić?")
                        .setCancelable(false)
                        .setPositiveButton("Tak", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // Kliknięto tak
                                // @TODO Reset ustawień
                                TextView textView = (TextView) findViewById(R.id.dane_fb);
                                TextView textView2 = (TextView) findViewById(R.id.dane_tw);
                                TextView textView3 = (TextView) findViewById(R.id.tw_link);
                                TextView textView4 = (TextView) findViewById(R.id.pass_invalid);
                                TextView fbLogin = (TextView) findViewById(R.id.login_fb);
                                TextView twLogin = (TextView) findViewById(R.id.login_tw);

                                Button twBtn = (Button) findViewById(R.id.btn_twitter);
                                SharedPreferences sharedPrefs = getDefaultSharedPreferences(MainActivity.this);
                                SharedPreferences.Editor editor = sharedPrefs.edit();
                                editor.clear();
                                editor.commit();
                                textView.setText("");
                                textView2.setText("");
                                textView3.setText("");
                                textView4.setText("");
                                fbLogin.setText("");
                                twLogin.setText("");
                                twBtn.setVisibility(View.GONE);


                                // @TODO Wyświetlenie Toast z informacją
                                Toast.makeText(MainActivity.this, "Ustawienia zresetowane", Toast.LENGTH_LONG).show();
                            }
                        })
                        .setNegativeButton("Nie", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // Kliknięto nie
                                // @TODO Wyświetlenie Toast z informacją
                                Toast.makeText(MainActivity.this, "Rezygnacja z resetu ustawień", Toast.LENGTH_LONG).show();
                                dialog.cancel();
                            }
                        });

                // Utworz okno dialogowe i wyswietl je
                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();
            }
        });

//        Twitter Button
        btnTw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(view.getContext());

                // Tytul
                alertDialogBuilder.setTitle("Przejdź do TWITTERA");

                // Ustawiamy dialog
                alertDialogBuilder
                        .setMessage("Kliknij tak, aby potwierdzić?")
                        .setCancelable(false)
                        .setPositiveButton("Tak", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // Kliknięto tak
                                // @TODO Reset ustawień
                                Uri uri = Uri.parse(pref.getString("twitter_link", "brak"));
                                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                startActivity(intent);


                                // @TODO Wyświetlenie Toast z informacją
                                Toast.makeText(MainActivity.this, "Twitter", Toast.LENGTH_LONG).show();
                            }
                        })
                        .setNegativeButton("Nie", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // Kliknięto nie
                                // @TODO Wyświetlenie Toast z informacją
                                Toast.makeText(MainActivity.this, "Rezygnacja z przejścia do Twittera", Toast.LENGTH_LONG).show();
                                dialog.cancel();
                            }
                        });

                // Utworz okno dialogowe i wyswietl je
                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        showPrefs();
    }

    private void showPrefs() {
        /* Pobieramy ustawienia i wyswietlamy login FB/TW/LINK TW, jesli jest ustawiony */
        pref = getDefaultSharedPreferences(this);
        Boolean fb_on = pref.getBoolean("facebook_on", false);
        Boolean tw_on = pref.getBoolean("twitter_on", false);
        Boolean isValid = pref.getBoolean("fbIsValid", false);
        Boolean passIsValid = pref.getBoolean("passIsValid", false);

        Object isFbFragment = SettingsActivity.getIsFacebookFragment();
        Object isTwFragment = SettingsActivity.getIsTwitterFragment();

//        Boolean isTwFragment = SettingsActivity.getIsTwitterFragment();

        //Boolean twSubmit = pref.getBoolean("twitter_submit", false);

        String tw_link = pref.getString("twitter_link", "brak");
        TextView textView = (TextView) findViewById(R.id.dane_fb);
        TextView textView2 = (TextView) findViewById(R.id.dane_tw);
        TextView textView3 = (TextView) findViewById(R.id.tw_link);
        TextView textView4 = (TextView) findViewById(R.id.pass_invalid);
        TextView fbLogin = (TextView) findViewById(R.id.login_fb);
        TextView twLogin = (TextView) findViewById(R.id.login_tw);

        Boolean fbFragmentIsSet = false;
        Boolean twFragmentIsSet = false;


        Button twBtn = (Button) findViewById(R.id.btn_twitter);
        //Boolean isValid = SettingsActivity.fbIsValid;
        //Boolean passIsValid = SettingsActivity.passIsValid;

        System.out.println("isFbFragment  :" + isFbFragment);

        //checkIfTwOrFbFragmentIsSet(isFbFragment, isTwFragment, fbFragmentIsSet, twFragmentIsSet);

        if (checkIfFbFragmentIsSet(isFbFragment, isTwFragment, fbFragmentIsSet, twFragmentIsSet)) {
            if (fb_on) {
                textView.setText("Twój login FB to: " + pref.getString("facebook_login", "brak"));
            } else if (!fb_on) {
                textView.setText("FB wyłączone");
            }
            validateFbLogin(isValid, fbLogin, "FACEBOOK", fb_on);
            showIfWrongFbPass(textView4, passIsValid, fb_on);
        }

        if (checkIfTwFragmentIsSet(isFbFragment, isTwFragment, fbFragmentIsSet, twFragmentIsSet)) {
            if (tw_on) {
                textView2.setText("Twój login TW to: " + pref.getString("twitter_login", "brak"));
                textView3.setText("Twój link do TW to: " + tw_link);
            } else {
                textView2.setText("TW wyłączone");
                textView3.setText("TW wyłączone, brak linka");
            }
            validateTwLogin(isValid, twLogin, "TWITTER", tw_on);
            showIfWrongTwPass(textView4, passIsValid, tw_on);
        }
        //System.out.println("twSubmit "+twSubmit);

//        validateFbLogin(isValid, fbLogin, "FACEBOOK", fb_on);
//        validateTwLogin(isValid, twLogin, "TWITTER", tw_on);
//
//        showIfWrongFbPass(textView4, passIsValid, fb_on);
//        showIfWrongTwPass(textView4, passIsValid, tw_on);

        showHideTwBtn(twBtn, tw_on, tw_link);
        // @TODO Wyświetlanie przycisku kierującego do profilu Twitter, o ile taki został ustawiony
    }

    private boolean checkIfFbFragmentIsSet(Object isFbFragment, Object isTwFragment, Boolean fbFragmentIsSet, Boolean twFragmentIsSet){
        if(isFbFragment == null) {
            fbFragmentIsSet = false;
        }
       else if (isFbFragment != null) {
            fbFragmentIsSet = true;
            twFragmentIsSet = false;
        }
        return fbFragmentIsSet;
    }

    private boolean checkIfTwFragmentIsSet(Object isFbFragment, Object isTwFragment, Boolean fbFragmentIsSet, Boolean twFragmentIsSet){
        if(isTwFragment == null) {
            twFragmentIsSet = false;
        }
        else if (isTwFragment != null) {
            fbFragmentIsSet = false;
            twFragmentIsSet = true;
        }
        return twFragmentIsSet;
    }

    //Validate FB/TW login- check if e-mail
    private void validateFbLogin(Boolean isValid, TextView tw, String twOrFB, Boolean fbOn){
        if(!isValid){
            tw.setText("Niepoprawny login "+ twOrFB +" ,podaj e-mail");
        } else if (!fbOn) {
            tw.setText("Brak loginu, "+ twOrFB +" ,wyłączony");
        }
        else if (fbOn && isValid){
            tw.setText("Poprawny login "+ twOrFB);
        }
    }

    private void validateTwLogin(Boolean isValid, TextView tw, String twOrFB, Boolean twOn){
        if(!isValid){
            tw.setText("Niepoprawny login " + twOrFB + " ,podaj e-mail");
            return;
        }
        else if (!twOn) {
            tw.setText("Brak loginu, "+ twOrFB +" ,wyłączony");
            return;
        }
        else if(twOn && isValid) {
            tw.setText("Poprawny login "+ twOrFB);
        }
    }

    //Show msg only if wrong FB pass.
    private void showIfWrongFbPass(TextView passMsg, Boolean passIsValid, Boolean twOn) {
        if (!passIsValid && twOn) {
            passMsg.setText("BLEDNE HASLO FB - zly format");
            passMsg.setVisibility(View.VISIBLE);
        } else {
            //passMsg.setText("Haslo OK");
            passMsg.setVisibility(View.GONE);
        }
    }

    //Show msg only if wrong TW pass.
    private void showIfWrongTwPass(TextView passMsg, Boolean passIsValid, Boolean fbOn) {
        if (!passIsValid && fbOn) {
            passMsg.setText("BLEDNE HASLO TW - zly format");
            passMsg.setVisibility(View.VISIBLE);
        } else {
            //passMsg.setText("Haslo OK");
            passMsg.setVisibility(View.GONE);
        }
    }

    //Show/hide twitter button
    private void showHideTwBtn(Button btn, Boolean twOn, String twLink) {
        if(!twOn || twLink.isEmpty()) {
            btn.setVisibility(View.GONE);
        } else {
            btn.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Intent settings = new Intent(this, SettingsActivity.class);
            startActivity(settings);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

