package org.ntlab.acanthus_client.views.userpage;

import android.util.Log;

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

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;

public class UserPageViewModel extends ViewModel {

    private MutableLiveData<FollowUidJson> followUidJsonMutableLiveData;
    private MutableLiveData<FollowerUidJson> followerUidJsonMutableLiveData;

    public UserPageViewModel(){
        this.followUidJsonMutableLiveData = new MutableLiveData<>();
        this.followerUidJsonMutableLiveData = new MutableLiveData<>();
    }

    public LiveData<FollowUidJson> getFollowUidJson(){
        return this.followUidJsonMutableLiveData;
    }

    public LiveData<FollowerUidJson> getFollowerUidJson() {
        return this.followerUidJsonMutableLiveData;
    }

    //ユーザーのフォローリストを取得する
    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) {
                        followUidJsonMutableLiveData.setValue(response.body());
                    }
                }
            }

            @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<Void> call = followsRest.releaseFollows(uid, token, followUid);
        call.enqueue(new Callback<Void>() {
            @Override
            public void onResponse(Call<Void> call, Response<Void> response) {

            }

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

            }
        });

    }

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

        //フォロワーリストの取得
        Call<FollowerUidJson> call = followersRest.getFollowers(uid);
        call.enqueue(new Callback<FollowerUidJson>() {
            @Override
            public void onResponse(Call<FollowerUidJson> call, Response<FollowerUidJson> response) {
                if (response.isSuccessful()) {
                    if (response.body() != null) {
                        followerUidJsonMutableLiveData.setValue(response.body());
                    }
                }
            }

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

            }
        });

    }

}
