package ch.pizzalemon.android.activity;

import android.content.Intent;
import androidx.annotation.NonNull;
import android.os.Bundle;
import android.widget.Button;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ch.pizzalemon.android.R;
import ch.pizzalemon.android.api.ApiErrorUtils;
import ch.pizzalemon.android.api.ApiService;
import ch.pizzalemon.android.api.ResponseObject;
import ch.pizzalemon.android.helper.DialogHelper;
import ch.pizzalemon.android.helper.SessionHelper;
import ch.pizzalemon.android.helper.ViewHelper;
import ch.pizzalemon.android.view.AppEditText;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ForgotPasswordActivity extends BaseActivity {

    @BindView(R.id.forgotPasswordEmailPizzalinkEditText)
    AppEditText forgotPasswordEmailAppEditText;
    @BindView(R.id.resetPasswordButton) Button resetPasswordButton;

    @BindString(R.string.alert_fill_all_fields) String fillAllFieldsText;
    @BindString(R.string.alert_invalid_email) String validEmailText;
    @BindString(R.string.password_reset) String passwordResetText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgot_password);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.resetPasswordButton)
    protected void onClick(){
        ViewHelper.hideKeyboard();
        if(checkFields()){
            resetPassword();
        }
    }

    private boolean checkFields(){

        if(forgotPasswordEmailAppEditText.isEmpty()){
            DialogHelper.showAlertDialog(this, fillAllFieldsText);
            return false;
        }

        if(!forgotPasswordEmailAppEditText.isEmail()){
            DialogHelper.showAlertDialog(this, validEmailText);
            return false;
        }

        return true;
    }

    private void resetPassword(){
        DialogHelper.showLoadingDialog();
        Call<ResponseObject> call = ApiService.apiInterface.forgotPassword(
                SessionHelper.getSelectedStore().getStoreName(),
                forgotPasswordEmailAppEditText.getText());
        call.enqueue(new Callback<ResponseObject>() {
            @Override
            public void onResponse(Call<ResponseObject> call, Response<ResponseObject> response) {
                DialogHelper.hideLoadingDialog();
                if(response.isSuccessful() && response.body().isSuccess()){
                    DialogHelper.showOneButtonDialogWithCallback(passwordResetText, new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            Intent intent = new Intent(BaseActivity.currentActivity, LoginActivity.class);
                            BaseActivity.currentActivity.startActivity(intent);
                            BaseActivity.currentActivity.finishAffinity();
                        }
                    });
                }
                else{
                    ApiErrorUtils.parseError(response);
                }
            }

            @Override
            public void onFailure(Call<ResponseObject> call, Throwable t) {
                DialogHelper.hideLoadingDialog();
                DialogHelper.showFailedDialog();
            }
        });
    }
}
