package ch.pizzalink.android.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.HashMap;

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

public class UpdatePasswordActivity extends BaseActivity {

    @BindView(R.id.oldPasswordPizzalinkEditText) PizzalinkEditText oldPasswordPizzalinkEditText;
    @BindView(R.id.newPasswordPizzalinkEditText) PizzalinkEditText newPasswordPizzalinkEditText;
    @BindView(R.id.confirmNewPasswordPizzalinkEditText) PizzalinkEditText confirmNewPasswordPizzalinkEditText;
    @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(oldPasswordPizzalinkEditText.isEmpty() ||
                newPasswordPizzalinkEditText.isEmail() ||
                confirmNewPasswordPizzalinkEditText.isEmpty()){
            DialogHelper.showAlertDialog(BaseActivity.currentActivity, fillAllFieldsText);
            return false;
        }

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

        return true;
    }

    private void updatePassword(){
        DialogHelper.showLoadingDialog();
        Call<ResponseObject> call = ApiService.apiInterface.updatePassword(
                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", oldPasswordPizzalinkEditText.getText());
        params.put("password", newPasswordPizzalinkEditText.getText());
        params.put("confirm", confirmNewPasswordPizzalinkEditText.getText());
        return params;
    }
}
