package com.example.tampopo_client.viewmodels;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.tampopo_client.models.FriendPair;
import com.example.tampopo_client.resources.FriendsResource;
import com.example.tampopo_client.resources.UserResource;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import retrofit2.http.Field;

public class FriendViewModel extends ViewModel {

    private final Retrofit retrofit;
    private final UserResource usersResource;
    private final FriendsResource friendsResource;
    private final MutableLiveData<List<String>> friendIdsLiveData;
    private final MutableLiveData<FriendPair> friendPair;

    public FriendViewModel(){
        this.retrofit = new Retrofit.Builder()
                .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/tampopo/")
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
        this.usersResource = retrofit.create(UserResource.class);
        this.friendsResource = retrofit.create(FriendsResource.class);
        this.friendIdsLiveData = new MutableLiveData<>();
        this.friendPair = new MutableLiveData<>();
    }

    public MutableLiveData<List<String>> getFriendIdsLiveData(){
        return this.friendIdsLiveData;
    }
    public MutableLiveData<FriendPair> getFriendPair(){
        return this.friendPair;
    }

    public void loadFriends(String userId, String token){
        //userResourceではなくfriendResourceに書き換え
        Call<List<String>> call = friendsResource.getFriends(userId, token);

        call.enqueue(new Callback<List<String>>() {
            @Override
            public void onResponse(Call<List<String>> call, Response<List<String>> response) {
                if(response.isSuccessful()){
                    List<String> friendIds = response.body();
                    friendIdsLiveData.setValue(friendIds);
                    System.out.println(response.code());
                }else{
                    System.out.println(response.code());
                }
            }

            @Override
            public void onFailure(Call<List<String>> call, Throwable t) {
                System.out.println("NetWorkError" + t);
            }
        });
    }

    public void createFriend(String token, String user0Id, String user1Id){
        Call<FriendPair> call = friendsResource.createFriend(token, user0Id, user1Id);

        call.enqueue(new Callback<FriendPair>() {
            @Override
            public void onResponse(Call<FriendPair> call, Response<FriendPair> response) {
                if(response.isSuccessful()){
                    friendPair.setValue(response.body());
                    System.out.println(response.code());
                }else{
                    System.out.println(response.code());
                }
            }

            @Override
            public void onFailure(Call<FriendPair> call, Throwable t) {
                System.out.println("NetWorkError" + t);
            }
        });
    }
}
