package in.techware.carrefour.dbUtils.repositories;

import android.os.AsyncTask;

import java.util.ArrayList;
import java.util.List;

import in.techware.carrefour.dbUtils.CarrefourRoomDatabase;
import in.techware.carrefour.dbUtils.dao.TransformQuantityDao;
import in.techware.carrefour.dbUtils.entity.TransformQuantityEntity;
import in.techware.carrefour.model.TransformQuantityBean;

/**
 * Created by Jemsheer K D on 25 July, 2018.
 * Package in.techware.carrefour.dbUtils.repositories
 * Project Carrefour
 */
public class TransformQuantityRepository {

    private static TransformQuantityRepository instance;


    public static TransformQuantityRepository getInstance() {
        if (instance == null) {
            synchronized (CarrefourRoomDatabase.class) {
                if (instance == null) {
                    instance = new TransformQuantityRepository();
                }
            }
        }
        return instance;
    }

    public void insertTransformQuantity(TransformQuantityBean transformQuantityBean, TransformQuantityRepositoryListener listener) {
        InsertTransformQuantityTask insertTransformQuantityTask = new InsertTransformQuantityTask(transformQuantityBean, listener);
        insertTransformQuantityTask.execute();

    }

    public void updateTransformQuantity(TransformQuantityBean transformQuantityBean, TransformQuantityRepositoryListener listener) {
        UpdateTransformQuantityTask updateTransformQuantityTask = new UpdateTransformQuantityTask(transformQuantityBean, listener);
        updateTransformQuantityTask.execute();

    }

    public void deleteTransformQuantity(TransformQuantityBean transformQuantityBean, TransformQuantityRepositoryListener listener) {
        DeleteTransformQuantityTask deleteTransformQuantityTask = new DeleteTransformQuantityTask(transformQuantityBean, listener);
        deleteTransformQuantityTask.execute();

    }

    public void getAllTransformQuantitys(TransformQuantityRepositoryListener listener) {
        GetAllTransformQuantityTask getAllTransformQuantityTask = new GetAllTransformQuantityTask(listener);
        getAllTransformQuantityTask.execute();

    }

    public void getAllUnsentTransformQuantities(TransformQuantityRepositoryListener listener) {
        GetAllUnsentTransformQuantityTask getAllUnsentTransformQuantityTask = new GetAllUnsentTransformQuantityTask(listener);
        getAllUnsentTransformQuantityTask.execute();

    }

    public void getAllSentTransformQuantities(TransformQuantityRepositoryListener listener) {
        GetAllSentTransformQuantityTask getAllSentTransformQuantityTask = new GetAllSentTransformQuantityTask(listener);
        getAllSentTransformQuantityTask.execute();

    }

    public void getTransformQuantity(String transformQuantityID, TransformQuantityRepositoryListener listener) {
        GetTransformQuantityTask getTransformQuantityTask = new GetTransformQuantityTask(transformQuantityID, listener);
        getTransformQuantityTask.execute();

    }

    public void deleteAllTransformQuantitys(TransformQuantityRepositoryListener listener) {
        DeleteAllTransformQuantityTask deleteAllTransformQuantityTask = new DeleteAllTransformQuantityTask(listener);
        deleteAllTransformQuantityTask.execute();

    }

    public void deleteAllUnsentTransformQuantitys(TransformQuantityRepositoryListener listener) {
        DeleteAllUnsentTransformQuantityTask deleteAllUnsentTransformQuantityTask = new DeleteAllUnsentTransformQuantityTask(listener);
        deleteAllUnsentTransformQuantityTask.execute();

    }

    public void deleteAllSentTransformQuantitys(TransformQuantityRepositoryListener listener) {
        DeleteAllSentTransformQuantityTask deleteAllSentTransformQuantityTask = new DeleteAllSentTransformQuantityTask(listener);
        deleteAllSentTransformQuantityTask.execute();

    }

    private static class InsertTransformQuantityTask extends AsyncTask<String, Integer, Long> {
        private final TransformQuantityBean transformQuantityBean;
        private final TransformQuantityRepositoryListener listener;

        public InsertTransformQuantityTask(TransformQuantityBean transformQuantityBean, TransformQuantityRepositoryListener listener) {
            super();
            this.transformQuantityBean = transformQuantityBean;
            this.listener = listener;
        }

        @Override
        protected Long doInBackground(String... params) {
            //System.out.println(">>>>>>>>>doInBackground");
            TransformQuantityEntity transformQuantityEntity = new TransformQuantityEntity(transformQuantityBean);
            TransformQuantityDao transformQuantityDao = CarrefourRoomDatabase.getDatabase().transformQuantityDao();
            long id = transformQuantityDao.insert(transformQuantityEntity);
            return id;
        }

        @Override
        protected void onPostExecute(Long id) {
            super.onPostExecute(id);
            if (id != -1) {
                listener.onAddCompleted(transformQuantityBean);
            } else
                listener.onFailed();
        }
    }

    private static class UpdateTransformQuantityTask extends AsyncTask<String, Integer, Boolean> {
        private final TransformQuantityBean transformQuantityBean;
        private final TransformQuantityRepositoryListener listener;

        public UpdateTransformQuantityTask(TransformQuantityBean transformQuantityBean, TransformQuantityRepositoryListener listener) {
            super();
            this.transformQuantityBean = transformQuantityBean;
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            TransformQuantityEntity transformQuantityEntity = new TransformQuantityEntity(transformQuantityBean);
            TransformQuantityDao transformQuantityDao = CarrefourRoomDatabase.getDatabase().transformQuantityDao();
            transformQuantityDao.update(transformQuantityEntity);
            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            if (result)
                listener.onUpdateCompleted(transformQuantityBean);
            else
                listener.onFailed();
        }
    }

    private static class DeleteTransformQuantityTask extends AsyncTask<String, Integer, Boolean> {
        private final TransformQuantityBean transformQuantityBean;
        private final TransformQuantityRepositoryListener listener;

        public DeleteTransformQuantityTask(TransformQuantityBean transformQuantityBean, TransformQuantityRepositoryListener listener) {
            super();
            this.transformQuantityBean = transformQuantityBean;
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            TransformQuantityEntity transformQuantityEntity = new TransformQuantityEntity(transformQuantityBean);
            TransformQuantityDao transformQuantityDao = CarrefourRoomDatabase.getDatabase().transformQuantityDao();
            transformQuantityDao.delete(transformQuantityEntity);
            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            if (result)
                listener.onDeleteCompleted(transformQuantityBean);
            else
                listener.onFailed();
        }
    }

    private static class GetAllTransformQuantityTask extends AsyncTask<String, Integer, ArrayList<TransformQuantityBean>> {
        private final TransformQuantityRepositoryListener listener;

        public GetAllTransformQuantityTask(TransformQuantityRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected ArrayList<TransformQuantityBean> doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            TransformQuantityDao transformQuantityDao = CarrefourRoomDatabase.getDatabase().transformQuantityDao();
            List<TransformQuantityEntity> transformQuantityEntities = transformQuantityDao.loadAllTransformQuantitys();
            ArrayList<TransformQuantityBean> transformQuantityList = new ArrayList<>();
            for (TransformQuantityEntity phoneTransformQuantityEntity : transformQuantityEntities) {
                TransformQuantityBean transformQuantityBean = phoneTransformQuantityEntity.getBean();
                transformQuantityList.add(transformQuantityBean);
            }
            return transformQuantityList;
        }

        @Override
        protected void onPostExecute(ArrayList<TransformQuantityBean> result) {
            super.onPostExecute(result);
            if (result != null) {
                listener.onAllListLoaded(result);
            } else
                listener.onFailed();
        }
    }

    private static class GetAllUnsentTransformQuantityTask extends AsyncTask<String, Integer, ArrayList<TransformQuantityBean>> {
        private final TransformQuantityRepositoryListener listener;

        public GetAllUnsentTransformQuantityTask(TransformQuantityRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected ArrayList<TransformQuantityBean> doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            TransformQuantityDao transformQuantityDao = CarrefourRoomDatabase.getDatabase().transformQuantityDao();
            List<TransformQuantityEntity> transformQuantityEntities = transformQuantityDao.getAllUnsentTransformQuantity();
            ArrayList<TransformQuantityBean> transformQuantityList = new ArrayList<>();
            for (TransformQuantityEntity phoneTransformQuantityEntity : transformQuantityEntities) {
                TransformQuantityBean transformQuantityBean = phoneTransformQuantityEntity.getBean();
                transformQuantityList.add(transformQuantityBean);
            }
            return transformQuantityList;
        }

        @Override
        protected void onPostExecute(ArrayList<TransformQuantityBean> result) {
            super.onPostExecute(result);
            if (result != null) {
                listener.onAllUnsentListLoaded(result);
            } else
                listener.onFailed();
        }
    }

    private static class GetAllSentTransformQuantityTask extends AsyncTask<String, Integer, ArrayList<TransformQuantityBean>> {
        private final TransformQuantityRepositoryListener listener;

        public GetAllSentTransformQuantityTask(TransformQuantityRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected ArrayList<TransformQuantityBean> doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            TransformQuantityDao transformQuantityDao = CarrefourRoomDatabase.getDatabase().transformQuantityDao();
            List<TransformQuantityEntity> transformQuantityEntities = transformQuantityDao.getAllSentTransformQuantity();
            ArrayList<TransformQuantityBean> transformQuantityList = new ArrayList<>();
            for (TransformQuantityEntity phoneTransformQuantityEntity : transformQuantityEntities) {
                TransformQuantityBean transformQuantityBean = phoneTransformQuantityEntity.getBean();
                transformQuantityList.add(transformQuantityBean);
            }
            return transformQuantityList;
        }

        @Override
        protected void onPostExecute(ArrayList<TransformQuantityBean> result) {
            super.onPostExecute(result);
            if (result != null) {
                listener.onAllSentListLoaded(result);
            } else
                listener.onFailed();
        }
    }

    private static class GetTransformQuantityTask extends AsyncTask<String, Integer, TransformQuantityBean> {
        private final String transformQuantityID;
        private final TransformQuantityRepositoryListener listener;

        public GetTransformQuantityTask(String transformQuantityID, TransformQuantityRepositoryListener listener) {
            super();
            this.transformQuantityID = transformQuantityID;
            this.listener = listener;
        }

        @Override
        protected TransformQuantityBean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            TransformQuantityDao transformQuantityDao = CarrefourRoomDatabase.getDatabase().transformQuantityDao();
            List<TransformQuantityEntity> transformQuantityEntities = transformQuantityDao.getTransformQuantity(transformQuantityID);
            if (transformQuantityEntities != null && !transformQuantityEntities.isEmpty()) {
                return transformQuantityEntities.get(0).getBean();
            } else {
                return null;
            }
        }

        @Override
        protected void onPostExecute(TransformQuantityBean result) {
            super.onPostExecute(result);
            if (result != null)
                listener.onTransformQuantityBeanLoaded(result);
            else
                listener.onFailed();
        }
    }

    private static class DeleteAllTransformQuantityTask extends AsyncTask<String, Integer, Boolean> {
        private final TransformQuantityRepositoryListener listener;

        public DeleteAllTransformQuantityTask(TransformQuantityRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            TransformQuantityDao transformQuantityDao = CarrefourRoomDatabase.getDatabase().transformQuantityDao();
            transformQuantityDao.deleteAll();
            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            if (result) {
                listener.onDeleteAllCompleted();
            } else
                listener.onFailed();
        }
    }

    private static class DeleteAllUnsentTransformQuantityTask extends AsyncTask<String, Integer, Boolean> {
        private final TransformQuantityRepositoryListener listener;

        public DeleteAllUnsentTransformQuantityTask(TransformQuantityRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            TransformQuantityDao transformQuantityDao = CarrefourRoomDatabase.getDatabase().transformQuantityDao();
            transformQuantityDao.deleteAllUnsentTransformQuantity();
            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            if (result) {
                listener.onDeleteAllUnsentCompleted();
            } else
                listener.onFailed();
        }
    }

    private static class DeleteAllSentTransformQuantityTask extends AsyncTask<String, Integer, Boolean> {
        private final TransformQuantityRepositoryListener listener;

        public DeleteAllSentTransformQuantityTask(TransformQuantityRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            TransformQuantityDao transformQuantityDao = CarrefourRoomDatabase.getDatabase().transformQuantityDao();
            transformQuantityDao.deleteAllSentTransformQuantity();
            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            if (result) {
                listener.onDeleteAllSentCompleted();
            } else
                listener.onFailed();
        }
    }


    public static interface TransformQuantityRepositoryListener {


        void onAddCompleted(TransformQuantityBean transformQuantityBean);

        void onAllListLoaded(ArrayList<TransformQuantityBean> transformQuantityList);

        void onAllSentListLoaded(ArrayList<TransformQuantityBean> transformQuantityList);

        void onAllUnsentListLoaded(ArrayList<TransformQuantityBean> transformQuantityList);

        void onDeleteAllCompleted();

        void onDeleteAllSentCompleted();

        void onDeleteAllUnsentCompleted();

        void onDeleteCompleted(TransformQuantityBean transformQuantityBean);

        void onFailed();

        void onUpdateCompleted(TransformQuantityBean transformQuantityBean);

        void onTransformQuantityBeanLoaded(TransformQuantityBean transformQuantityBean);

    }

}
