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.ItemListDao;
import in.techware.carrefour.dbUtils.entity.ItemListEntity;
import in.techware.carrefour.model.ItemListBean;

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

    private static ItemListRepository instance;


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

    public void insertItemList(ItemListBean itemListBean, ItemListRepository.ItemListRepositoryListener listener) {
        ItemListRepository.InsertItemListTask insertItemListTask = new ItemListRepository.InsertItemListTask(itemListBean, listener);
        insertItemListTask.execute();

    }

    public void updateItemList(ItemListBean itemListBean, ItemListRepository.ItemListRepositoryListener listener) {
        ItemListRepository.UpdateItemListTask updateItemListTask = new ItemListRepository.UpdateItemListTask(itemListBean, listener);
        updateItemListTask.execute();

    }

    public void deleteItemList(ItemListBean itemListBean, ItemListRepository.ItemListRepositoryListener listener) {
        ItemListRepository.DeleteItemListTask deleteItemListTask = new ItemListRepository.DeleteItemListTask(itemListBean, listener);
        deleteItemListTask.execute();

    }

    public void getAllItemLists(ItemListRepository.ItemListRepositoryListener listener) {
        ItemListRepository.GetAllItemListTask getAllItemListTask = new ItemListRepository.GetAllItemListTask(listener);
        getAllItemListTask.execute();

    }

    public void getAllUnsentItemLists(ItemListRepository.ItemListRepositoryListener listener) {
        ItemListRepository.GetAllUnsentItemListTask getAllUnsentItemListTask = new ItemListRepository.GetAllUnsentItemListTask(listener);
        getAllUnsentItemListTask.execute();

    }

    public void getAllSentItemLists(ItemListRepository.ItemListRepositoryListener listener) {
        ItemListRepository.GetAllSentItemListTask getAllSentItemListTask = new ItemListRepository.GetAllSentItemListTask(listener);
        getAllSentItemListTask.execute();

    }

    public void getItemList(String itemListID, ItemListRepository.ItemListRepositoryListener listener) {
        ItemListRepository.GetItemListTask getItemListTask = new ItemListRepository.GetItemListTask(itemListID, listener);
        getItemListTask.execute();

    }

    public void deleteAllItemLists(ItemListRepository.ItemListRepositoryListener listener) {
        ItemListRepository.DeleteAllItemListTask deleteAllItemListTask = new ItemListRepository.DeleteAllItemListTask(listener);
        deleteAllItemListTask.execute();

    }

    public void deleteAllUnsentItemLists(ItemListRepository.ItemListRepositoryListener listener) {
        ItemListRepository.DeleteAllUnsentItemListTask deleteAllUnsentItemListTask = new ItemListRepository.DeleteAllUnsentItemListTask(listener);
        deleteAllUnsentItemListTask.execute();

    }

    public void deleteAllSentItemLists(ItemListRepository.ItemListRepositoryListener listener) {
        ItemListRepository.DeleteAllSentItemListTask deleteAllSentItemListTask = new ItemListRepository.DeleteAllSentItemListTask(listener);
        deleteAllSentItemListTask.execute();

    }

    private static class InsertItemListTask extends AsyncTask<String, Integer, Long> {
        private final ItemListBean itemListBean;
        private final ItemListRepository.ItemListRepositoryListener listener;

        public InsertItemListTask(ItemListBean itemListBean, ItemListRepository.ItemListRepositoryListener listener) {
            super();
            this.itemListBean = itemListBean;
            this.listener = listener;
        }

        @Override
        protected Long doInBackground(String... params) {
            //System.out.println(">>>>>>>>>doInBackground");
            ItemListEntity itemListEntity = new ItemListEntity(itemListBean);
            ItemListDao itemListDao = CarrefourRoomDatabase.getDatabase().itemListDao();
            long id = itemListDao.insert(itemListEntity);
            return id;
        }

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

    private static class UpdateItemListTask extends AsyncTask<String, Integer, Boolean> {
        private final ItemListBean itemListBean;
        private final ItemListRepository.ItemListRepositoryListener listener;

        public UpdateItemListTask(ItemListBean itemListBean, ItemListRepository.ItemListRepositoryListener listener) {
            super();
            this.itemListBean = itemListBean;
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            ItemListEntity itemListEntity = new ItemListEntity(itemListBean);
            ItemListDao itemListDao = CarrefourRoomDatabase.getDatabase().itemListDao();
            itemListDao.update(itemListEntity);
            return true;
        }

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

    private static class DeleteItemListTask extends AsyncTask<String, Integer, Boolean> {
        private final ItemListBean itemListBean;
        private final ItemListRepository.ItemListRepositoryListener listener;

        public DeleteItemListTask(ItemListBean itemListBean, ItemListRepository.ItemListRepositoryListener listener) {
            super();
            this.itemListBean = itemListBean;
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            ItemListEntity itemListEntity = new ItemListEntity(itemListBean);
            ItemListDao itemListDao = CarrefourRoomDatabase.getDatabase().itemListDao();
            itemListDao.delete(itemListEntity);
            return true;
        }

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

    private static class GetAllItemListTask extends AsyncTask<String, Integer, ArrayList<ItemListBean>> {
        private final ItemListRepository.ItemListRepositoryListener listener;

        public GetAllItemListTask(ItemListRepository.ItemListRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected ArrayList<ItemListBean> doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            ItemListDao itemListDao = CarrefourRoomDatabase.getDatabase().itemListDao();
            List<ItemListEntity> itemListEntities = itemListDao.loadAllItemLists();
            ArrayList<ItemListBean> itemListList = new ArrayList<>();
            for (ItemListEntity phoneItemListEntity : itemListEntities) {
                ItemListBean itemListBean = phoneItemListEntity.getBean();
                itemListList.add(itemListBean);
            }
            return itemListList;
        }

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

    private static class GetAllUnsentItemListTask extends AsyncTask<String, Integer, ArrayList<ItemListBean>> {
        private final ItemListRepository.ItemListRepositoryListener listener;

        public GetAllUnsentItemListTask(ItemListRepository.ItemListRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected ArrayList<ItemListBean> doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            ItemListDao itemListDao = CarrefourRoomDatabase.getDatabase().itemListDao();
            List<ItemListEntity> itemListEntities = itemListDao.getAllUnsentItemList();
            ArrayList<ItemListBean> itemListList = new ArrayList<>();
            for (ItemListEntity phoneItemListEntity : itemListEntities) {
                ItemListBean itemListBean = phoneItemListEntity.getBean();
                itemListList.add(itemListBean);
            }
            return itemListList;
        }

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

    private static class GetAllSentItemListTask extends AsyncTask<String, Integer, ArrayList<ItemListBean>> {
        private final ItemListRepository.ItemListRepositoryListener listener;

        public GetAllSentItemListTask(ItemListRepository.ItemListRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected ArrayList<ItemListBean> doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            ItemListDao itemListDao = CarrefourRoomDatabase.getDatabase().itemListDao();
            List<ItemListEntity> itemListEntities = itemListDao.getAllSentItemList();
            ArrayList<ItemListBean> itemListList = new ArrayList<>();
            for (ItemListEntity phoneItemListEntity : itemListEntities) {
                ItemListBean itemListBean = phoneItemListEntity.getBean();
                itemListList.add(itemListBean);
            }
            return itemListList;
        }

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

    private static class GetItemListTask extends AsyncTask<String, Integer, ItemListBean> {
        private final String itemListID;
        private final ItemListRepository.ItemListRepositoryListener listener;

        public GetItemListTask(String itemListID, ItemListRepository.ItemListRepositoryListener listener) {
            super();
            this.itemListID = itemListID;
            this.listener = listener;
        }

        @Override
        protected ItemListBean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            ItemListDao itemListDao = CarrefourRoomDatabase.getDatabase().itemListDao();
            List<ItemListEntity> itemListEntities = itemListDao.getItemList(itemListID);
            if (itemListEntities != null && !itemListEntities.isEmpty()) {
                return itemListEntities.get(0).getBean();
            } else {
                return null;
            }
        }

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

    private static class DeleteAllItemListTask extends AsyncTask<String, Integer, Boolean> {
        private final ItemListRepository.ItemListRepositoryListener listener;

        public DeleteAllItemListTask(ItemListRepository.ItemListRepositoryListener listener) {
            super();
            this.listener = listener;
        }

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

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

    private static class DeleteAllUnsentItemListTask extends AsyncTask<String, Integer, Boolean> {
        private final ItemListRepository.ItemListRepositoryListener listener;

        public DeleteAllUnsentItemListTask(ItemListRepository.ItemListRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            ItemListDao itemListDao = CarrefourRoomDatabase.getDatabase().itemListDao();
            itemListDao.deleteAllUnsentItemList();
            return true;
        }

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

    private static class DeleteAllSentItemListTask extends AsyncTask<String, Integer, Boolean> {
        private final ItemListRepository.ItemListRepositoryListener listener;

        public DeleteAllSentItemListTask(ItemListRepository.ItemListRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            ItemListDao itemListDao = CarrefourRoomDatabase.getDatabase().itemListDao();
            itemListDao.deleteAllSentItemList();
            return true;
        }

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


    public static interface ItemListRepositoryListener {


        void onAddCompleted(ItemListBean itemListBean);

        void onAllListLoaded(ArrayList<ItemListBean> itemListList);

        void onAllSentListLoaded(ArrayList<ItemListBean> itemListList);

        void onAllUnsentListLoaded(ArrayList<ItemListBean> itemListList);

        void onDeleteAllCompleted();

        void onDeleteAllSentCompleted();

        void onDeleteAllUnsentCompleted();

        void onDeleteCompleted(ItemListBean itemListBean);

        void onFailed();

        void onUpdateCompleted(ItemListBean itemListBean);

        void onItemListBeanLoaded(ItemListBean itemListBean);

    }

}
