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.InternalSalesCorrectionDao;
import in.techware.carrefour.dbUtils.entity.InternalSalesCorrectionEntity;
import in.techware.carrefour.model.InternalSalesCorrectionBean;

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

    private static InternalSalesCorrectionRepository instance;


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

    public void insertInternalSalesCorrection(InternalSalesCorrectionBean internalSalesCorrectionBean,
                                              InternalSalesCorrectionRepositoryListener listener) {
        InsertInternalSalesCorrectionTask insertInternalSalesCorrectionTask = 
                new InsertInternalSalesCorrectionTask(internalSalesCorrectionBean, listener);
        insertInternalSalesCorrectionTask.execute();

    }

    public void updateInternalSalesCorrection(InternalSalesCorrectionBean internalSalesCorrectionBean, 
                                              InternalSalesCorrectionRepositoryListener listener) {
        UpdateInternalSalesCorrectionTask updateInternalSalesCorrectionTask = 
                new UpdateInternalSalesCorrectionTask(internalSalesCorrectionBean, listener);
        updateInternalSalesCorrectionTask.execute();

    }

    public void deleteInternalSalesCorrection(InternalSalesCorrectionBean internalSalesCorrectionBean, 
                                              InternalSalesCorrectionRepositoryListener listener) {
        DeleteInternalSalesCorrectionTask deleteInternalSalesCorrectionTask = 
                new DeleteInternalSalesCorrectionTask(internalSalesCorrectionBean, listener);
        deleteInternalSalesCorrectionTask.execute();

    }

    public void getAllInternalSalesCorrection(InternalSalesCorrectionRepositoryListener listener) {
        GetAllInternalSalesCorrectionTask getAllInternalSalesCorrectionTask = 
                new GetAllInternalSalesCorrectionTask(listener);
        getAllInternalSalesCorrectionTask.execute();

    }

    public void getAllUnsentInternalSalesCorrection(InternalSalesCorrectionRepositoryListener listener) {
        GetAllUnsentInternalSalesCorrectionTask getAllUnsentInternalSalesCorrectionTask = 
                new GetAllUnsentInternalSalesCorrectionTask(listener);
        getAllUnsentInternalSalesCorrectionTask.execute();

    }

    public void getAllSentInternalSalesCorrection(InternalSalesCorrectionRepositoryListener listener) {
        GetAllSentInternalSalesCorrectionTask getAllSentInternalSalesCorrectionTask = 
                new GetAllSentInternalSalesCorrectionTask(listener);
        getAllSentInternalSalesCorrectionTask.execute();

    }

    public void getInternalSalesCorrection(String internalSalesCorrectionID, InternalSalesCorrectionRepositoryListener listener) {
        GetInternalSalesCorrectionTask getInternalSalesCorrectionTask =
                new GetInternalSalesCorrectionTask(internalSalesCorrectionID, listener);
        getInternalSalesCorrectionTask.execute();

    }

    public void deleteAllInternalSalesCorrection(InternalSalesCorrectionRepositoryListener listener) {
        DeleteAllInternalSalesCorrectionTask deleteAllInternalSalesCorrectionTask = 
                new DeleteAllInternalSalesCorrectionTask(listener);
        deleteAllInternalSalesCorrectionTask.execute();

    }

    public void deleteAllUnsentInternalSalesCorrection(InternalSalesCorrectionRepositoryListener listener) {
        DeleteAllUnsentInternalSalesCorrectionTask deleteAllUnsentInternalSalesCorrectionTask = 
                new DeleteAllUnsentInternalSalesCorrectionTask(listener);
        deleteAllUnsentInternalSalesCorrectionTask.execute();

    }

    public void deleteAllSentInternalSalesCorrection(InternalSalesCorrectionRepositoryListener listener) {
        DeleteAllSentInternalSalesCorrectionTask deleteAllSentInternalSalesCorrectionTask =
                new DeleteAllSentInternalSalesCorrectionTask(listener);
        deleteAllSentInternalSalesCorrectionTask.execute();

    }

    private static class InsertInternalSalesCorrectionTask extends AsyncTask<String, Integer, Long> {
        private final InternalSalesCorrectionBean internalSalesCorrectionBean;
        private final InternalSalesCorrectionRepositoryListener listener;

        public InsertInternalSalesCorrectionTask(InternalSalesCorrectionBean internalSalesCorrectionBean,
                                                 InternalSalesCorrectionRepositoryListener listener) {
            super();
            this.internalSalesCorrectionBean = internalSalesCorrectionBean;
            this.listener = listener;
        }

        @Override
        protected Long doInBackground(String... params) {
            //System.out.println(">>>>>>>>>doInBackground");
            InternalSalesCorrectionEntity internalSalesCorrectionEntity = new InternalSalesCorrectionEntity(internalSalesCorrectionBean);
            InternalSalesCorrectionDao internalSalesCorrectionDao = CarrefourRoomDatabase.getDatabase().internalSalesCorrectionDao();
            long id = internalSalesCorrectionDao.insert(internalSalesCorrectionEntity);
            return id;
        }

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

    private static class UpdateInternalSalesCorrectionTask extends AsyncTask<String, Integer, Boolean> {
        private final InternalSalesCorrectionBean internalSalesCorrectionBean;
        private final InternalSalesCorrectionRepositoryListener listener;

        public UpdateInternalSalesCorrectionTask(InternalSalesCorrectionBean internalSalesCorrectionBean,
                                                 InternalSalesCorrectionRepositoryListener listener) {
            super();
            this.internalSalesCorrectionBean = internalSalesCorrectionBean;
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            InternalSalesCorrectionEntity internalSalesCorrectionEntity = new InternalSalesCorrectionEntity(internalSalesCorrectionBean);
            InternalSalesCorrectionDao internalSalesCorrectionDao = CarrefourRoomDatabase.getDatabase().internalSalesCorrectionDao();
            internalSalesCorrectionDao.update(internalSalesCorrectionEntity);
            return true;
        }

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

    private static class DeleteInternalSalesCorrectionTask extends AsyncTask<String, Integer, Boolean> {
        private final InternalSalesCorrectionBean internalSalesCorrectionBean;
        private final InternalSalesCorrectionRepositoryListener listener;

        public DeleteInternalSalesCorrectionTask(InternalSalesCorrectionBean internalSalesCorrectionBean,
                                                 InternalSalesCorrectionRepositoryListener listener) {
            super();
            this.internalSalesCorrectionBean = internalSalesCorrectionBean;
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            InternalSalesCorrectionEntity internalSalesCorrectionEntity = new InternalSalesCorrectionEntity(internalSalesCorrectionBean);
            InternalSalesCorrectionDao internalSalesCorrectionDao = CarrefourRoomDatabase.getDatabase().internalSalesCorrectionDao();
            internalSalesCorrectionDao.delete(internalSalesCorrectionEntity);
            return true;
        }

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

    private static class GetAllInternalSalesCorrectionTask extends AsyncTask<String, Integer, ArrayList<InternalSalesCorrectionBean>> {
        private final InternalSalesCorrectionRepositoryListener listener;

        public GetAllInternalSalesCorrectionTask(InternalSalesCorrectionRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected ArrayList<InternalSalesCorrectionBean> doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            InternalSalesCorrectionDao internalSalesCorrectionDao = CarrefourRoomDatabase.getDatabase().internalSalesCorrectionDao();
            List<InternalSalesCorrectionEntity> internalSalesCorrectionEntities =
                    internalSalesCorrectionDao.loadAllInternalSalesCorrections();
            ArrayList<InternalSalesCorrectionBean> internalSalesCorrectionList = new ArrayList<>();
            for (InternalSalesCorrectionEntity phoneInternalSalesCorrectionEntity : internalSalesCorrectionEntities) {
                InternalSalesCorrectionBean internalSalesCorrectionBean = phoneInternalSalesCorrectionEntity.getBean();
                internalSalesCorrectionList.add(internalSalesCorrectionBean);
            }
            return internalSalesCorrectionList;
        }

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

    private static class GetAllUnsentInternalSalesCorrectionTask extends AsyncTask<String, Integer, ArrayList<InternalSalesCorrectionBean>> {
        private final InternalSalesCorrectionRepositoryListener listener;

        public GetAllUnsentInternalSalesCorrectionTask(InternalSalesCorrectionRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected ArrayList<InternalSalesCorrectionBean> doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            InternalSalesCorrectionDao internalSalesCorrectionDao = CarrefourRoomDatabase.getDatabase().internalSalesCorrectionDao();
            List<InternalSalesCorrectionEntity> internalSalesCorrectionEntities =
                    internalSalesCorrectionDao.getAllUnsentInternalSalesCorrection();
            ArrayList<InternalSalesCorrectionBean> internalSalesCorrectionList = new ArrayList<>();
            for (InternalSalesCorrectionEntity phoneInternalSalesCorrectionEntity : internalSalesCorrectionEntities) {
                InternalSalesCorrectionBean internalSalesCorrectionBean = phoneInternalSalesCorrectionEntity.getBean();
                internalSalesCorrectionList.add(internalSalesCorrectionBean);
            }
            return internalSalesCorrectionList;
        }

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

    private static class GetAllSentInternalSalesCorrectionTask extends AsyncTask<String, Integer, ArrayList<InternalSalesCorrectionBean>> {
        private final InternalSalesCorrectionRepositoryListener listener;

        public GetAllSentInternalSalesCorrectionTask(InternalSalesCorrectionRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected ArrayList<InternalSalesCorrectionBean> doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            InternalSalesCorrectionDao internalSalesCorrectionDao = CarrefourRoomDatabase.getDatabase().internalSalesCorrectionDao();
            List<InternalSalesCorrectionEntity> internalSalesCorrectionEntities =
                    internalSalesCorrectionDao.getAllSentInternalSalesCorrection();
            ArrayList<InternalSalesCorrectionBean> internalSalesCorrectionList = new ArrayList<>();
            for (InternalSalesCorrectionEntity phoneInternalSalesCorrectionEntity : internalSalesCorrectionEntities) {
                InternalSalesCorrectionBean internalSalesCorrectionBean = phoneInternalSalesCorrectionEntity.getBean();
                internalSalesCorrectionList.add(internalSalesCorrectionBean);
            }
            return internalSalesCorrectionList;
        }

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

    private static class GetInternalSalesCorrectionTask extends AsyncTask<String, Integer, InternalSalesCorrectionBean> {
        private final String internalSalesCorrectionID;
        private final InternalSalesCorrectionRepositoryListener listener;

        public GetInternalSalesCorrectionTask(String internalSalesCorrectionID, InternalSalesCorrectionRepositoryListener listener) {
            super();
            this.internalSalesCorrectionID = internalSalesCorrectionID;
            this.listener = listener;
        }

        @Override
        protected InternalSalesCorrectionBean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            InternalSalesCorrectionDao internalSalesCorrectionDao = CarrefourRoomDatabase.getDatabase().internalSalesCorrectionDao();
            List<InternalSalesCorrectionEntity> internalSalesCorrectionEntities =
                    internalSalesCorrectionDao.getInternalSalesCorrection(internalSalesCorrectionID);
            if (internalSalesCorrectionEntities != null && !internalSalesCorrectionEntities.isEmpty()) {
                return internalSalesCorrectionEntities.get(0).getBean();
            } else {
                return null;
            }
        }

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

    private static class DeleteAllInternalSalesCorrectionTask extends AsyncTask<String, Integer, Boolean> {
        private final InternalSalesCorrectionRepositoryListener listener;

        public DeleteAllInternalSalesCorrectionTask(InternalSalesCorrectionRepositoryListener listener) {
            super();
            this.listener = listener;
        }

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

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

    private static class DeleteAllUnsentInternalSalesCorrectionTask extends AsyncTask<String, Integer, Boolean> {
        private final InternalSalesCorrectionRepositoryListener listener;

        public DeleteAllUnsentInternalSalesCorrectionTask(InternalSalesCorrectionRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            InternalSalesCorrectionDao internalSalesCorrectionDao = CarrefourRoomDatabase.getDatabase().internalSalesCorrectionDao();
            internalSalesCorrectionDao.deleteAllUnsentInternalSalesCorrection();
            return true;
        }

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

    private static class DeleteAllSentInternalSalesCorrectionTask extends AsyncTask<String, Integer, Boolean> {
        private final InternalSalesCorrectionRepositoryListener listener;

        public DeleteAllSentInternalSalesCorrectionTask(InternalSalesCorrectionRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            InternalSalesCorrectionDao internalSalesCorrectionDao = CarrefourRoomDatabase.getDatabase().internalSalesCorrectionDao();
            internalSalesCorrectionDao.deleteAllSentInternalSalesCorrection();
            return true;
        }

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


    public static interface InternalSalesCorrectionRepositoryListener {


        void onAddCompleted(InternalSalesCorrectionBean internalSalesCorrectionBean);

        void onAllListLoaded(ArrayList<InternalSalesCorrectionBean> internalSalesCorrectionList);

        void onAllSentListLoaded(ArrayList<InternalSalesCorrectionBean> internalSalesCorrectionList);

        void onAllUnsentListLoaded(ArrayList<InternalSalesCorrectionBean> internalSalesCorrectionList);

        void onDeleteAllCompleted();

        void onDeleteAllSentCompleted();

        void onDeleteAllUnsentCompleted();

        void onDeleteCompleted(InternalSalesCorrectionBean internalSalesCorrectionBean);

        void onFailed();

        void onUpdateCompleted(InternalSalesCorrectionBean internalSalesCorrectionBean);

        void onInternalSalesCorrectionBeanLoaded(InternalSalesCorrectionBean internalSalesCorrectionBean);

    }

}
