package org.deke.risk.riskahead;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.util.Patterns;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.deke.risk.riskahead.fragment.FacebookButtonFragment;
import org.deke.risk.riskahead.fragment.GooglePlusButtonFragment;
import org.deke.risk.riskahead.fragment.TwitterButtonFragment;
import org.deke.risk.riskahead.helper.AppConfig;
import org.deke.risk.riskahead.helper.AppController;
import org.deke.risk.riskahead.helper.SessionManager;
import org.deke.risk.riskahead.helper.TextValidator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * (C) Copyright 2016 RiskAhead (http://www.riskahead.net)
 * Developer & Contributor: Dennis Thießen (dennis.thiessen@riskahead.net)
 * Date of Release: 10.02.2016
 *
 */
public class LoginActivity extends AppCompatActivity implements FacebookButtonFragment.OnFragmentInteractionListener, GooglePlusButtonFragment.OnFragmentInteractionListener, TwitterButtonFragment.OnFragmentInteractionListener{

    private final static String TAG = LoginActivity.class.getSimpleName();

    private TextView input_email;
    private TextView input_username;
    private TextView input_password;

    private ProgressDialog pDialog;
    private SessionManager session;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        session = new SessionManager(getApplicationContext());

        initProgressDialog();
        handleIntentAndInitView();

        initSocialLoginButtons();
        checkIfVersionSupported();
    }

    @Override
    protected void onResume(){
        super.onResume();
        isNetworkAvailable();
        checkForMaintenance();

    }

    public void isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if(activeNetworkInfo == null){
            new AlertDialog.Builder(this)
                    .setTitle(getString(R.string.alert_internetconnection_title))
                    .setMessage(getString(R.string.alert_internetconnection_text))
                    .setCancelable(false)
                    .setNeutralButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            gotoStartActivity();
                        }
                    })
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .show();
        }
    }

    private void handleIntentAndInitView() {
        Intent intent = getIntent();
        String msg_intent = intent.getStringExtra(AppConfig.EXTRA_MESSAGE);

        if(msg_intent.equals(AppConfig.INTENT_LOGIN)){
            initLoginView();
        }else if(msg_intent.equals(AppConfig.INTENT_REGISTER)){
            initRegisterView();
        }
    }

    private void initProgressDialog() {
        pDialog = new ProgressDialog(this);
        pDialog.setMessage(getString(R.string.progress_getdata_text));
        pDialog.setCancelable(false);
    }

    private void initLoginView() {
        setContentView(R.layout.activity_login);

        input_email = (TextView) findViewById(R.id.input_register_email);
        input_password = (TextView) findViewById(R.id.input_register_password);
        TextView btn_requestPW = (TextView) findViewById(R.id.lbl_login_resendPW);

        Button btn_login = (Button) findViewById(R.id.btn_login);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String email = input_email.getText().toString();
                String password = input_password.getText().toString();

                if (input_password.getError() != null || input_email.getError() != null) {
                    showMessage(getString(R.string.error_validation));
                } else {
                    showDialog();
                    performLogin(email, password);
                }
            }
        });

        input_email.addTextChangedListener(new TextValidator(input_email, getApplicationContext()) {
            @Override
            public void validate(TextView textView, String text) {
                if (!Patterns.EMAIL_ADDRESS.matcher(text).matches()) {
                    textView.setError(getString(R.string.error_email));
                }
            }
        });

        input_password.addTextChangedListener(new TextValidator(input_password, getApplicationContext()) {
            @Override
            public void validate(TextView textView, String text) {
                if (text.length() < 5) {
                    textView.setError(getString(R.string.error_password));
                }
            }
        });

        btn_requestPW.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRequestNewPasswordClick(v);
            }
        });
    }

    private void initRegisterView() {
        setContentView(R.layout.activity_register);

        input_username = (TextView) findViewById(R.id.input_register_name);
        input_email = (TextView) findViewById(R.id.input_register_email);
        input_password = (TextView) findViewById(R.id.input_register_password);
        Button btn_login = (Button) findViewById(R.id.btn_register);

        input_username.addTextChangedListener(new TextValidator(input_username, getApplicationContext()) {
            @Override
            public void validate(TextView textView, String text) {
                if (text.trim().length() < 5) {
                    textView.setError(getString(R.string.error_anytext));
                } else {
                    textView.setError(null);
                }
            }
        });

        input_email.addTextChangedListener(new TextValidator(input_email, getApplicationContext()) {
            @Override
            public void validate(TextView textView, String text) {
                if (!Patterns.EMAIL_ADDRESS.matcher(text).matches()) {
                    textView.setError(getString(R.string.error_email));
                }else{
                    textView.setError(null);
                }
            }
        });

        input_password.addTextChangedListener(new TextValidator(input_password, getApplicationContext()) {
            @Override
            public void validate(TextView textView, String text) {
                if (text.length() < 5) {
                    textView.setError(getString(R.string.error_password));
                } else {
                    textView.setError(null);
                }
            }
        });

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = input_email.getText().toString();
                String password = input_password.getText().toString();

                String name = input_username.getText().toString();
                if (input_password.getError() != null || input_email.getError() != null || input_username.getError() != null || input_email.getText().toString().trim().length() == 0 || input_username.getText().toString().trim().length() == 0 || input_password.getText().toString().trim().length() == 0) {
                    showMessage(getString(R.string.error_validation));
                } else {
                    showDialog();
                    checkForUserAndPerformRegistration(name, email, password);
                }
            }
        });
    }

    private void initSocialLoginButtons() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        //FacebookButtonFragment fragmentFB = new FacebookButtonFragment();
        new FacebookButtonFragment();
        GooglePlusButtonFragment fragmentGP = new GooglePlusButtonFragment();

        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.frag_login_btnGP, fragmentGP);
        transaction.commit();
    }

    private void checkForMaintenance() {
        String tag_string_req = "req_checkformaintenance";
        StringRequest strReq = getStringRequestCheckForMaintenance();
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }

    private StringRequest getStringRequestCheckForMaintenance() {
        return new StringRequest(Method.POST, AppConfig.URL_ENCRYPTED, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Check User Exists Response: " + response);
                hideDialog();

                try {
                    JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");

                    if (!error) {
                        if(jObj.getBoolean("maintenancemode")){
                            showMessage(jObj.getString("maintenancemsg"));
                            gotoStartActivity();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                params.put("tag", AppConfig.REST_CHECK_MAINTENANCE);

                return params;
            }
        };
    }

    private void gotoStartActivity() {
        Intent intent = new Intent(
                LoginActivity.this,
                StartActivity.class);
        startActivity(intent);
    }

    private void checkIfVersionSupported() {
        String tag_string_req = "req_checkformaintenance";
        StringRequest strReq = getStringRequestCheckIfVersionSupported();
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }

    private StringRequest getStringRequestCheckIfVersionSupported() {
        return new StringRequest(Method.POST, AppConfig.URL_ENCRYPTED, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Check User Exists Response: " + response);
                hideDialog();

                try {
                    JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");

                    if (!error) {
                        if(!jObj.getBoolean("supported")){
                            showNotSupportedDialog();
                        }else if(!jObj.getBoolean("recommended")){
                            showNotRecommendDialog();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                int versionCode = 99;

                try {
                    versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }

                params.put("tag", AppConfig.REST_CHECK_VERSION);
                params.put("version", Integer.toString(versionCode));
                return params;
            }
        };
    }

    private void showNotSupportedDialog() {
        new AlertDialog.Builder(LoginActivity.this)
                .setTitle(getString(R.string.alert_notsupported_title))
                .setMessage(getString(R.string.alert_notsupported_text))
                .setCancelable(false)
                .setPositiveButton(R.string.update, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + getPackageName())));
                    }
                })
                .setNegativeButton(R.string.exit, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Intent.ACTION_MAIN);
                        intent.addCategory(Intent.CATEGORY_HOME);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }
                })
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();
    }

    private void showNotRecommendDialog() {
        new AlertDialog.Builder(LoginActivity.this)
                .setTitle(getString(R.string.alert_notrecommended_title))
                .setMessage(getString(R.string.alert_notrecommended_text))
                .setCancelable(false)
                .setPositiveButton(R.string.update, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + getPackageName())));
                    }
                })
                .setNegativeButton(R.string.later, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();
    }

    private void performLogin(final String email, final String password) {
        String tag_string_req = "req_login";
        StringRequest strReq = getStringRequestLogin(email, password);
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }

    private void checkForUserAndPerformRegistration(final String username, final String email, final String password) {
        String tag_string_req = "req_checkregister";
        StringRequest strReq = getStringRequestCheckUserExists(username, email, password);
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }

    private void performRegistration(final String username, final String email, final String password) {
        String tag_string_req = "req_register";
        StringRequest strReq = getStringRequestRegisterUser(username, email, password);
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }

    public void handleSocialMediaLogin(final String key, final String providerType, final String username, final String email){
        if(session.isLoggedIn()){
            Intent intent = new Intent(
                    LoginActivity.this,
                    MainActivity.class);
            intent.putExtra(AppConfig.EXTRA_MESSAGE, AppConfig.INTENT_SOCIALMEDIALOGIN);
            startActivity(intent);
            finish();
        }

        session.removeUser();
        showDialog();

        String tag_string_req = "socialloginrequest";
        StringRequest strReq = getStringRequestCheckSocialUserExists(key, providerType, username, email);
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }

    public void onRequestNewPasswordClick(View view){
        if((input_email.getText() != null && input_email.getText().toString().isEmpty()) || (input_email.getError() != null)){
            showMessage(getString(R.string.message_enteremail));
            return;
        }

        new AlertDialog.Builder(LoginActivity.this)
                .setTitle(getString(R.string.alert_passwordreset_title))
                .setMessage(getString(R.string.alert_passwordreset_text))
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        performRequestNewPassword();
                    }
                })
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // do nothing
                    }
                })
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();
    }

    private void performRequestNewPassword() {
        String tag_string_req = "requestpwreset";
        StringRequest strReq = getStringRequestResetPW(input_email.getText().toString());
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }

    private void performSocialMediaLogin(final String key, final String providerType, final String username, final String email){
        String tag_string_req = "sociallogin";
        StringRequest strReq = getStringRequestSocialMediaLogin( key,  providerType,  username,  email);
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }

    private StringRequest getStringRequestSocialMediaLogin(final String key, final String providerType, final String username, final String email) {
        return new StringRequest(Method.POST,AppConfig.URL_ENCRYPTED, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Social Media Login Response: " + response);
                hideDialog();

                try {
                    JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");
                    if (!error) {
                        session.setLogin(true);

                        JSONObject user = jObj.getJSONObject("user");
                        String id = user.getString("id");
                        String username = user.getString("username");
                        String name = user.getString("name");
                        String surname = user.getString("surname");
                        String email = user.getString("email");
                        String status = user.getString("status");
                        String created_at = user.getString("created_at");
                        String updated_at = user.getString("updated_at");
                        String lastlogin_at = user.getString("lastlogin_at");
                        String providerType = user.getString("providerType");
                        String token = user.getString("accesskey");

                        JSONArray jSubs = user.getJSONArray("subs");
                        boolean isPremium = false;
                        boolean isAdFree = false;

                        if(jSubs.length() > 0){
                            for(int i = 0; i < jSubs.length();i++) {
                                if(jSubs.getJSONObject(i).getString("productId").contains("extra_functions")){
                                    isPremium = true;
                                }

                                if(jSubs.getJSONObject(i).getString("productId").contains("no_ads")){
                                    isAdFree = true;
                                }
                            }
                        }

                        session.addUser(id, username, name, surname, email, status, providerType, created_at, updated_at, lastlogin_at, token,isPremium,isAdFree);

                        Intent intent = new Intent(
                                LoginActivity.this,
                                MainActivity.class);
                        intent.putExtra(AppConfig.EXTRA_MESSAGE, AppConfig.INTENT_SOCIALMEDIALOGIN);
                        startActivity(intent);
                        finish();
                    } else {
                        String errorMsg = jObj.getString("error_msg");
                        Log.e(TAG, "Error at social media login (Server returned error): " + errorMsg);
                        Toast.makeText(getApplicationContext(), getResources().getString(R.string.txt_errormsg_login)+getResources().getString(R.string.txt_errormsg_suffix), Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Social Media Login Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(), getResources().getString(R.string.txt_errormsg_login)+getResources().getString(R.string.txt_errormsg_suffix), Toast.LENGTH_LONG).show();
                hideDialog();
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                params.put("tag", AppConfig.REST_LOGIN_SOCIAL);
                params.put("key", key);
                params.put("providerType", providerType);
                params.put("username", username);
                params.put("email",email);

                return params;
            }

        };
    }

    private StringRequest getStringRequestLogin(final String email, final String password) {
        return new StringRequest(Method.POST, AppConfig.URL_ENCRYPTED, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Login Response: " + response);
                hideDialog();

                try {
                    JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");

                    if (!error) {
                        JSONObject user = jObj.getJSONObject("user");
                        String id = user.getString("id");
                        String username = user.getString("username");
                        String name = user.getString("name");
                        String surname = user.getString("surname");
                        String email = user.getString("email");
                        String status = user.getString("status");
                        String created_at = user.getString("created_at");
                        String updated_at = user.getString("updated_at");
                        String lastlogin_at = user.getString("lastlogin_at");
                        String token = user.getString("accesskey");
                        String providerType = "local";

                        JSONArray jSubs = user.getJSONArray("subs");
                        boolean isPremium = false;
                        boolean isAdFree = false;

                        if(jSubs.length() > 0){
                            for(int i = 0; i < jSubs.length();i++) {
                                if(jSubs.getJSONObject(i).getString("productId").contains("extra_functions")){
                                    isPremium = true;
                                }

                                if(jSubs.getJSONObject(i).getString("productId").contains("no_ads")){
                                    isAdFree = true;
                                }
                            }
                        }

                        if(status.equals("0")){
                            new AlertDialog.Builder(LoginActivity.this)
                                    .setTitle(getString(R.string.alert_accactivation_title))
                                    .setMessage(getString(R.string.alert_accactivation_text))
                                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            String tag_string_req = "resendactivationusermail";

                                            StringRequest strReq = getStringRequestActivationLinkUser(input_email.getText().toString());

                                            AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
                                            showMessage(getString(R.string.alert_accactivation_confirmation));
                                        }
                                    })
                                    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                        }
                                    })
                                    .setIcon(android.R.drawable.ic_dialog_alert)
                                    .show();
                        }else {
                            session.addUser(id, username, name, surname, email, status, providerType, created_at, updated_at, lastlogin_at, token, isPremium, isAdFree);
                            session.setLogin(true);

                            hideDialog();

                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    } else {
                        String errorMsg = jObj.getString("error_msg");
                        Log.e(TAG, "Error at login (Server returned error): " + errorMsg);
                        Toast.makeText(getApplicationContext(), getResources().getString(R.string.txt_errormsg_login), Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Login Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(), getResources().getString(R.string.txt_errormsg_login), Toast.LENGTH_LONG).show();
                hideDialog();
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                params.put("tag", AppConfig.REST_LOGIN);
                params.put("email", email);
                params.put("password", password);

                return params;
            }
        };
    }

    private StringRequest getStringRequestCheckUserExists(final String username, final String email, final String password) {
        return new StringRequest(Method.POST, AppConfig.URL_ENCRYPTED, new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    Log.d(TAG, "Check User Exists Response: " + response);
                    hideDialog();

                    try {
                        JSONObject jObj = new JSONObject(response);
                        boolean error = jObj.getBoolean("error");
                        if (!error) {

                            if(jObj.getInt("exists") == 1){
                               showMessage("Registration failed. Username and/or E-Mail already taken");
                            }else{
                                createAndShowPrivacyDialog(username, email, password);
                            }



                        } else {
                            String errorMsg = jObj.getString("error_msg");
                            Log.e(TAG, "Error occured: "+errorMsg);
                            Toast.makeText(getApplicationContext(), getResources().getString(R.string.txt_errormsg_register), Toast.LENGTH_LONG).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e(TAG, "Registration Error: " + error.getMessage());
                    Toast.makeText(getApplicationContext(),
                            getResources().getString(R.string.txt_errormsg_register), Toast.LENGTH_LONG).show();
                    hideDialog();
                }
            }) {

                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<>();
                    params.put("tag", AppConfig.REST_LOGIN_EXISTS);
                    params.put("email", email);

                    return params;
                }
            };
    }

    private StringRequest getStringRequestCheckSocialUserExists(final String key, final String providerType, final String username, final String email) {
        return new StringRequest(Method.POST, AppConfig.URL_ENCRYPTED, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Check User Exists Response: " + response);
                hideDialog();

                try {
                    JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");
                    if (!error) {

                        if(jObj.getInt("exists") == 1){
                            performSocialMediaLogin( key,providerType, username, email);
                        }else{
                            createAndShowPrivacyDialogForSocialUser( key,providerType, username, email);
                        }



                    } else {
                        String errorMsg = jObj.getString("error_msg");
                        Log.e(TAG, "Error occured: "+errorMsg);
                        Toast.makeText(getApplicationContext(), getResources().getString(R.string.txt_errormsg_register), Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Registration Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        getResources().getString(R.string.txt_errormsg_register), Toast.LENGTH_LONG).show();
                hideDialog();
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                params.put("tag", AppConfig.REST_LOGIN_EXISTS);
                params.put("key", key);

                return params;
            }
        };
    }

    private void createAndShowPrivacyDialogForSocialUser(final String key, final String providerType, final String username, final String email) {
        final TextView privacyText = new TextView(LoginActivity.this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);


        privacyText.setLayoutParams(lp);
        privacyText.setText(R.string.txt_register_privacy);
        privacyText.setMovementMethod(LinkMovementMethod.getInstance());
        privacyText.setPadding(5, 10, 5, 10);

        CheckBox privacyCheckbox = new CheckBox(getApplicationContext());
        privacyCheckbox.setText(R.string.txt_register_privacy_text);
        privacyCheckbox.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.md_black_1000));
        final float scale = this.getResources().getDisplayMetrics().density;
        privacyCheckbox.setPadding(privacyCheckbox.getPaddingLeft() + (int) (10.0f * scale + 0.5f),
                privacyCheckbox.getPaddingTop() + 5,
                privacyCheckbox.getPaddingRight(),
                privacyCheckbox.getPaddingBottom());
        privacyCheckbox.setButtonDrawable(Resources.getSystem().getIdentifier("btn_check_holo_light", "drawable", "android"));


        LinearLayout layout = new LinearLayout(LoginActivity.this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(privacyText);
        layout.addView(privacyCheckbox);

        AlertDialog.Builder builder =  new AlertDialog.Builder(LoginActivity.this)
                .setTitle(R.string.txt_register_privacy_title)
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        performSocialMediaLogin(key, providerType, username, email);
                    }
                })
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .setIcon(R.drawable.logo_380);

        final AlertDialog dialog = builder.create();

        privacyCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
                } else {
                    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
                }
            }
        });

        dialog.setView(layout, 30, 30, 30, 30);
        dialog.show();


        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
    }

    private void createAndShowPrivacyDialog(final String username, final String email, final String password) {
        final TextView privacyText = new TextView(LoginActivity.this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);


        privacyText.setLayoutParams(lp);
        privacyText.setText(R.string.txt_register_privacy);
        privacyText.setMovementMethod(LinkMovementMethod.getInstance());
        privacyText.setPadding(5, 10, 5, 10);

        CheckBox privacyCheckbox = new CheckBox(getApplicationContext());
        privacyCheckbox.setText(R.string.txt_register_privacy_text);
        privacyCheckbox.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.md_black_1000));
        final float scale = this.getResources().getDisplayMetrics().density;
        privacyCheckbox.setPadding(privacyCheckbox.getPaddingLeft() + (int) (10.0f * scale + 0.5f),
                privacyCheckbox.getPaddingTop() + 5,
                privacyCheckbox.getPaddingRight(),
                privacyCheckbox.getPaddingBottom());
        privacyCheckbox.setButtonDrawable(Resources.getSystem().getIdentifier("btn_check_holo_light", "drawable", "android"));


        LinearLayout layout = new LinearLayout(LoginActivity.this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(privacyText);
        layout.addView(privacyCheckbox);

        AlertDialog.Builder builder =  new AlertDialog.Builder(LoginActivity.this)
                .setTitle(R.string.txt_register_privacy_title)
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        performRegistration(username, email, password);
                    }
                })
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .setIcon(R.drawable.logo_380);

        final AlertDialog dialog = builder.create();

        privacyCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
                } else {
                    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
                }
            }
        });

        dialog.setView(layout, 30, 30, 30, 30);
        dialog.show();

        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
    }

    private StringRequest getStringRequestResetPW(final String email) {
        return new StringRequest(Method.POST, AppConfig.URL_ENCRYPTED, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Resend E-Mail Response: " + response);
                hideDialog();

                try {
                    JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");
                    if (!error) {
                        showMessage(getString(R.string.alert_passwordreset_confirmation));
                    } else {
                        String errorMsg = jObj.getString("error_msg");
                        Log.e(TAG, "Reset PW returned error value: "+errorMsg);
                        Toast.makeText(getApplicationContext(),
                                getResources().getString(R.string.txt_errormsg_getpwreset), Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "E-Mail pw resend Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        getResources().getString(R.string.txt_errormsg_getpwreset), Toast.LENGTH_LONG).show();
                hideDialog();
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                params.put("tag", AppConfig.REST_REQUESTPWRESET);
                params.put("email", email);

                return params;
            }
        };
    }

    private StringRequest getStringRequestActivationLinkUser(final String email) {
        return new StringRequest(Method.POST, AppConfig.URL_ENCRYPTED, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Resend E-Mail Response: " + response);
                hideDialog();

                try {
                    JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");
                    if (error) {
                        String errorMsg = jObj.getString("error_msg");
                        Log.e(TAG,"Resend E-Mail returned error value: "+errorMsg);
                        Toast.makeText(getApplicationContext(),
                                getResources().getString(R.string.txt_errormsg_getactivationlink), Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "E-Mail pw resend Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        getResources().getString(R.string.txt_errormsg_getactivationlink), Toast.LENGTH_LONG).show();
                hideDialog();
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                params.put("tag", AppConfig.REST_RESENDACTIVATIONMAIL);
                params.put("email", email);

                return params;
            }
        };
    }

    private StringRequest getStringRequestRegisterUser(final String username, final String email, final String password) {
        return new StringRequest(Method.POST, AppConfig.URL_ENCRYPTED, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Register Response: " + response);
                hideDialog();

                try {
                    JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");
                    if (!error) {
                        new AlertDialog.Builder(LoginActivity.this)
                                .setTitle(getString(R.string.alert_accregistration_title))
                                .setMessage(getString(R.string.alert_accregistration_text))
                                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        Intent intent = new Intent(
                                                LoginActivity.this,
                                                LoginActivity.class);
                                        intent.putExtra(AppConfig.EXTRA_MESSAGE, AppConfig.INTENT_LOGIN);
                                        startActivity(intent);
                                        finish();
                                    }
                                })
                                .setIcon(android.R.drawable.ic_dialog_alert)
                                .show();
                    } else {
                        String errorMsg = jObj.getString("error_msg");
                        Log.e(TAG, "Error occured: "+errorMsg);
                        Toast.makeText(getApplicationContext(), getResources().getString(R.string.txt_errormsg_register), Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Registration Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        getResources().getString(R.string.txt_errormsg_register), Toast.LENGTH_LONG).show();
                hideDialog();
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                params.put("tag", AppConfig.REST_REGISTER);
                params.put("username", username);
                params.put("email", email);
                params.put("password", password);

                return params;
            }
        };
    }


    public void showMessage(String statusText){
        Toast.makeText(this, statusText, Toast.LENGTH_LONG).show();
    }

    public void onFragmentInteraction(Uri uri){
    }

    private void showDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

    private void hideDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }

    @Override
    public void onBackPressed() {
        gotoStartActivity();
        finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_common, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if((id == R.id.menu_action_exit)){
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }else if((id == R.id.menu_action_about)){
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(AppConfig.URL_ABOUT));
            startActivity(browserIntent);
        }else if((id == R.id.menu_action_help)){
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(AppConfig.URL_CONTACT));
            startActivity(browserIntent);
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == GooglePlusButtonFragment.RC_SIGN_IN) {
            GooglePlusButtonFragment fragment = (GooglePlusButtonFragment) getSupportFragmentManager().findFragmentById(R.id.frag_login_btnGP);
            fragment.onActivityResult(requestCode, resultCode, data);
        }

        TwitterButtonFragment twitter_fragment = (TwitterButtonFragment) getSupportFragmentManager().findFragmentById(R.id.frag_login_btnTW);
        if (twitter_fragment != null) {
            twitter_fragment.onActivityResult(requestCode, resultCode, data);
        }

        FacebookButtonFragment facebook_fragment = (FacebookButtonFragment) getSupportFragmentManager().findFragmentById(R.id.frag_login_btnFB);
        if (facebook_fragment != null) {
            facebook_fragment.onActivityResult(requestCode, resultCode, data);
        }

    }
}
