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.MinStockDao;
import in.techware.carrefour.dbUtils.entity.MinStockEntity;
import in.techware.carrefour.model.MinStockBean;

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

    private static MinStockRepository instance;


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

    public void insertMinStock(MinStockBean minStockBean, MinStockRepository.MinStockRepositoryListener listener) {
        MinStockRepository.InsertMinStockTask insertMinStockTask = new MinStockRepository.InsertMinStockTask(minStockBean, listener);
        insertMinStockTask.execute();

    }

    public void updateMinStock(MinStockBean minStockBean, MinStockRepository.MinStockRepositoryListener listener) {
        MinStockRepository.UpdateMinStockTask updateMinStockTask = new MinStockRepository.UpdateMinStockTask(minStockBean, listener);
        updateMinStockTask.execute();

    }

    public void deleteMinStock(MinStockBean minStockBean, MinStockRepository.MinStockRepositoryListener listener) {
        MinStockRepository.DeleteMinStockTask deleteMinStockTask = new MinStockRepository.DeleteMinStockTask(minStockBean, listener);
        deleteMinStockTask.execute();

    }

    public void getAllMinStocks(MinStockRepository.MinStockRepositoryListener listener) {
        MinStockRepository.GetAllMinStockTask getAllMinStockTask = new MinStockRepository.GetAllMinStockTask(listener);
        getAllMinStockTask.execute();

    }

    public void getAllUnsentMinStocks(MinStockRepository.MinStockRepositoryListener listener) {
        MinStockRepository.GetAllUnsentMinStockTask getAllUnsentMinStockTask = new MinStockRepository.GetAllUnsentMinStockTask(listener);
        getAllUnsentMinStockTask.execute();

    }

    public void getAllSentMinStocks(MinStockRepository.MinStockRepositoryListener listener) {
        MinStockRepository.GetAllSentMinStockTask getAllSentMinStockTask = new MinStockRepository.GetAllSentMinStockTask(listener);
        getAllSentMinStockTask.execute();

    }

    public void getMinStock(String minStockID, MinStockRepository.MinStockRepositoryListener listener) {
        MinStockRepository.GetMinStockTask getMinStockTask = new MinStockRepository.GetMinStockTask(minStockID, listener);
        getMinStockTask.execute();

    }

    public void deleteAllMinStocks(MinStockRepository.MinStockRepositoryListener listener) {
        MinStockRepository.DeleteAllMinStockTask deleteAllMinStockTask = new MinStockRepository.DeleteAllMinStockTask(listener);
        deleteAllMinStockTask.execute();

    }

    public void deleteAllUnsentMinStocks(MinStockRepository.MinStockRepositoryListener listener) {
        MinStockRepository.DeleteAllUnsentMinStockTask deleteAllUnsentMinStockTask = new MinStockRepository.DeleteAllUnsentMinStockTask(listener);
        deleteAllUnsentMinStockTask.execute();

    }

    public void deleteAllSentMinStocks(MinStockRepository.MinStockRepositoryListener listener) {
        MinStockRepository.DeleteAllSentMinStockTask deleteAllSentMinStockTask = new MinStockRepository.DeleteAllSentMinStockTask(listener);
        deleteAllSentMinStockTask.execute();

    }

    private static class InsertMinStockTask extends AsyncTask<String, Integer, Long> {
        private final MinStockBean minStockBean;
        private final MinStockRepository.MinStockRepositoryListener listener;

        public InsertMinStockTask(MinStockBean minStockBean, MinStockRepository.MinStockRepositoryListener listener) {
            super();
            this.minStockBean = minStockBean;
            this.listener = listener;
        }

        @Override
        protected Long doInBackground(String... params) {
            //System.out.println(">>>>>>>>>doInBackground");
            MinStockEntity minStockEntity = new MinStockEntity(minStockBean);
            MinStockDao minStockDao = CarrefourRoomDatabase.getDatabase().minStockDao();
            long id = minStockDao.insert(minStockEntity);
            return id;
        }

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

    private static class UpdateMinStockTask extends AsyncTask<String, Integer, Boolean> {
        private final MinStockBean minStockBean;
        private final MinStockRepository.MinStockRepositoryListener listener;

        public UpdateMinStockTask(MinStockBean minStockBean, MinStockRepository.MinStockRepositoryListener listener) {
            super();
            this.minStockBean = minStockBean;
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            MinStockEntity minStockEntity = new MinStockEntity(minStockBean);
            MinStockDao minStockDao = CarrefourRoomDatabase.getDatabase().minStockDao();
            minStockDao.update(minStockEntity);
            return true;
        }

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

    private static class DeleteMinStockTask extends AsyncTask<String, Integer, Boolean> {
        private final MinStockBean minStockBean;
        private final MinStockRepository.MinStockRepositoryListener listener;

        public DeleteMinStockTask(MinStockBean minStockBean, MinStockRepository.MinStockRepositoryListener listener) {
            super();
            this.minStockBean = minStockBean;
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            MinStockEntity minStockEntity = new MinStockEntity(minStockBean);
            MinStockDao minStockDao = CarrefourRoomDatabase.getDatabase().minStockDao();
            minStockDao.delete(minStockEntity);
            return true;
        }

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

    private static class GetAllMinStockTask extends AsyncTask<String, Integer, ArrayList<MinStockBean>> {
        private final MinStockRepository.MinStockRepositoryListener listener;

        public GetAllMinStockTask(MinStockRepository.MinStockRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected ArrayList<MinStockBean> doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            MinStockDao minStockDao = CarrefourRoomDatabase.getDatabase().minStockDao();
            List<MinStockEntity> minStockEntities = minStockDao.loadAllMinStocks();
            ArrayList<MinStockBean> minStockList = new ArrayList<>();
            for (MinStockEntity phoneMinStockEntity : minStockEntities) {
                MinStockBean minStockBean = phoneMinStockEntity.getBean();
                minStockList.add(minStockBean);
            }
            return minStockList;
        }

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

    private static class GetAllUnsentMinStockTask extends AsyncTask<String, Integer, ArrayList<MinStockBean>> {
        private final MinStockRepository.MinStockRepositoryListener listener;

        public GetAllUnsentMinStockTask(MinStockRepository.MinStockRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected ArrayList<MinStockBean> doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            MinStockDao minStockDao = CarrefourRoomDatabase.getDatabase().minStockDao();
            List<MinStockEntity> minStockEntities = minStockDao.getAllUnsentMinStock();
            ArrayList<MinStockBean> minStockList = new ArrayList<>();
            for (MinStockEntity phoneMinStockEntity : minStockEntities) {
                MinStockBean minStockBean = phoneMinStockEntity.getBean();
                minStockList.add(minStockBean);
            }
            return minStockList;
        }

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

    private static class GetAllSentMinStockTask extends AsyncTask<String, Integer, ArrayList<MinStockBean>> {
        private final MinStockRepository.MinStockRepositoryListener listener;

        public GetAllSentMinStockTask(MinStockRepository.MinStockRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected ArrayList<MinStockBean> doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            MinStockDao minStockDao = CarrefourRoomDatabase.getDatabase().minStockDao();
            List<MinStockEntity> minStockEntities = minStockDao.getAllSentMinStock();
            ArrayList<MinStockBean> minStockList = new ArrayList<>();
            for (MinStockEntity phoneMinStockEntity : minStockEntities) {
                MinStockBean minStockBean = phoneMinStockEntity.getBean();
                minStockList.add(minStockBean);
            }
            return minStockList;
        }

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

    private static class GetMinStockTask extends AsyncTask<String, Integer, MinStockBean> {
        private final String minStockID;
        private final MinStockRepository.MinStockRepositoryListener listener;

        public GetMinStockTask(String minStockID, MinStockRepository.MinStockRepositoryListener listener) {
            super();
            this.minStockID = minStockID;
            this.listener = listener;
        }

        @Override
        protected MinStockBean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            MinStockDao minStockDao = CarrefourRoomDatabase.getDatabase().minStockDao();
            List<MinStockEntity> minStockEntities = minStockDao.getMinStock(minStockID);
            if (minStockEntities != null && !minStockEntities.isEmpty()) {
                return minStockEntities.get(0).getBean();
            } else {
                return null;
            }
        }

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

    private static class DeleteAllMinStockTask extends AsyncTask<String, Integer, Boolean> {
        private final MinStockRepository.MinStockRepositoryListener listener;

        public DeleteAllMinStockTask(MinStockRepository.MinStockRepositoryListener listener) {
            super();
            this.listener = listener;
        }

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

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

    private static class DeleteAllUnsentMinStockTask extends AsyncTask<String, Integer, Boolean> {
        private final MinStockRepository.MinStockRepositoryListener listener;

        public DeleteAllUnsentMinStockTask(MinStockRepository.MinStockRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            MinStockDao minStockDao = CarrefourRoomDatabase.getDatabase().minStockDao();
            minStockDao.deleteAllUnsentMinStock();
            return true;
        }

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

    private static class DeleteAllSentMinStockTask extends AsyncTask<String, Integer, Boolean> {
        private final MinStockRepository.MinStockRepositoryListener listener;

        public DeleteAllSentMinStockTask(MinStockRepository.MinStockRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            MinStockDao minStockDao = CarrefourRoomDatabase.getDatabase().minStockDao();
            minStockDao.deleteAllSentMinStock();
            return true;
        }

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


    public static interface MinStockRepositoryListener {


        void onAddCompleted(MinStockBean minStockBean);

        void onAllListLoaded(ArrayList<MinStockBean> minStockList);

        void onAllSentListLoaded(ArrayList<MinStockBean> minStockList);

        void onAllUnsentListLoaded(ArrayList<MinStockBean> minStockList);

        void onDeleteAllCompleted();

        void onDeleteAllSentCompleted();

        void onDeleteAllUnsentCompleted();

        void onDeleteCompleted(MinStockBean minStockBean);

        void onFailed();

        void onUpdateCompleted(MinStockBean minStockBean);

        void onMinStockBeanLoaded(MinStockBean minStockBean);

    }

}
