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.PromotionDao;
import in.techware.carrefour.dbUtils.entity.PromotionEntity;
import in.techware.carrefour.model.PromotionBean;

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

    private static PromotionRepository instance;


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

    public void insertPromotion(PromotionBean promotionBean, PromotionRepositoryListener listener) {
        InsertPromotionTask insertPromotionTask = new InsertPromotionTask(promotionBean, listener);
        insertPromotionTask.execute();

    }

    public void updatePromotion(PromotionBean promotionBean, PromotionRepositoryListener listener) {
        UpdatePromotionTask updatePromotionTask = new UpdatePromotionTask(promotionBean, listener);
        updatePromotionTask.execute();

    }

    public void deletePromotion(PromotionBean promotionBean, PromotionRepositoryListener listener) {
        DeletePromotionTask deletePromotionTask = new DeletePromotionTask(promotionBean, listener);
        deletePromotionTask.execute();

    }

    public void getAllPromotions(PromotionRepositoryListener listener) {
        GetAllPromotionTask getAllPromotionTask = new GetAllPromotionTask(listener);
        getAllPromotionTask.execute();

    }

    public void getAllUnsentPromotions(PromotionRepositoryListener listener) {
        GetAllUnsentPromotionTask getAllUnsentPromotionTask = new GetAllUnsentPromotionTask(listener);
        getAllUnsentPromotionTask.execute();

    }

    public void getAllSentPromotions(PromotionRepositoryListener listener) {
        GetAllSentPromotionTask getAllSentPromotionTask = new GetAllSentPromotionTask(listener);
        getAllSentPromotionTask.execute();

    }

    public void getPromotion(String promotionID, PromotionRepositoryListener listener) {
        GetPromotionTask getPromotionTask = new GetPromotionTask(promotionID, listener);
        getPromotionTask.execute();

    }

    public void deleteAllPromotions(PromotionRepositoryListener listener) {
        DeleteAllPromotionTask deleteAllPromotionTask = new DeleteAllPromotionTask(listener);
        deleteAllPromotionTask.execute();

    }

    public void deleteAllUnsentPromotions(PromotionRepositoryListener listener) {
        DeleteAllUnsentPromotionTask deleteAllUnsentPromotionTask = new DeleteAllUnsentPromotionTask(listener);
        deleteAllUnsentPromotionTask.execute();

    }

    public void deleteAllSentPromotions(PromotionRepositoryListener listener) {
        DeleteAllSentPromotionTask deleteAllSentPromotionTask = new DeleteAllSentPromotionTask(listener);
        deleteAllSentPromotionTask.execute();

    }

    private static class InsertPromotionTask extends AsyncTask<String, Integer, Long> {
        private final PromotionBean promotionBean;
        private final PromotionRepositoryListener listener;

        public InsertPromotionTask(PromotionBean promotionBean, PromotionRepositoryListener listener) {
            super();
            this.promotionBean = promotionBean;
            this.listener = listener;
        }

        @Override
        protected Long doInBackground(String... params) {
            //System.out.println(">>>>>>>>>doInBackground");
            PromotionEntity promotionEntity = new PromotionEntity(promotionBean);
            PromotionDao promotionDao = CarrefourRoomDatabase.getDatabase().promotionDao();
            long id = promotionDao.insert(promotionEntity);
            return id;
        }

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

    private static class UpdatePromotionTask extends AsyncTask<String, Integer, Boolean> {
        private final PromotionBean promotionBean;
        private final PromotionRepositoryListener listener;

        public UpdatePromotionTask(PromotionBean promotionBean, PromotionRepositoryListener listener) {
            super();
            this.promotionBean = promotionBean;
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            PromotionEntity promotionEntity = new PromotionEntity(promotionBean);
            PromotionDao promotionDao = CarrefourRoomDatabase.getDatabase().promotionDao();
            promotionDao.update(promotionEntity);
            return true;
        }

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

    private static class DeletePromotionTask extends AsyncTask<String, Integer, Boolean> {
        private final PromotionBean promotionBean;
        private final PromotionRepositoryListener listener;

        public DeletePromotionTask(PromotionBean promotionBean, PromotionRepositoryListener listener) {
            super();
            this.promotionBean = promotionBean;
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            PromotionEntity promotionEntity = new PromotionEntity(promotionBean);
            PromotionDao promotionDao = CarrefourRoomDatabase.getDatabase().promotionDao();
            promotionDao.delete(promotionEntity);
            return true;
        }

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

    private static class GetAllPromotionTask extends AsyncTask<String, Integer, ArrayList<PromotionBean>> {
        private final PromotionRepositoryListener listener;

        public GetAllPromotionTask(PromotionRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected ArrayList<PromotionBean> doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            PromotionDao promotionDao = CarrefourRoomDatabase.getDatabase().promotionDao();
            List<PromotionEntity> promotionEntities = promotionDao.loadAllPromotions();
            ArrayList<PromotionBean> promotionList = new ArrayList<>();
            for (PromotionEntity phonePromotionEntity : promotionEntities) {
                PromotionBean promotionBean = phonePromotionEntity.getBean();
                promotionList.add(promotionBean);
            }
            return promotionList;
        }

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

    private static class GetAllUnsentPromotionTask extends AsyncTask<String, Integer, ArrayList<PromotionBean>> {
        private final PromotionRepositoryListener listener;

        public GetAllUnsentPromotionTask(PromotionRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected ArrayList<PromotionBean> doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            PromotionDao promotionDao = CarrefourRoomDatabase.getDatabase().promotionDao();
            List<PromotionEntity> promotionEntities = promotionDao.getAllUnsentPromotion();
            ArrayList<PromotionBean> promotionList = new ArrayList<>();
            for (PromotionEntity phonePromotionEntity : promotionEntities) {
                PromotionBean promotionBean = phonePromotionEntity.getBean();
                promotionList.add(promotionBean);
            }
            return promotionList;
        }

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

    private static class GetAllSentPromotionTask extends AsyncTask<String, Integer, ArrayList<PromotionBean>> {
        private final PromotionRepositoryListener listener;

        public GetAllSentPromotionTask(PromotionRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected ArrayList<PromotionBean> doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            PromotionDao promotionDao = CarrefourRoomDatabase.getDatabase().promotionDao();
            List<PromotionEntity> promotionEntities = promotionDao.getAllSentPromotion();
            ArrayList<PromotionBean> promotionList = new ArrayList<>();
            for (PromotionEntity phonePromotionEntity : promotionEntities) {
                PromotionBean promotionBean = phonePromotionEntity.getBean();
                promotionList.add(promotionBean);
            }
            return promotionList;
        }

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

    private static class GetPromotionTask extends AsyncTask<String, Integer, PromotionBean> {
        private final String promotionID;
        private final PromotionRepositoryListener listener;

        public GetPromotionTask(String promotionID, PromotionRepositoryListener listener) {
            super();
            this.promotionID = promotionID;
            this.listener = listener;
        }

        @Override
        protected PromotionBean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            PromotionDao promotionDao = CarrefourRoomDatabase.getDatabase().promotionDao();
            List<PromotionEntity> promotionEntities = promotionDao.getPromotion(promotionID);
            if (promotionEntities != null && !promotionEntities.isEmpty()) {
                return promotionEntities.get(0).getBean();
            } else {
                return null;
            }
        }

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

    private static class DeleteAllPromotionTask extends AsyncTask<String, Integer, Boolean> {
        private final PromotionRepositoryListener listener;

        public DeleteAllPromotionTask(PromotionRepositoryListener listener) {
            super();
            this.listener = listener;
        }

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

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

    private static class DeleteAllUnsentPromotionTask extends AsyncTask<String, Integer, Boolean> {
        private final PromotionRepositoryListener listener;

        public DeleteAllUnsentPromotionTask(PromotionRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            PromotionDao promotionDao = CarrefourRoomDatabase.getDatabase().promotionDao();
            promotionDao.deleteAllUnsentPromotion();
            return true;
        }

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

    private static class DeleteAllSentPromotionTask extends AsyncTask<String, Integer, Boolean> {
        private final PromotionRepositoryListener listener;

        public DeleteAllSentPromotionTask(PromotionRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            PromotionDao promotionDao = CarrefourRoomDatabase.getDatabase().promotionDao();
            promotionDao.deleteAllSentPromotion();
            return true;
        }

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


    public static interface PromotionRepositoryListener {


        void onAddCompleted(PromotionBean promotionBean);

        void onAllListLoaded(ArrayList<PromotionBean> promotionList);

        void onAllSentListLoaded(ArrayList<PromotionBean> promotionList);

        void onAllUnsentListLoaded(ArrayList<PromotionBean> promotionList);

        void onDeleteAllCompleted();

        void onDeleteAllSentCompleted();

        void onDeleteAllUnsentCompleted();

        void onDeleteCompleted(PromotionBean promotionBean);

        void onFailed();

        void onUpdateCompleted(PromotionBean promotionBean);

        void onPromotionBeanLoaded(PromotionBean promotionBean);

    }

}
