package com.airson.alive.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.airson.alive.R;
import com.airson.alive.activity.base.BaseActivity;
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;

/**
 * Created by Shrikant on 4/22/18.
 */

public class SignupActivity extends BaseActivity {

    @BindView(R.id.et_mobile)
    EditText userName;
    @BindView(R.id.et_password)
    EditText paswword;
    @BindView(R.id.et_confirmpassword)
    EditText confirmPassword;
    @BindView(R.id.sigupBtn)
    Button sigupBtn;
    @BindView(R.id.login)
    TextView login;
    String usernameString = "", passwordString = "", confirmPasswordString = "";

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

    @Override
    protected void setUp() {

    }

    @OnClick(R.id.sigupBtn)
    public void OnClickSignIn(View view){

        if (CommonUtils.isInternetOn(this, true)){
            if (isValidInput()){

                showLoading();
                try {
                    ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
                    Call<UserResponseModel> call = apiService.getSignup(usernameString, passwordString);
                    call.enqueue(new Callback<UserResponseModel>() {
                        @Override
                        public void onResponse(Call<UserResponseModel> call, Response<UserResponseModel> response) {
                            hideLoading();
                            if(response.body()!=null){
                                if(response.body().isError()==false){

                                    Preferences.getInstance().setAuthToken(SignupActivity.this, response.body().getUser().getAuthtoken());
                                    Preferences.getInstance().setUserName(SignupActivity.this, response.body().getUser().getName());
                                    Preferences.getInstance().setEmail(SignupActivity.this, response.body().getUser().getEmail());
                                    Preferences.getInstance().setMobile(SignupActivity.this, response.body().getUser().getUsername());
                                    Preferences.getInstance().setGender(SignupActivity.this, response.body().getUser().getGender());
                                    Preferences.getInstance().setIsLogin(SignupActivity.this, true);
                                    Preferences.getInstance().setIsAddMedicalID(SignupActivity.this, false);

                                    Intent intent = new Intent(SignupActivity.this, MedicalIDActivity.class);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                                    startActivity(intent);
                                    finish();

                                } else {
                                    showMessage(response.body().getMessage());
                                }
                            }


                        }

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


                        }
                    });


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

                }
            }
        }

    }

    @OnClick(R.id.login)
    public void OnClickSignUp(View view){

//        Intent intent = new Intent(this, SignupActivity.class);
//        startActivity(intent);
        finish();
    }


    private boolean isValidInput() {

        Boolean isValid = true;
        usernameString = userName.getText().toString();
        passwordString = paswword.getText().toString();
        confirmPasswordString = confirmPassword.getText().toString();

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

        } else if (usernameString.length()>0 && usernameString.length()<10) {
            isValid = false;
            onError(getString(R.string.invalid_mobile));

        }else  if (passwordString ==null || passwordString.isEmpty()) {
            isValid = false;
            onError(getString(R.string.enter_password));
        } else  if (confirmPasswordString ==null || confirmPasswordString.isEmpty() || !confirmPasswordString.equalsIgnoreCase(passwordString)) {
            isValid = false;
            onError(getString(R.string.enter_confirm_password));
        }

        return isValid;
    }

}
