package org.ntlab.acanthus_client.views.userpage;

import org.ntlab.acanthus_client.entities.FollowAddJson;
import org.ntlab.acanthus_client.entities.FollowUidJson;
import org.ntlab.acanthus_client.resources.accounts.FollowsRest;

import java.util.ArrayList;
import java.util.Collection;

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

public class UserPageViewModel extends ViewModel {

    private Collection<Integer> followUidList;

    public UserPageViewModel(){
        this.followUidList = new ArrayList<>();
    }

    public Collection<Integer> getFollowUidList(){
        return this.followUidList;
    }

    //ユーザーのフォローリストを取得する
    public void getFollows(Integer uid){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/acanthus/")
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
        final FollowsRest followsRest = retrofit.create(FollowsRest.class);

        //フォローリストの取得
        Call<FollowUidJson> call = followsRest.getFollows(uid);
        call.enqueue(new Callback<FollowUidJson>() {
            @Override
            public void onResponse(Call<FollowUidJson> call, Response<FollowUidJson> response) {
                if (response.isSuccessful()) {
                    if (response.body() != null) {
                        followUidList = response.body().getUidList();
                    }
                }
            }

            @Override
            public void onFailure(Call<FollowUidJson> call, Throwable t) {

            }
        });

    }

    //ユーザーをフォローする
    public void addFollow(Integer uid, String token, Integer followUid){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/acanthus/")
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
        final FollowsRest followsRest = retrofit.create(FollowsRest.class);

        //フォローの追加
        Call<FollowAddJson> call = followsRest.addFollows(uid, token, followUid);
        call.enqueue(new Callback<FollowAddJson>() {
            @Override
            public void onResponse(Call<FollowAddJson> call, Response<FollowAddJson> response) {
                if (response.isSuccessful()) {
                    if (response.body() != null) {

                    }
                }
            }

            @Override
            public void onFailure(Call<FollowAddJson> call, Throwable t) {

            }
        });

    }

    //ユーザーのフォローを解除
    public void releaseFollow(Integer uid, String token, Integer followUid){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/acanthus/")
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
        final FollowsRest followsRest = retrofit.create(FollowsRest.class);

        //フォローの解除
        Call<String> call = followsRest.releaseFollows(uid, token, followUid);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    if (response.body() != null) {

                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });

    }

}
