package it.integry.integrywmsnative.gest.login.core;

import android.content.Context;
import android.util.Log;

import it.integry.integrywmsnative.core.CommonConst;
import it.integry.integrywmsnative.core.REST.RESTBuilder;
import it.integry.integrywmsnative.core.REST.consumers.ISimpleOperationCallback;
import it.integry.integrywmsnative.core.REST.consumers.ISingleOperationDoubleCallback;
import it.integry.integrywmsnative.core.REST.model.EsitoType;
import it.integry.integrywmsnative.core.REST.model.ServiceRESTResponse;
import it.integry.integrywmsnative.gest.login.dto.LoginAziendaDTO;
import it.integry.integrywmsnative.gest.login.dto.LoginDTO;
import it.integry.integrywmsnative.gest.login.rest.LoginRESTConsumerService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginHelper {

    private Context mContext;

    private String mCodAzienda;
    private String mUsername;
    private String mPassword;

    public LoginHelper(Context context, String codAzienda, String username, String password){
        this.mContext = context;

        this.mCodAzienda = codAzienda;
        this.mUsername = username;
        this.mPassword = password;
    }


    public void retrieveServerData(final ISimpleOperationCallback<LoginAziendaDTO> callback){

        String host = CommonConst.Login.Azienda.host;
        int port = CommonConst.Login.Azienda.port;

        LoginRESTConsumerService service = RESTBuilder.getService(LoginRESTConsumerService.class, host, port, false);
        service.loginAzienda(mCodAzienda).enqueue(new Callback<ServiceRESTResponse<LoginAziendaDTO>>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<LoginAziendaDTO>> call, Response<ServiceRESTResponse<LoginAziendaDTO>> response) {
                if(response.isSuccessful()) {

                    if(response.body() != null) {
                        if(response.body().getEsito() == EsitoType.OK) {
                            callback.onSuccess(response.body().getDto());
                        } else {
                            Log.e("LoginAzienda", response.body().getErrorMessage());
                            callback.onFailed(new Exception(response.body().getErrorMessage()));
                        }
                    } else {
                        Log.e("LoginAzienda", response.message());
                        callback.onFailed(new Exception(response.message()));
                    }
                } else {
                    Log.e("LoginAzienda", "Status " + response.code() + ": " + response.message());
                    callback.onFailed(new Exception("Status " + response.code() + ": " + response.message()));
                }
            }

            @Override
            public void onFailure(Call<ServiceRESTResponse<LoginAziendaDTO>> call, Throwable t) {
                Log.e("LoginAzienda", t.toString());
                callback.onFailed(new Exception(t));
            }
        });
    }


    public void doLogin(String host, int port, final ISimpleOperationCallback<LoginDTO> callback){
        LoginRESTConsumerService service = RESTBuilder.getService(LoginRESTConsumerService.class, host, port, false);
        service.login(mUsername, mPassword).enqueue(new Callback<ServiceRESTResponse<LoginDTO>>() {
            @Override
            public void onResponse(Call<ServiceRESTResponse<LoginDTO>> call, Response<ServiceRESTResponse<LoginDTO>> response) {

                if(response.isSuccessful()) {

                    if(response.body() != null) {
                        if(response.body().getEsito() == EsitoType.OK) {
                            callback.onSuccess(response.body().getDto());
                        } else {
                            Log.e("Login", response.body().getErrorMessage());
                            callback.onFailed(new Exception(response.body().getErrorMessage()));
                        }
                    } else {
                        Log.e("Login", response.message());
                        callback.onFailed(new Exception(response.message()));
                    }
                } else {
                    Log.e("Login", "Status " + response.code() + ": " + response.message());
                    callback.onFailed(new Exception("Status " + response.code() + ": " + response.message()));
                }


            }

            @Override
            public void onFailure(Call<ServiceRESTResponse<LoginDTO>> call, final Throwable t) {

                Log.e("Login", t.toString());
                callback.onFailed(new Exception(t));



            }
        });
    }


}
