package in.techware.carrefour.util;

import java.util.ArrayList;

import in.techware.carrefour.model.FacingBean;
import in.techware.carrefour.model.InternalSalesBean;
import in.techware.carrefour.model.InternalSalesCorrectionBean;
import in.techware.carrefour.model.ItemListBean;
import in.techware.carrefour.model.MinStockBean;
import in.techware.carrefour.model.StockTakeBean;
import in.techware.carrefour.model.WasteBean;

/**
 * Created by Jemsheer K D on 01 August, 2018.
 * Package in.techware.carrefour.util
 * Project Carrefour
 */
public class FileTransferUtil {

    public static ArrayList<WasteBean> getUniqueWasteList(ArrayList<WasteBean> wasteList) {
        ArrayList<WasteBean> list = new ArrayList<>();

        for (WasteBean wasteBean : wasteList) {
            boolean flag = false;
            for (int i = 0; i < list.size(); i++) {
                WasteBean bean = list.get(i);
                if (bean.getBarcode().equals(wasteBean.getBarcode())
                        && bean.getOriginTypeCode().equals(wasteBean.getOriginTypeCode())) {
                    bean.setQuantity(bean.getQuantity() + wasteBean.getQuantity());
                    list.set(i, bean);
                    flag = true;
                    break;
                }
            }

            if (!flag) {
                list.add(wasteBean);
            }
        }

        return list;
    }

    public static ArrayList<InternalSalesBean> getUniqueInternalSaleList(ArrayList<InternalSalesBean> internalSaleList) {
        ArrayList<InternalSalesBean> list = new ArrayList<>();

        for (InternalSalesBean internalSalesBean : internalSaleList) {
            boolean flag = false;
            for (int i = 0; i < list.size(); i++) {
                InternalSalesBean bean = list.get(i);
                if (bean.getBarcode().equals(internalSalesBean.getBarcode())
                        && bean.getDepartmentCode().equals(internalSalesBean.getDepartmentCode())
                        && bean.getSectionCode().equals(internalSalesBean.getSectionCode())) {
                    bean.setQuantity(bean.getQuantity() + internalSalesBean.getQuantity());
                    list.set(i, bean);
                    flag = true;
                    break;
                }
            }

            if (!flag) {
                list.add(internalSalesBean);
            }
        }

        return list;
    }

    public static ArrayList<InternalSalesCorrectionBean> getUniqueInternalSaleCorrectionList(
            ArrayList<InternalSalesCorrectionBean> internalSaleList) {
        ArrayList<InternalSalesCorrectionBean> list = new ArrayList<>();

        for (InternalSalesCorrectionBean internalSalesCorrectionBean : internalSaleList) {
            boolean flag = false;
            for (int i = 0; i < list.size(); i++) {
                InternalSalesCorrectionBean bean = list.get(i);
                if (bean.getBarcode().equals(internalSalesCorrectionBean.getBarcode())
                        && bean.getDepartmentCode().equals(internalSalesCorrectionBean.getDepartmentCode())
                        && bean.getSectionCode().equals(internalSalesCorrectionBean.getSectionCode())) {
                    bean.setQuantity(bean.getQuantity() + internalSalesCorrectionBean.getQuantity());
                    list.set(i, bean);
                    flag = true;
                    break;
                }
            }

            if (!flag) {
                list.add(internalSalesCorrectionBean);
            }
        }

        return list;
    }

    public static ArrayList<StockTakeBean> getUniqueStockTakeList(ArrayList<StockTakeBean> stockTakeList) {
        ArrayList<StockTakeBean> list = new ArrayList<>();

        for (StockTakeBean stockTakeBean : stockTakeList) {
            boolean flag = false;
            for (int i = 0; i < list.size(); i++) {
                StockTakeBean bean = list.get(i);
                if (bean.getBarcode().equals(stockTakeBean.getBarcode())) {
                    bean.setQuantity(bean.getQuantity() + stockTakeBean.getQuantity());
                    list.set(i, bean);
                    flag = true;
                    break;
                }
            }

            if (!flag) {
                list.add(stockTakeBean);
            }
        }

        return list;
    }

    public static ArrayList<MinStockBean> getUniqueMinStockList(ArrayList<MinStockBean> minStockList) {
        ArrayList<MinStockBean> list = new ArrayList<>();

        for (MinStockBean minStockBean : minStockList) {
            boolean flag = false;
            for (int i = 0; i < list.size(); i++) {
                MinStockBean bean = list.get(i);
                if (bean.getBarcode().equals(minStockBean.getBarcode())) {
                    bean.setQuantity(bean.getQuantity() + minStockBean.getQuantity());
                    list.set(i, bean);
                    flag = true;
                    break;
                }
            }

            if (!flag) {
                list.add(minStockBean);
            }
        }

        return list;
    }

    public static ArrayList<FacingBean> getUniqueFacingList(ArrayList<FacingBean> facingList) {
        ArrayList<FacingBean> list = new ArrayList<>();

        for (FacingBean facingBean : facingList) {
            boolean flag = false;
            for (int i = 0; i < list.size(); i++) {
                FacingBean bean = list.get(i);
                if (bean.getBarcode().equals(facingBean.getBarcode())) {
                    bean.setQuantity(bean.getQuantity() + facingBean.getQuantity());
                    list.set(i, bean);
                    flag = true;
                    break;
                }
            }

            if (!flag) {
                list.add(facingBean);
            }
        }

        return list;
    }

    public static ArrayList<ItemListBean> getUniqueItemListList(ArrayList<ItemListBean> itemListList) {
        ArrayList<ItemListBean> list = new ArrayList<>();

        for (ItemListBean itemListBean : itemListList) {
            boolean flag = false;
            for (int i = 0; i < list.size(); i++) {
                ItemListBean bean = list.get(i);
                if (bean.getBarcode().equals(itemListBean.getBarcode())) {
                    bean.setQuantity(bean.getQuantity() + itemListBean.getQuantity());
                    list.set(i, bean);
                    flag = true;
                    break;
                }
            }

            if (!flag) {
                list.add(itemListBean);
            }
        }

        return list;
    }
}
