package com.naijab.nextzytimeline.ui.people.manager;

import android.content.Context;

import com.naijab.nextzytimeline.utility.StringUtility;

import io.realm.Realm;
import io.realm.RealmResults;
import io.realm.Sort;

public class PeopleManager {

    private static PeopleManager peopleManager;
    private RealmResults<PeopleModel> resultRealm;
    private PeopleModel peopleItem;
    private Realm realm = Realm.getDefaultInstance();
    private int id = 0;

    public PeopleManager(Realm realm) {
        this.realm = realm;
    }

    public static PeopleManager getInstance(Realm realm) {
        if (peopleManager == null) {
            peopleManager = new PeopleManager(realm);
        }
        return peopleManager;
    }

    public Realm getRealm() {
        return realm;
    }

    public RealmResults<PeopleModel> getPeople() {
        realm = Realm.getDefaultInstance();
        return realm.where(PeopleModel.class)
                .findAllSorted("id", Sort.DESCENDING);
    }

    public RealmResults<PeopleModel> getPeopleByName() {
        realm = Realm.getDefaultInstance();
        return realm.where(PeopleModel.class)
                .findAllSorted("name", Sort.ASCENDING);
    }

    public RealmResults<PeopleModel> getPeopleByPosition() {
        realm = Realm.getDefaultInstance();
        return realm.where(PeopleModel.class)
                .findAllSorted("job", Sort.ASCENDING);
    }

    public PeopleModel getPeople(int id) {
        return realm.where(PeopleModel.class)
                .equalTo("id", id)
                .findFirst();
    }

    public void deleteAll(final Context context, final onDeleteCallBack callBack) {
        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                realm.delete(PeopleModel.class);
            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {
                callBack.onDeleteSuccess(StringUtility.getDeleteSuccess(context));
            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                callBack.onDeleteError(StringUtility.getDeleteError(context) + ": " +error.getMessage());
            }
        });

    }

    public void deleteByID(final int id,
                           final Context context,
                           final onDeleteCallBack callBack) {
        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm bgRealm) {
                RealmResults<PeopleModel> results = bgRealm.where(PeopleModel.class)
                        .equalTo("id", id)
                        .findAll();
                results.deleteAllFromRealm();
            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {
                callBack.onDeleteSuccess(StringUtility.getDeleteSuccess(context));
            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                callBack.onDeleteError(StringUtility.getDeleteError(context) + ": " + error.getMessage());
            }
        });

    }

    public void saveRealm(final PeopleModel peopleModel,
                          final Context context,
                          final onSaveCallBack callBack) {

        resultRealm = realm.where(PeopleModel.class)
                .findAll();

        id = resultRealm.size() + 1;

        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm bgRealm) {

                PeopleModel people = bgRealm.createObject(PeopleModel.class, id);
                people.setName(peopleModel.getName());
                people.setJob(peopleModel.getJob());
                people.setBirthDay(peopleModel.getBirthDay());
                people.setJobStart(peopleModel.getJobStart());
                people.setJobDescription(peopleModel.getJobDescription());
                people.setGame(peopleModel.getGame());
                people.setSmartPhone(peopleModel.getSmartPhone());
                people.setPhoto(peopleModel.getPhoto());
                people.setProfile(peopleModel.getProfile());


            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {
                callBack.onSaveSuccess(StringUtility.getSaveSuccess(context));
            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                callBack.onSaveError(StringUtility.getDeleteError(context) + ": " +error.getMessage());
            }
        });
    }


    public void editRealm(final PeopleModel peopleModel,
                          final Context context,
                          final onEditCallBack callBack) {


        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm bgRealm) {

                RealmResults<PeopleModel> results = bgRealm.where(PeopleModel.class)
                        .findAll();

                peopleItem = results.where().equalTo("id", peopleModel.getId())
                        .findFirst();

                peopleItem.setName(peopleModel.getName());
                peopleItem.setJob(peopleModel.getJob());
                peopleItem.setBirthDay(peopleModel.getBirthDay());
                peopleItem.setJobStart(peopleModel.getJobStart());
                peopleItem.setJobDescription(peopleModel.getJobDescription());
                peopleItem.setGame(peopleModel.getGame());
                peopleItem.setSmartPhone(peopleModel.getSmartPhone());
                peopleItem.setPhoto(peopleModel.getPhoto());
                peopleItem.setProfile(peopleModel.getProfile());

                bgRealm.copyToRealmOrUpdate(peopleItem);

            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {
                callBack.onSaveSuccess(StringUtility.getSaveSuccess(context));
                realm.close();
            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                callBack.onSaveError(StringUtility.getSaveError(context) + ": " +error.getMessage());
            }
        });
    }


    public interface onSaveCallBack {
        void onSaveSuccess(String message);

        void onSaveError(String message);
    }

    public interface onEditCallBack {
        void onSaveSuccess(String message);

        void onSaveError(String message);
    }

    public interface onDeleteCallBack {
        void onDeleteSuccess(String message);

        void onDeleteError(String message);
    }

}

@@ -1611,31 +1611,8 @@
 nal 
-Context context, final 
 onDe
@@ -1628,32 +1628,32 @@
 ack callBack) %7B%0A
+
         realm.ex
@@ -2003,39 +2003,32 @@
 etDeleteSuccess(
-context
 ));%0A            
@@ -2207,39 +2207,32 @@
 .getDeleteError(
-context
 ) + %22: %22 +error.
@@ -2325,58 +2325,8 @@
 id,%0A
-                           final Context context,%0A
     
@@ -2913,39 +2913,32 @@
 etDeleteSuccess(
-context
 ));%0A            
@@ -3117,39 +3117,32 @@
 .getDeleteError(
-context
 ) + %22: %22 + error
@@ -3252,57 +3252,8 @@
 el,%0A
-                          final Context context,%0A
     
@@ -4378,39 +4378,32 @@
 .getSaveSuccess(
-context
 ));%0A            
@@ -4584,31 +4584,24 @@
 DeleteError(
-context
 ) + %22: %22 +er
@@ -4714,57 +4714,8 @@
 el,%0A
-                          final Context context,%0A
     
@@ -5981,23 +5981,16 @@
 Success(
-context
 ));%0A    
@@ -6140,32 +6140,32 @@
 owable error) %7B%0A
+
                 
@@ -6216,15 +6216,8 @@
 ror(
-context
 ) + 

Remove context