package ch.pizzalemon.android.activity;

import android.os.Bundle;
import android.widget.Button;

import java.util.HashMap;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ch.pizzalemon.android.R;
import ch.pizzalemon.android.api.ApiEndPoints;
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 UpdatePasswordActivity extends BaseActivity {

    @BindView(R.id.oldPasswordPizzalinkEditText)
    AppEditText oldPasswordAppEditText;
    @BindView(R.id.newPasswordPizzalinkEditText)
    AppEditText newPasswordAppEditText;
    @BindView(R.id.confirmNewPasswordPizzalinkEditText)
    AppEditText confirmNewPasswordAppEditText;
    @BindView(R.id.updatePasswordButton) Button updatePasswordButton;

    @BindString(R.string.alert_fill_all_fields) String fillAllFieldsText;
    @BindString(R.string.alert_passwords_not_matched) String passwordsNotMatchedText;
    @BindString(R.string.password_updated) String passwordUpdatedText;

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

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

    private boolean checkFields(){

        if(oldPasswordAppEditText.isEmpty() ||
                newPasswordAppEditText.isEmail() ||
                confirmNewPasswordAppEditText.isEmpty()){
            DialogHelper.showAlertDialog(BaseActivity.currentActivity, fillAllFieldsText);
            return false;
        }

        if(!newPasswordAppEditText.getText().equals(confirmNewPasswordAppEditText.getText())){
            DialogHelper.showAlertDialog(BaseActivity.currentActivity, passwordsNotMatchedText);
            return false;
        }

        return true;
    }

    private void updatePassword(){
        DialogHelper.showLoadingDialog();
        Call<ResponseObject> call = ApiService.apiInterface.updatePassword(
                "/" + SessionHelper.getSelectedStore().getStoreName() + ApiEndPoints.API_UPDATE_PASSWORD + SessionHelper.getCustomerToken().getToken(),
                getUpdatePasswordRequestParams());
        call.enqueue(new Callback<ResponseObject>() {
            @Override
            public void onResponse(Call<ResponseObject> call, Response<ResponseObject> response) {
                DialogHelper.hideLoadingDialog();
                if(response.isSuccessful() &&
                        response.body().getData() != null &&
                        response.body().isSuccess()){
                    DialogHelper.showDialogWithPositiveButton(BaseActivity.currentActivity, passwordUpdatedText);
                }
                else {
                    ApiErrorUtils.parseError(response);
                }
            }

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

    private HashMap<String, Object> getUpdatePasswordRequestParams(){
        HashMap<String, Object> params = new HashMap<>();
        params.put("oldPassword", oldPasswordAppEditText.getText());
        params.put("password", newPasswordAppEditText.getText());
        params.put("confirm", confirmNewPasswordAppEditText.getText());
        return params;
    }
}
