package com.dowerx.quack.service;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.loader.content.AsyncTaskLoader;

import com.dowerx.quack.model.User;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.Filter;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutionException;

public class UserService {
    public static class GetUserByUsername extends AsyncTaskLoader<User> {
        private final String username;

        public GetUserByUsername(@NonNull Context context, String username) {
            super(context);
            this.username = username;
        }

        @Nullable
        @Override
        public User loadInBackground() {
            FirebaseFirestore db = FirebaseFirestore.getInstance();
            try {
                DocumentSnapshot user = Tasks.await(db.document("/user/" + username).get());
                return new User(
                        user.get("username", String.class),
                        user.get("email", String.class),
                        user.get("id", String.class));
            } catch (ExecutionException | InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        protected void onStartLoading() {
            forceLoad();
        }
    }
    public static class GetUserById extends AsyncTaskLoader<User> {

        private final String id;

        public GetUserById(@NonNull Context context, String id) {
            super(context);
            this.id = id;
        }

        @Nullable
        @Override
        public User loadInBackground() {
            FirebaseFirestore db = FirebaseFirestore.getInstance();
            try {
                DocumentSnapshot user = Tasks.await(db.collection("user").where(Filter.equalTo("id", id)).get()).getDocuments().get(0);
                return new User(
                        user.get("username", String.class),
                        user.get("email", String.class),
                        user.get("id", String.class));
            } catch (ExecutionException | InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        protected void onStartLoading() {
            forceLoad();
        }
    }

    public static class SearchUsername extends AsyncTaskLoader<List<User>> {
        private final String username;

        public SearchUsername(String username, Context context) {
            super(context);
            this.username = username;
        }

        @Nullable
        @Override
        public List<User> loadInBackground() {
            List<User> users = new LinkedList<>();
            try {
                FirebaseFirestore db = FirebaseFirestore.getInstance();
                // firestore doesn't have full-string search
                // ugly, but i have to filter it myself
                QuerySnapshot result = Tasks.await(db.collection("user").get());

                for (DocumentSnapshot doc : result) {
                    if (doc.getString("username").contains(username)) {
                        users.add(new User(
                                doc.getString("username"),
                                doc.getString("email"),
                                doc.getString("id")
                        ));
                    }
                }
            } catch (Exception e) {
            }

            return users;
        }

        @Override
        protected void onStartLoading() {
            forceLoad();
        }
    }
}
