package com.kfb.kfbv1.db;

import android.content.Context;
import android.os.AsyncTask;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.room.DatabaseConfiguration;
import androidx.room.InvalidationTracker;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;
import androidx.sqlite.db.SupportSQLiteOpenHelper;

import java.util.List;

public class DbManage {
    private DbAbstractManage db;


    public DbManage(Context context) {
        db = Room.databaseBuilder(context, DbAbstractManage.class, DbAbstractManage.DB_NAME).allowMainThreadQueries().
                fallbackToDestructiveMigration()
                .build();
    }

    public  List<Pomiar> getPomiarAll() {
        return db.daoPomiar().getAll() ;
    }

    public void insertPomiar(final Pomiar pom)
    {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... voids) {
                db.daoPomiar().insertAll(pom);
                return null;
            }
        }.execute();
    }

    public  List<Urzadzenia> getUrzadzeniaAll() {
        return db.daoUrzazenie().getAll() ;
    }

    public Urzadzenia getUrzadzenie(int id)
    {
        return db.daoUrzazenie().loadAllByIds(id);
    }
    public void insertUrzazenie(final Urzadzenia urz)
    {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... voids) {
                db.daoUrzazenie().insertAll(urz);
                return null;
            }
        }.execute();
    }
    public void updateUrzazenie(final Urzadzenia urz)
    {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... voids) {
                db.daoUrzazenie().update(urz);
                return null;
            }
        }.execute();

    }

    public void insertTask(String login,
                           String passwd,
                           boolean active) {

        User us = new User();
        us.login = login;
        us.passwd = passwd;
        us.active = active;


        insertTask(us);
    }
    public void insertTask(final User us) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... voids) {
                db.daoUsers().insertAll(us);
                return null;
            }
        }.execute();
    }
    public void updateTask(final User us) {

    }
    public void deleteTask(final User us) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... voids) {
                db.daoUsers().delete(us);
                return null;
            }
        }.execute();
    }

    public User getUser(String login) {
        return db.daoUsers().loadAllByIds(login);
    }

    public  List<User> getUsers() {
        return db.daoUsers().getAll() ;
    }

    public void close()
    {
         //  db.close();
    }
}
