package com.airson.alive.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.airson.alive.R;
import com.airson.alive.activity.base.BaseActivity;
import com.airson.alive.model.ResponseModel;
import com.airson.alive.model.UserResponseModel;
import com.airson.alive.network.ApiClient;
import com.airson.alive.network.ApiInterface;
import com.airson.alive.preference.Preferences;
import com.airson.alive.utils.CommonUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ChangePasswordActivity extends BaseActivity{

    @BindView(R.id.currentPassword)
    EditText currentPassword;
    @BindView(R.id.newPassword)
    EditText newPassword;
    @BindView(R.id.conifrmPassword)
    EditText confirmPassword;
    @BindView(R.id.resetbtn)
    Button resetButton;

    String currentPasswordString = "", newPasswordString = "", confirmPasswordString = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        setUnBinder(ButterKnife.bind(this));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("Change Password");
    }

    @Override
    protected void setUp() {

    }

    @OnClick(R.id.resetbtn)
    public void OnClickReset(View view){
        if (CommonUtils.isInternetOn(this, true)) {
            if (isValidInput()) {
                showLoading();
                try {
                    ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
                    Call<ResponseModel> call = apiService.changePassword(Preferences.getInstance().getAuthToken(ChangePasswordActivity.this), Preferences.getInstance().getMobile(ChangePasswordActivity.this),
                            currentPasswordString, newPasswordString);
                    call.enqueue(new Callback<ResponseModel>() {
                        @Override
                        public void onResponse(Call<ResponseModel> call, Response<ResponseModel> response) {
                            hideLoading();
                            if (response.body() != null) {
                                if (response.body().isError() == false) {
                                    showMessage(response.body().getMessage());
                                    finish();
                                } else {
                                    showMessage(response.body().getMessage());
                                }
                            }
                        }

                        @Override
                        public void onFailure(Call<ResponseModel> call, Throwable t) {
                            // Log error here since request failed
                            hideLoading();
                            showMessage(t.getMessage());


                        }
                    });


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

                }
            }
        }

    }

    private boolean isValidInput() {

        Boolean isValid = true;
        currentPasswordString = currentPassword.getText().toString();
        newPasswordString = newPassword.getText().toString();
        confirmPasswordString = confirmPassword.getText().toString();


        if (currentPasswordString ==null || currentPasswordString.isEmpty()) {
            isValid = false;
            onError(getString(R.string.enter_current_password));

        } else if (newPasswordString ==null || newPasswordString.isEmpty()) {
            isValid = false;
            onError(getString(R.string.enter_new_password));

        } else if (confirmPasswordString ==null || confirmPasswordString.isEmpty()) {
            isValid = false;
            onError(getString(R.string.enter_confirmpassword));
        } else if (!newPasswordString.equalsIgnoreCase(confirmPasswordString)) {
            isValid = false;
            onError(getString(R.string.password_not_match));
        }

        return isValid;
    }
}
