package org.ntlab.acanthus_client.views.title;

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

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

import static java.lang.Thread.sleep;

public class TitleViewModel extends ViewModel {
    private MutableLiveData<Acanthus> acanthusMutableLiveData;
    private MutableLiveData<Boolean> loginStatus;
    public TitleViewModel() {
        this.acanthusMutableLiveData = new MutableLiveData<>();
        this.loginStatus = new MutableLiveData<>();
    }

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


    public void LoginToken(Acanthus acanthus, String emailForm, String passwordForm) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/acanthus/")
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
        final LoginRest loginRest = retrofit.create(LoginRest.class);

        String emailAddress = emailForm;
        String password = passwordForm;

        // ログイン
        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()) {
                    if(response.body()!=null){
                        setVariableFromResponse(acanthus, response.body().getToken(), emailAddress, password);
                    }else{
                        loginStatus.setValue(false);
                    }
                }else{
                    loginStatus.setValue(false);
                }
            }

            @Override
            public void onFailure(Call<LoginResponseJson> call, Throwable t) {
                loginStatus.setValue(false);
            }
        });
    }

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