package app.fedilab.fedilabtube.viewmodel;
/* Copyright 2020 Thomas Schneider
 *
 * This file is a part of TubeLab
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with TubeLab; if not,
 * see <http://www.gnu.org/licenses>. */

import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import app.fedilab.fedilabtube.client.APIResponse;
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;


public class CommentVM extends AndroidViewModel {
    private MutableLiveData<APIResponse> apiResponseMutableLiveData;

    public CommentVM(@NonNull Application application) {
        super(application);
    }

    public LiveData<APIResponse> getThread(String videoId, String max_Id) {
        apiResponseMutableLiveData = new MutableLiveData<>();
        getThreadComments(videoId, max_Id);
        return apiResponseMutableLiveData;
    }

    public LiveData<APIResponse> getRepliesComment(String videoId, String commentId) {
        apiResponseMutableLiveData = new MutableLiveData<>();
        getThreadRepliesComments(videoId, commentId);
        return apiResponseMutableLiveData;
    }


    private void getThreadComments(String videoId, String max_id) {
        Context _mContext = getApplication().getApplicationContext();
        new Thread(() -> {
            try {
                RetrofitPeertubeAPI api = new RetrofitPeertubeAPI(_mContext);
                APIResponse apiResponse = api.getComments(CommentVM.action.GET_THREAD, videoId, null, max_id);
                Handler mainHandler = new Handler(Looper.getMainLooper());
                Runnable myRunnable = () -> apiResponseMutableLiveData.setValue(apiResponse);
                mainHandler.post(myRunnable);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }


    private void getThreadRepliesComments(String videoId, String commentId) {
        Context _mContext = getApplication().getApplicationContext();
        new Thread(() -> {
            try {
                RetrofitPeertubeAPI api = new RetrofitPeertubeAPI(_mContext);
                APIResponse apiResponse = api.getComments(action.GET_REPLIES, videoId, null, null);
                Handler mainHandler = new Handler(Looper.getMainLooper());
                Runnable myRunnable = () -> apiResponseMutableLiveData.setValue(apiResponse);
                mainHandler.post(myRunnable);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }

    public enum action {
        GET_THREAD,
        GET_REPLIES
    }
}
