package org.ntlab.acanthus_client.views.login;

import android.util.Log;
import android.widget.EditText;

import androidx.annotation.RestrictTo;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import org.ntlab.acanthus_client.Acanthus;
import org.ntlab.acanthus_client.entities.LoginResponseJson;
import org.ntlab.acanthus_client.resources.accounts.LoginRest;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

//-----------------------------------------------------------------
//
public class LoginScreenViewModel extends ViewModel {

    private MutableLiveData<Acanthus> acanthusMutableLiveData;
    private Retrofit retrofit;

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    //
    public LoginScreenViewModel() {
        this.acanthusMutableLiveData = new MutableLiveData<>();
        this.retrofit = new Retrofit.Builder()
                .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/acanthus/")
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
    }

    //-----------------------------------------------------------------
    // getter
    public LiveData<Acanthus> getAcanthus() {
        return this.acanthusMutableLiveData;
    }

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    // トークンの発行APIの呼び出し
    public void issueLoginToken(Acanthus acanthus, EditText emailForm, EditText passwordForm) {
        final LoginRest loginRest = retrofit.create(LoginRest.class);

        String emailAddress = emailForm.getText().toString();
        String password = passwordForm.getText().toString();

        // ログイン
        Call<LoginResponseJson> call = loginRest.issueLoginToken(emailAddress, password);
        call.enqueue(new Callback<LoginResponseJson>() {
            @Override
            public void onResponse(Call<LoginResponseJson> call, Response<LoginResponseJson> response) {
                if (response.isSuccessful()) {
                    LoginResponseJson responseJson = response.body();
                    setVariableFromResponse(acanthus, responseJson, emailAddress, password);
                }
            }

            @Override
            public void onFailure(Call<LoginResponseJson> call, Throwable t) {
                Log.d("login:", "failed");
            }
        });
    }

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    // ログイン成功時に値の変更を行う
    private void setVariableFromResponse(Acanthus acanthus, LoginResponseJson loginResponse, String email, String password) {
        acanthus.setPreferenceUid(loginResponse.getUid());
        acanthus.setPreferenceToken(loginResponse.getToken());
        acanthus.setPreferenceEmail(email);
        acanthus.setPreferencePassword(password);
        acanthusMutableLiveData.setValue(acanthus);
    }
    //-----------------------------------------------------------------

}
