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.LabelSurveyDao;
import in.techware.carrefour.dbUtils.entity.LabelSurveyEntity;
import in.techware.carrefour.model.LabelSurveyBean;

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

    private static LabelSurveyRepository instance;


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

    public void insertLabelSurvey(LabelSurveyBean labelSurveyBean, LabelSurveyRepositoryListener listener) {
        InsertLabelSurveyTask insertLabelSurveyTask = new InsertLabelSurveyTask(labelSurveyBean, listener);
        insertLabelSurveyTask.execute();

    }

    public void updateLabelSurvey(LabelSurveyBean labelSurveyBean, LabelSurveyRepositoryListener listener) {
        UpdateLabelSurveyTask updateLabelSurveyTask = new UpdateLabelSurveyTask(labelSurveyBean, listener);
        updateLabelSurveyTask.execute();

    }

    public void deleteLabelSurvey(LabelSurveyBean labelSurveyBean, LabelSurveyRepositoryListener listener) {
        DeleteLabelSurveyTask deleteLabelSurveyTask = new DeleteLabelSurveyTask(labelSurveyBean, listener);
        deleteLabelSurveyTask.execute();

    }

    public void getAllLabelSurveys(LabelSurveyRepositoryListener listener) {
        GetAllLabelSurveyTask getAllLabelSurveyTask = new GetAllLabelSurveyTask(listener);
        getAllLabelSurveyTask.execute();

    }

    public void getAllUnsentLabelSurveys(LabelSurveyRepositoryListener listener) {
        GetAllUnsentLabelSurveyTask getAllUnsentLabelSurveyTask = new GetAllUnsentLabelSurveyTask(listener);
        getAllUnsentLabelSurveyTask.execute();

    }

    public void getAllSentLabelSurveys(LabelSurveyRepositoryListener listener) {
        GetAllSentLabelSurveyTask getAllSentLabelSurveyTask = new GetAllSentLabelSurveyTask(listener);
        getAllSentLabelSurveyTask.execute();

    }

    public void getLabelSurvey(String labelSurveyID, LabelSurveyRepositoryListener listener) {
        GetLabelSurveyTask getLabelSurveyTask = new GetLabelSurveyTask(labelSurveyID, listener);
        getLabelSurveyTask.execute();

    }

    public void deleteAllLabelSurveys(LabelSurveyRepositoryListener listener) {
        DeleteAllLabelSurveyTask deleteAllLabelSurveyTask = new DeleteAllLabelSurveyTask(listener);
        deleteAllLabelSurveyTask.execute();

    }

    public void deleteAllUnsentLabelSurveys(LabelSurveyRepositoryListener listener) {
        DeleteAllUnsentLabelSurveyTask deleteAllUnsentLabelSurveyTask = new DeleteAllUnsentLabelSurveyTask(listener);
        deleteAllUnsentLabelSurveyTask.execute();

    }

    public void deleteAllSentLabelSurveys(LabelSurveyRepositoryListener listener) {
        DeleteAllSentLabelSurveyTask deleteAllSentLabelSurveyTask = new DeleteAllSentLabelSurveyTask(listener);
        deleteAllSentLabelSurveyTask.execute();

    }

    private static class InsertLabelSurveyTask extends AsyncTask<String, Integer, Long> {
        private final LabelSurveyBean labelSurveyBean;
        private final LabelSurveyRepositoryListener listener;

        public InsertLabelSurveyTask(LabelSurveyBean labelSurveyBean, LabelSurveyRepositoryListener listener) {
            super();
            this.labelSurveyBean = labelSurveyBean;
            this.listener = listener;
        }

        @Override
        protected Long doInBackground(String... params) {
            //System.out.println(">>>>>>>>>doInBackground");
            LabelSurveyEntity labelSurveyEntity = new LabelSurveyEntity(labelSurveyBean);
            LabelSurveyDao labelSurveyDao = CarrefourRoomDatabase.getDatabase().labelSurveyDao();
            long id = labelSurveyDao.insert(labelSurveyEntity);
            return id;
        }

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

    private static class UpdateLabelSurveyTask extends AsyncTask<String, Integer, Boolean> {
        private final LabelSurveyBean labelSurveyBean;
        private final LabelSurveyRepositoryListener listener;

        public UpdateLabelSurveyTask(LabelSurveyBean labelSurveyBean, LabelSurveyRepositoryListener listener) {
            super();
            this.labelSurveyBean = labelSurveyBean;
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            LabelSurveyEntity labelSurveyEntity = new LabelSurveyEntity(labelSurveyBean);
            LabelSurveyDao labelSurveyDao = CarrefourRoomDatabase.getDatabase().labelSurveyDao();
            labelSurveyDao.update(labelSurveyEntity);
            return true;
        }

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

    private static class DeleteLabelSurveyTask extends AsyncTask<String, Integer, Boolean> {
        private final LabelSurveyBean labelSurveyBean;
        private final LabelSurveyRepositoryListener listener;

        public DeleteLabelSurveyTask(LabelSurveyBean labelSurveyBean, LabelSurveyRepositoryListener listener) {
            super();
            this.labelSurveyBean = labelSurveyBean;
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            LabelSurveyEntity labelSurveyEntity = new LabelSurveyEntity(labelSurveyBean);
            LabelSurveyDao labelSurveyDao = CarrefourRoomDatabase.getDatabase().labelSurveyDao();
            labelSurveyDao.delete(labelSurveyEntity);
            return true;
        }

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

    private static class GetAllLabelSurveyTask extends AsyncTask<String, Integer, ArrayList<LabelSurveyBean>> {
        private final LabelSurveyRepositoryListener listener;

        public GetAllLabelSurveyTask(LabelSurveyRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected ArrayList<LabelSurveyBean> doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            LabelSurveyDao labelSurveyDao = CarrefourRoomDatabase.getDatabase().labelSurveyDao();
            List<LabelSurveyEntity> labelSurveyEntities = labelSurveyDao.loadAllLabelSurveys();
            ArrayList<LabelSurveyBean> labelSurveyList = new ArrayList<>();
            for (LabelSurveyEntity phoneLabelSurveyEntity : labelSurveyEntities) {
                LabelSurveyBean labelSurveyBean = phoneLabelSurveyEntity.getBean();
                labelSurveyList.add(labelSurveyBean);
            }
            return labelSurveyList;
        }

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

    private static class GetAllUnsentLabelSurveyTask extends AsyncTask<String, Integer, ArrayList<LabelSurveyBean>> {
        private final LabelSurveyRepositoryListener listener;

        public GetAllUnsentLabelSurveyTask(LabelSurveyRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected ArrayList<LabelSurveyBean> doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            LabelSurveyDao labelSurveyDao = CarrefourRoomDatabase.getDatabase().labelSurveyDao();
            List<LabelSurveyEntity> labelSurveyEntities = labelSurveyDao.getAllUnsentLabelSurvey();
            ArrayList<LabelSurveyBean> labelSurveyList = new ArrayList<>();
            for (LabelSurveyEntity phoneLabelSurveyEntity : labelSurveyEntities) {
                LabelSurveyBean labelSurveyBean = phoneLabelSurveyEntity.getBean();
                labelSurveyList.add(labelSurveyBean);
            }
            return labelSurveyList;
        }

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

    private static class GetAllSentLabelSurveyTask extends AsyncTask<String, Integer, ArrayList<LabelSurveyBean>> {
        private final LabelSurveyRepositoryListener listener;

        public GetAllSentLabelSurveyTask(LabelSurveyRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected ArrayList<LabelSurveyBean> doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            LabelSurveyDao labelSurveyDao = CarrefourRoomDatabase.getDatabase().labelSurveyDao();
            List<LabelSurveyEntity> labelSurveyEntities = labelSurveyDao.getAllSentLabelSurvey();
            ArrayList<LabelSurveyBean> labelSurveyList = new ArrayList<>();
            for (LabelSurveyEntity phoneLabelSurveyEntity : labelSurveyEntities) {
                LabelSurveyBean labelSurveyBean = phoneLabelSurveyEntity.getBean();
                labelSurveyList.add(labelSurveyBean);
            }
            return labelSurveyList;
        }

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

    private static class GetLabelSurveyTask extends AsyncTask<String, Integer, LabelSurveyBean> {
        private final String labelSurveyID;
        private final LabelSurveyRepositoryListener listener;

        public GetLabelSurveyTask(String labelSurveyID, LabelSurveyRepositoryListener listener) {
            super();
            this.labelSurveyID = labelSurveyID;
            this.listener = listener;
        }

        @Override
        protected LabelSurveyBean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            LabelSurveyDao labelSurveyDao = CarrefourRoomDatabase.getDatabase().labelSurveyDao();
            List<LabelSurveyEntity> labelSurveyEntities = labelSurveyDao.getLabelSurvey(labelSurveyID);
            if (labelSurveyEntities != null && !labelSurveyEntities.isEmpty()) {
                return labelSurveyEntities.get(0).getBean();
            } else {
                return null;
            }
        }

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

    private static class DeleteAllLabelSurveyTask extends AsyncTask<String, Integer, Boolean> {
        private final LabelSurveyRepositoryListener listener;

        public DeleteAllLabelSurveyTask(LabelSurveyRepositoryListener listener) {
            super();
            this.listener = listener;
        }

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

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

    private static class DeleteAllUnsentLabelSurveyTask extends AsyncTask<String, Integer, Boolean> {
        private final LabelSurveyRepositoryListener listener;

        public DeleteAllUnsentLabelSurveyTask(LabelSurveyRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            LabelSurveyDao labelSurveyDao = CarrefourRoomDatabase.getDatabase().labelSurveyDao();
            labelSurveyDao.deleteAllUnsentLabelSurvey();
            return true;
        }

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

    private static class DeleteAllSentLabelSurveyTask extends AsyncTask<String, Integer, Boolean> {
        private final LabelSurveyRepositoryListener listener;

        public DeleteAllSentLabelSurveyTask(LabelSurveyRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            LabelSurveyDao labelSurveyDao = CarrefourRoomDatabase.getDatabase().labelSurveyDao();
            labelSurveyDao.deleteAllSentLabelSurvey();
            return true;
        }

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


    public static interface LabelSurveyRepositoryListener {

        void onAddCompleted(LabelSurveyBean labelSurveyBean);

        void onAllListLoaded(ArrayList<LabelSurveyBean> labelSurveyList);

        void onAllSentListLoaded(ArrayList<LabelSurveyBean> labelSurveyList);

        void onAllUnsentListLoaded(ArrayList<LabelSurveyBean> labelSurveyList);

        void onDeleteAllCompleted();

        void onDeleteAllSentCompleted();

        void onDeleteAllUnsentCompleted();

        void onDeleteCompleted(LabelSurveyBean labelSurveyBean);

        void onFailed();

        void onLabelSurveyBeanLoaded(LabelSurveyBean labelSurveyBean);

        void onUpdateCompleted(LabelSurveyBean labelSurveyBean);

    }

}
