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.InternalSalesDao;
import in.techware.carrefour.dbUtils.entity.InternalSalesEntity;
import in.techware.carrefour.model.InternalSalesBean;

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

    private static InternalSalesRepository instance;


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

    public void insertInternalSales(InternalSalesBean internalSalesBean, InternalSalesRepositoryListener listener) {
        InsertInternalSalesTask insertInternalSalesTask = new InsertInternalSalesTask(internalSalesBean, listener);
        insertInternalSalesTask.execute();

    }

    public void updateInternalSales(InternalSalesBean internalSalesBean, InternalSalesRepositoryListener listener) {
        UpdateInternalSalesTask updateInternalSalesTask = new UpdateInternalSalesTask(internalSalesBean, listener);
        updateInternalSalesTask.execute();

    }

    public void deleteInternalSales(InternalSalesBean internalSalesBean, InternalSalesRepositoryListener listener) {
        DeleteInternalSalesTask deleteInternalSalesTask = new DeleteInternalSalesTask(internalSalesBean, listener);
        deleteInternalSalesTask.execute();

    }

    public void getAllInternalSales(InternalSalesRepositoryListener listener) {
        GetAllInternalSalesTask getAllInternalSalesTask = new GetAllInternalSalesTask(listener);
        getAllInternalSalesTask.execute();

    }

    public void getAllUnsentInternalSales(InternalSalesRepositoryListener listener) {
        GetAllUnsentInternalSalesTask getAllUnsentInternalSalesTask = new GetAllUnsentInternalSalesTask(listener);
        getAllUnsentInternalSalesTask.execute();

    }

    public void getAllSentInternalSales(InternalSalesRepositoryListener listener) {
        GetAllSentInternalSalesTask getAllSentInternalSalesTask = new GetAllSentInternalSalesTask(listener);
        getAllSentInternalSalesTask.execute();

    }

    public void getInternalSales(String internalSalesID, InternalSalesRepositoryListener listener) {
        GetInternalSalesTask getInternalSalesTask = new GetInternalSalesTask(internalSalesID, listener);
        getInternalSalesTask.execute();

    }

    public void deleteAllInternalSales(InternalSalesRepositoryListener listener) {
        DeleteAllInternalSalesTask deleteAllInternalSalesTask = new DeleteAllInternalSalesTask(listener);
        deleteAllInternalSalesTask.execute();

    }

    public void deleteAllUnsentInternalSales(InternalSalesRepositoryListener listener) {
        DeleteAllUnsentInternalSalesTask deleteAllUnsentInternalSalesTask = new DeleteAllUnsentInternalSalesTask(listener);
        deleteAllUnsentInternalSalesTask.execute();

    }

    public void deleteAllSentInternalSales(InternalSalesRepositoryListener listener) {
        DeleteAllSentInternalSalesTask deleteAllSentInternalSalesTask = new DeleteAllSentInternalSalesTask(listener);
        deleteAllSentInternalSalesTask.execute();

    }

    private static class InsertInternalSalesTask extends AsyncTask<String, Integer, Long> {
        private final InternalSalesBean internalSalesBean;
        private final InternalSalesRepositoryListener listener;

        public InsertInternalSalesTask(InternalSalesBean internalSalesBean, InternalSalesRepositoryListener listener) {
            super();
            this.internalSalesBean = internalSalesBean;
            this.listener = listener;
        }

        @Override
        protected Long doInBackground(String... params) {
            //System.out.println(">>>>>>>>>doInBackground");
            InternalSalesEntity internalSalesEntity = new InternalSalesEntity(internalSalesBean);
            InternalSalesDao internalSalesDao = CarrefourRoomDatabase.getDatabase().internalSalesDao();
            long id = internalSalesDao.insert(internalSalesEntity);
            return id;
        }

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

    private static class UpdateInternalSalesTask extends AsyncTask<String, Integer, Boolean> {
        private final InternalSalesBean internalSalesBean;
        private final InternalSalesRepositoryListener listener;

        public UpdateInternalSalesTask(InternalSalesBean internalSalesBean, InternalSalesRepositoryListener listener) {
            super();
            this.internalSalesBean = internalSalesBean;
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            InternalSalesEntity internalSalesEntity = new InternalSalesEntity(internalSalesBean);
            InternalSalesDao internalSalesDao = CarrefourRoomDatabase.getDatabase().internalSalesDao();
            internalSalesDao.update(internalSalesEntity);
            return true;
        }

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

    private static class DeleteInternalSalesTask extends AsyncTask<String, Integer, Boolean> {
        private final InternalSalesBean internalSalesBean;
        private final InternalSalesRepositoryListener listener;

        public DeleteInternalSalesTask(InternalSalesBean internalSalesBean, InternalSalesRepositoryListener listener) {
            super();
            this.internalSalesBean = internalSalesBean;
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            InternalSalesEntity internalSalesEntity = new InternalSalesEntity(internalSalesBean);
            InternalSalesDao internalSalesDao = CarrefourRoomDatabase.getDatabase().internalSalesDao();
            internalSalesDao.delete(internalSalesEntity);
            return true;
        }

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

    private static class GetAllInternalSalesTask extends AsyncTask<String, Integer, ArrayList<InternalSalesBean>> {
        private final InternalSalesRepositoryListener listener;

        public GetAllInternalSalesTask(InternalSalesRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected ArrayList<InternalSalesBean> doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            InternalSalesDao internalSalesDao = CarrefourRoomDatabase.getDatabase().internalSalesDao();
            List<InternalSalesEntity> internalSalesEntities = internalSalesDao.loadAllInternalSales();
            ArrayList<InternalSalesBean> internalSalesList = new ArrayList<>();
            for (InternalSalesEntity phoneInternalSalesEntity : internalSalesEntities) {
                InternalSalesBean internalSalesBean = phoneInternalSalesEntity.getBean();
                internalSalesList.add(internalSalesBean);
            }
            return internalSalesList;
        }

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

    private static class GetAllUnsentInternalSalesTask extends AsyncTask<String, Integer, ArrayList<InternalSalesBean>> {
        private final InternalSalesRepositoryListener listener;

        public GetAllUnsentInternalSalesTask(InternalSalesRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected ArrayList<InternalSalesBean> doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            InternalSalesDao internalSalesDao = CarrefourRoomDatabase.getDatabase().internalSalesDao();
            List<InternalSalesEntity> internalSalesEntities = internalSalesDao.getAllUnsentInternalSales();
            ArrayList<InternalSalesBean> internalSalesList = new ArrayList<>();
            for (InternalSalesEntity phoneInternalSalesEntity : internalSalesEntities) {
                InternalSalesBean internalSalesBean = phoneInternalSalesEntity.getBean();
                internalSalesList.add(internalSalesBean);
            }
            return internalSalesList;
        }

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

    private static class GetAllSentInternalSalesTask extends AsyncTask<String, Integer, ArrayList<InternalSalesBean>> {
        private final InternalSalesRepositoryListener listener;

        public GetAllSentInternalSalesTask(InternalSalesRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected ArrayList<InternalSalesBean> doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            InternalSalesDao internalSalesDao = CarrefourRoomDatabase.getDatabase().internalSalesDao();
            List<InternalSalesEntity> internalSalesEntities = internalSalesDao.getAllSentInternalSales();
            ArrayList<InternalSalesBean> internalSalesList = new ArrayList<>();
            for (InternalSalesEntity phoneInternalSalesEntity : internalSalesEntities) {
                InternalSalesBean internalSalesBean = phoneInternalSalesEntity.getBean();
                internalSalesList.add(internalSalesBean);
            }
            return internalSalesList;
        }

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

    private static class GetInternalSalesTask extends AsyncTask<String, Integer, InternalSalesBean> {
        private final String internalSalesID;
        private final InternalSalesRepositoryListener listener;

        public GetInternalSalesTask(String internalSalesID, InternalSalesRepositoryListener listener) {
            super();
            this.internalSalesID = internalSalesID;
            this.listener = listener;
        }

        @Override
        protected InternalSalesBean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            InternalSalesDao internalSalesDao = CarrefourRoomDatabase.getDatabase().internalSalesDao();
            List<InternalSalesEntity> internalSalesEntities = internalSalesDao.getInternalSales(internalSalesID);
            if (internalSalesEntities != null && !internalSalesEntities.isEmpty()) {
                return internalSalesEntities.get(0).getBean();
            } else {
                return null;
            }
        }

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

    private static class DeleteAllInternalSalesTask extends AsyncTask<String, Integer, Boolean> {
        private final InternalSalesRepositoryListener listener;

        public DeleteAllInternalSalesTask(InternalSalesRepositoryListener listener) {
            super();
            this.listener = listener;
        }

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

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

    private static class DeleteAllUnsentInternalSalesTask extends AsyncTask<String, Integer, Boolean> {
        private final InternalSalesRepositoryListener listener;

        public DeleteAllUnsentInternalSalesTask(InternalSalesRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            InternalSalesDao internalSalesDao = CarrefourRoomDatabase.getDatabase().internalSalesDao();
            internalSalesDao.deleteAllUnsentInternalSales();
            return true;
        }

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

    private static class DeleteAllSentInternalSalesTask extends AsyncTask<String, Integer, Boolean> {
        private final InternalSalesRepositoryListener listener;

        public DeleteAllSentInternalSalesTask(InternalSalesRepositoryListener listener) {
            super();
            this.listener = listener;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            System.out.println(">>>>>>>>>doInBackground");
            InternalSalesDao internalSalesDao = CarrefourRoomDatabase.getDatabase().internalSalesDao();
            internalSalesDao.deleteAllSentInternalSales();
            return true;
        }

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


    public static interface InternalSalesRepositoryListener {


        void onAddCompleted(InternalSalesBean internalSalesBean);

        void onAllListLoaded(ArrayList<InternalSalesBean> internalSalesList);

        void onAllSentListLoaded(ArrayList<InternalSalesBean> internalSalesList);

        void onAllUnsentListLoaded(ArrayList<InternalSalesBean> internalSalesList);

        void onDeleteAllCompleted();

        void onDeleteAllSentCompleted();

        void onDeleteAllUnsentCompleted();

        void onDeleteCompleted(InternalSalesBean internalSalesBean);

        void onFailed();

        void onUpdateCompleted(InternalSalesBean internalSalesBean);

        void onInternalSalesBeanLoaded(InternalSalesBean internalSalesBean);

    }

}
