package com.pixelnx.eacademy.ui.forgotpassword;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Patterns;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.StringRequestListener;
import com.pixelnx.eacademy.R;
import com.pixelnx.eacademy.utils.AppConsts;
import com.pixelnx.eacademy.utils.ProjectUtils;
import com.pixelnx.eacademy.utils.widgets.CustomEditText;
import com.pixelnx.eacademy.utils.widgets.CustomSmallText;
import com.pixelnx.eacademy.utils.widgets.CustomTextExtraBold;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Locale;


public class ActivityForgotPassword extends Activity implements View.OnClickListener {
    CustomEditText emailAddress;
    RelativeLayout confirm_button;
    Context mContext;
    CustomSmallText msgTv;
    ImageView ivBack;
    CustomTextExtraBold tvHeader;
    static String checkLanguage = "en";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgot_password);
        emailAddress = findViewById(R.id.emailAddress);
        msgTv = findViewById(R.id.msgTv);
        ivBack = findViewById(R.id.ivBack);
        tvHeader = findViewById(R.id.tvHeader);
        tvHeader.setText(getResources().getString(R.string.app_name));
        ivBack.setOnClickListener(this);
        confirm_button = findViewById(R.id.confirm_button);
        confirm_button.setOnClickListener(this);
        mContext = ActivityForgotPassword.this;
        languageDynamic();
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.confirm_button) {
            if (!emailAddress.getText().toString().isEmpty()) {
                if (isValidEmail(emailAddress.getText().toString())) {
                    mailConfirmApi();
                } else {
                    emailAddress.setError(getResources().getString(R.string.InvalidEmail));

                }
            } else {
                emailAddress.setError("" + getResources().getString(R.string.Required_Field));
                emailAddress.requestFocus();
            }
        } else {
            if (view.getId() == R.id.ivBack) {
                onBackPressed();
            }
        }

    }

    public static boolean isValidEmail(CharSequence target) {
        return (!TextUtils.isEmpty(target) && Patterns.EMAIL_ADDRESS.matcher(target).matches());
    }

    void mailConfirmApi() {
        ProjectUtils.showProgressDialog(mContext, false, getResources().getString(R.string.Loading___));

        AndroidNetworking.post(AppConsts.BASE_URL + AppConsts.API_RESET_PASSWORD)
                .addBodyParameter(AppConsts.EMAIL, "" + emailAddress.getText().toString())
                .build().getAsString(new StringRequestListener() {
            @Override
            public void onResponse(String response) {
                ProjectUtils.pauseProgressDialog();
                try {
                    JSONObject jsonObject = new JSONObject(response);

                    msgTv.setText("" + jsonObject.getString("msg"));
                    hideKeyboard(ActivityForgotPassword.this);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(ANError anError) {
                ProjectUtils.pauseProgressDialog();
                Toast.makeText(mContext, getResources().getString(R.string.Something_went_wrong), Toast.LENGTH_SHORT).show();
                msgTv.setText(getResources().getString(R.string.Try_again_server_issue));

            }
        });

    }

    public static void hideKeyboard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        //Find the currently focused view, so we can grab the correct window token from it.
        View view = activity.getCurrentFocus();
        //If no view currently has focus, create a new one, just so we can grab a window token from it
        if (view == null) {
            view = new View(activity);
        }
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    void languageDynamic() {


        AndroidNetworking.post(AppConsts.BASE_URL + AppConsts.API_CHECKLANGUAGE)
                .build().getAsString(new StringRequestListener() {
            @Override
            public void onResponse(String response) {

                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if ("true".equalsIgnoreCase(jsonObject.getString("status"))) {

                        if (jsonObject.getString("languageName").equalsIgnoreCase("arabic")) {
                            //for rtl
                            Configuration configuration = getResources().getConfiguration();
                            configuration.setLayoutDirection(new Locale("fa"));
                            getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
                            String languageToLoad = "ar"; // your language
                            Locale locale = new Locale(languageToLoad);
                            Locale.setDefault(locale);
                            Configuration config = new Configuration();
                            config.locale = locale;
                            getBaseContext().getResources().updateConfiguration(config,
                                    getBaseContext().getResources().getDisplayMetrics());

                            if (!checkLanguage.equals("ar")) {
                                recreate();
                            }
                            checkLanguage = "ar";

                        }
                        if (jsonObject.getString("languageName").equalsIgnoreCase("french")) {
                            String languageToLoad = "fr"; // your language
                            Locale locale = new Locale(languageToLoad);
                            Locale.setDefault(locale);
                            Configuration config = new Configuration();
                            config.locale = locale;
                            getBaseContext().getResources().updateConfiguration(config,
                                    getBaseContext().getResources().getDisplayMetrics());
                            if (!checkLanguage.equals("fr")) {
                                recreate();
                            }
                            checkLanguage = "fr";

                        }
                        if (jsonObject.getString("languageName").equalsIgnoreCase("english")) {
                            String languageToLoad = "en"; // your language
                            Locale locale = new Locale(languageToLoad);
                            Locale.setDefault(locale);
                            Configuration config = new Configuration();
                            config.locale = locale;
                            getBaseContext().getResources().updateConfiguration(config,
                                    getBaseContext().getResources().getDisplayMetrics());
                            if (!checkLanguage.equals("en")) {
                                recreate();
                            }
                            checkLanguage = "en";


                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(ANError anError) {

            }
        });


    }
}