package com.gingersoft.gsa.cloud.print.bean.adapter;

import com.gingersoft.gsa.cloud.app.GsaCloudApplication;
import com.gingersoft.gsa.cloud.common.bean.SkyOrderItemDetailBean;
import com.gingersoft.gsa.cloud.common.bean.SkyOrderServingBean;
import com.gingersoft.gsa.cloud.common.constans.AppConstant;
import com.gingersoft.gsa.cloud.common.core.delivery.OrderDetails;
import com.gingersoft.gsa.cloud.common.core.restaurant.RestaurantInfoManager;
import com.gingersoft.gsa.cloud.common.core.settlement.SettlementReport;
import com.gingersoft.gsa.cloud.common.core.table.TableBean;
import com.gingersoft.gsa.cloud.common.utils.MoneyUtil;
import com.gingersoft.gsa.cloud.common.utils.other.TextUtil;
import com.gingersoft.gsa.cloud.common.utils.time.TimeUtils;
import com.gingersoft.gsa.cloud.order.billItem.BillItem;
import com.gingersoft.gsa.cloud.order.commodity.OrderDetail;
import com.gingersoft.gsa.cloud.order.order.MealOrder;
import com.gingersoft.gsa.cloud.pay.bean.PayMethod;
import com.gingersoft.gsa.cloud.print.bean.PrintBillContent;
import com.gingersoft.gsa.cloud.print.bean.PrintCleanMachineContent;
import com.gingersoft.gsa.cloud.print.bean.PrintContent;
import com.gingersoft.gsa.cloud.print.bean.PrintServingPaperContent;
import com.gingersoft.gsa.cloud.print.bean.PrintSlipContent;
import com.gingersoft.gsa.cloud.print.bean.PrintTakeawayCheckoutContent;
import com.gingersoft.gsa.cloud.print.bean.PrintTakeawayFormContent;
import com.gingersoft.gsa.cloud.print.bean.base.PrintBillItem;
import com.gingersoft.gsa.cloud.print.bean.base.PrintFoodItem;
import com.gingersoft.gsa.cloud.print.bean.base.PrintPayTypeItem;

import java.util.ArrayList;
import java.util.List;

import lombok.Data;

/**
 * @author : bin
 * @create date: 2020-12-01
 * @update date: 2020-12-01
 * @description:打印內容適配類，打印類只需要關注自身需要的實體信息
 */
@Data
public class PrintContentAdapter implements AdaptationContent {

    public PrintContentAdapter() {
    }

    @Override
    public PrintContent adaptationQRCode() {
        return null;
    }

    @Override
    public PrintContent adaptationPrintTakeawayCheckoutContent(OrderDetails.DataBean data) {
        return getPrintTakeawayFormContent(data, 1);
    }

    @Override
    public PrintContent adaptationPrintTakeawayFormContent(OrderDetails.DataBean data) {
        return getPrintTakeawayFormContent(data, 2);
    }

    /**
     * 清機打印
     *
     * @param settlementReport
     * @return
     */
    @Override
    public PrintContent adaptationPrintCleanMachineContent(SettlementReport settlementReport) {
        PrintCleanMachineContent cleanMachineContent = new PrintCleanMachineContent();
        cleanMachineContent.setYesterdayTime(settlementReport.getYesterdayTime());
        cleanMachineContent.setVo(settlementReport.getVo());
        cleanMachineContent.setCash(settlementReport.getCash());
        cleanMachineContent.setAnalysis(settlementReport.getAnalysis());
        cleanMachineContent.setRestaurantOperationTime(TimeUtils.getStringByFormat(settlementReport.getRestaurantOperation().getOperationTime(), TimeUtils.DEFAULT_DATE_FORMAT));
        cleanMachineContent.setStartTime(settlementReport.getStartTime());
        cleanMachineContent.setCurrentTime(settlementReport.getCurrentTime());
        return cleanMachineContent;
    }

    /**
     * 印單
     *
     * @param mealOrder
     * @param tableBean
     * @return
     */
    @Override
    public PrintContent adaptationPrintSlipContent(MealOrder mealOrder, TableBean.DataBean tableBean) {
        PrintSlipContent slipContent = new PrintSlipContent();
        slipContent.setBrand(RestaurantInfoManager.newInstance().getBrandName());
        slipContent.setResturantName(RestaurantInfoManager.newInstance().getRestaurantName());
        slipContent.setTableName(tableBean.getTableName());
        slipContent.setPerson(tableBean.getPerson());
        slipContent.setCreateTime(tableBean.getCreateTime());
        slipContent.setOrderNo(mealOrder.getOrderNo());
//        slipContent.setFoodItemList(orderDetailItemCastPrintFoodItemList(mealOrder.getNewFoodList()));
        slipContent.setFoodItemList(orderDetailItemCastPrintFoodItemList(mealOrder.getShoppingCart().getOrderCommodityList()));
        slipContent.setBillItemList(billItemCastPrintBillItemList(mealOrder.getShoppingCart().getBillItemList()));
        slipContent.setWholeAmount(mealOrder.getShoppingCart().getWholeAmount());
        slipContent.setTotalAmount(mealOrder.getShoppingCart().getTotalAmount());
        slipContent.setPoliteLanguage(GsaCloudApplication.functionConfiguration.getBottomPoliteLanguageVaule());
        return slipContent;
    }

    /**
     * 送單
     *
     * @param mealOrder
     * @param tableBean
     * @return
     */
    @Override
    public PrintContent adaptationSendOrderPaperContent(MealOrder mealOrder, TableBean.DataBean tableBean) {
        PrintServingPaperContent servingPaperContent = new PrintServingPaperContent();
        servingPaperContent.setBrand(RestaurantInfoManager.newInstance().getBrandName());
        servingPaperContent.setResturantName(RestaurantInfoManager.newInstance().getRestaurantName());
        servingPaperContent.setTableName(tableBean.getTableName());
        servingPaperContent.setPerson(tableBean.getPerson());
        servingPaperContent.setCreateTime(tableBean.getCreateTime());
        servingPaperContent.setOrderNo(mealOrder.getOrderNo());
        //送單
        servingPaperContent.setFoodItemList(orderDetailItemCastPrintFoodItemList(mealOrder.getNewFoodList()));
        servingPaperContent.setPoliteLanguage(GsaCloudApplication.functionConfiguration.getBottomPoliteLanguageVaule());
        return servingPaperContent;
    }

    /**
     * 上菜紙
     *
     * @param mealOrder
     * @param tableBean
     * @return
     */
    @Override
    public PrintContent adaptationServingPaperContent(MealOrder mealOrder, TableBean.DataBean tableBean) {
        PrintServingPaperContent servingPaperContent = new PrintServingPaperContent();
        servingPaperContent.setBrand(RestaurantInfoManager.newInstance().getBrandName());
        servingPaperContent.setResturantName(RestaurantInfoManager.newInstance().getRestaurantName());
        servingPaperContent.setTableName(tableBean.getTableName());
        servingPaperContent.setPerson(tableBean.getPerson());
        servingPaperContent.setCreateTime(tableBean.getCreateTime());
        servingPaperContent.setOrderNo(mealOrder.getOrderNo());
        servingPaperContent.setFoodItemList(orderDetailItemCastPrintFoodItemList(mealOrder.getShoppingCart().getOrderCommodityList()));
        servingPaperContent.setPoliteLanguage(GsaCloudApplication.functionConfiguration.getBottomPoliteLanguageVaule());
        return servingPaperContent;
    }

    @Override
    public PrintContent adaptationServingPaperContent(SkyOrderServingBean skyOrderServingBean) {
        PrintServingPaperContent servingPaperContent = new PrintServingPaperContent();
        servingPaperContent.setBrand(RestaurantInfoManager.newInstance().getBrandName());
        servingPaperContent.setResturantName(RestaurantInfoManager.newInstance().getRestaurantName());
        servingPaperContent.setTableName(skyOrderServingBean.getTableName());
        servingPaperContent.setPerson(skyOrderServingBean.getPerson());
        servingPaperContent.setCreateTime(TimeUtils.getCurrentDate(TimeUtils.DEFAULT_DATE_FORMAT));
        servingPaperContent.setOrderNo(skyOrderServingBean.getOrderId() + "");
        servingPaperContent.setFoodItemList(skyOrderServingCastPrintFoodItemList(skyOrderServingBean.getFoods()));
        servingPaperContent.setPoliteLanguage(GsaCloudApplication.functionConfiguration.getBottomPoliteLanguageVaule());
        return servingPaperContent;
    }

    /**
     * 打印結賬單
     *
     * @param mealOrder
     * @param tableBean
     * @return
     */
    @Override
    public PrintContent adaptationBillContent(MealOrder mealOrder, TableBean.DataBean tableBean) {
        PrintBillContent billContent = new PrintBillContent();
        billContent.setBrand(RestaurantInfoManager.newInstance().getBrandName());
        billContent.setResturantName(RestaurantInfoManager.newInstance().getRestaurantName());
        billContent.setTableName(tableBean.getTableName());
        billContent.setPerson(tableBean.getPerson());
        billContent.setCreateTime(tableBean.getCreateTime());
        billContent.setOrderNo(mealOrder.getOrderNo());
        billContent.setFoodItemList(orderDetailItemCastPrintFoodItemList(mealOrder.getShoppingCart().getOrderCommodityList()));
        billContent.setBillItemList(billItemCastPrintBillItemList(mealOrder.getShoppingCart().getBillItemList()));
        billContent.setPayMethodItemList(payMethodItemCastPrintBillItemList(mealOrder.getBillMoney()));
        billContent.setWholeAmount(mealOrder.getShoppingCart().getWholeAmount());
        billContent.setTotalAmount(mealOrder.getShoppingCart().getTotalAmount());
        if (mealOrder.getMemberInfo() != null) {
            billContent.setMemberName(mealOrder.getMemberInfo().getMemberName());
            billContent.setPhone(mealOrder.getMemberInfo().getPhone());
        }
        billContent.setAddPoints(mealOrder.getAddPoints());
        billContent.setOldPoints(mealOrder.getOldPoints());
        billContent.setNowPoints(mealOrder.getNowPoints());
        billContent.setIntegralQrCode(mealOrder.getIntegralQrcode());
        billContent.setPoliteLanguage(GsaCloudApplication.functionConfiguration.getBottomPoliteLanguageVaule());
        return billContent;
    }

    private List<PrintFoodItem> skyOrderServingCastPrintFoodItemList(List<SkyOrderServingBean.FoodsBean> foods) {
        List<PrintFoodItem> printFoodItems = new ArrayList<>();
        for (SkyOrderServingBean.FoodsBean food : foods) {
            //上菜紙不顯示價格，填0
            printFoodItems.add(new PrintFoodItem(food.getProductName(), food.getNumber(), 0, 1));
            if (food.getItemDetail() != null) {
                for (List<SkyOrderItemDetailBean> skyOrderItemDetailBeans : food.getItemDetail()) {
                    if (skyOrderItemDetailBeans != null) {
                        for (SkyOrderItemDetailBean skyOrderItemDetailBean : skyOrderItemDetailBeans) {
                            printFoodItems.add(new PrintFoodItem(skyOrderItemDetailBean.getProductName(), skyOrderItemDetailBean.getNumber(), 0, 2));
                            if (skyOrderItemDetailBean.getItemDetail() != null) {
                                for (SkyOrderItemDetailBean orderItemDetailBean : skyOrderItemDetailBean.getItemDetail()) {
                                    printFoodItems.add(new PrintFoodItem(orderItemDetailBean.getProductName(), orderItemDetailBean.getNumber(), 0, 3));
                                }
                            }
                        }
                    }
                }
            }
        }
        return printFoodItems;
    }

    private List<PrintFoodItem> orderDetailItemCastPrintFoodItemList(List<OrderDetail> newFoodList) {
        List<PrintFoodItem> printFoodItems = new ArrayList<>();
        if (newFoodList != null) {
            for (OrderDetail orderDetail : newFoodList) {
                printFoodItems.add(new PrintFoodItem(orderDetail.getProductName(), orderDetail.getNumber(), orderDetail.getPrice(), orderDetail.getItemType()));
            }
        }
        return printFoodItems;
    }

    private List<PrintBillItem> billItemCastPrintBillItemList(List<BillItem> billItems) {
        List<PrintBillItem> printBillItems = new ArrayList<>();
        if (billItems != null) {
            for (BillItem billItem : billItems) {
                printBillItems.add(new PrintBillItem(billItem.getName(), String.valueOf(billItem.getMoney())));
            }
        }
        return printBillItems;
    }

    private List<PrintBillItem> payMethodItemCastPrintBillItemList(List<PayMethod> payMethods) {
        List<PrintBillItem> printBillItems = new ArrayList<>();
        if (payMethods != null) {
            for (PayMethod payMethod : payMethods) {
                printBillItems.add(new PrintBillItem(payMethod.getPayName(), String.valueOf(payMethod.getPayMoney())));
            }
        }
        return printBillItems;
    }

    public PrintTakeawayFormContent getPrintTakeawayFormContent(OrderDetails.DataBean data, int type) {
        PrintTakeawayFormContent takeawayPrintContent;
        if (type == 1) {
            takeawayPrintContent = new PrintTakeawayCheckoutContent();
            PrintTakeawayCheckoutContent checkoutContent = (PrintTakeawayCheckoutContent) takeawayPrintContent;
            checkoutContent.setPayTypeList(getPrintPayTypeItemsByPayMultiple(data.getPayMultiple()));
        } else {
            takeawayPrintContent = new PrintTakeawayFormContent();
        }
        takeawayPrintContent.setBrand(RestaurantInfoManager.newInstance().getBrandName());
        takeawayPrintContent.setResturantName(RestaurantInfoManager.newInstance().getRestaurantName());
        if (data.getOrderType() == 2) {
            takeawayPrintContent.setOrderType("外送");
        } else {
            takeawayPrintContent.setOrderType("自取");
        }
        if (data.getPayType() == 2) {
            if (!TextUtil.isEmptyOrNullOrUndefined(data.getPayTime())) {
                //支付時間，只有在線支付有
                if (!TextUtil.isEmptyOrNullOrUndefined(data.getPayTime())) {
                    takeawayPrintContent.setPayTime("支付時間：" + data.getPayTime());
                }
            }
        }
        //取餐碼
        if (data.getOrderType() == 7) {
            //自取  顯示 取餐碼+訂單碼
            if (!TextUtil.isEmptyOrNullOrUndefined(data.getTakeFoodCode())) {
                takeawayPrintContent.setOrderTakeFoodCode("取餐碼：" + data.getTakeFoodCode());
            }
        } else {
            //外送 顯示訂單碼
            takeawayPrintContent.setOrderTakeFoodCode("訂單碼：" + data.getTakeFoodCode());
        }
        if (data.getTakeFoodCode() == null || data.getTakeFoodCode().equals("0")) {
            takeawayPrintContent.setOrderTakeFoodCode("");
        }
        if (!TextUtil.isEmptyOrNullOrUndefined(data.getRemark())) {
            takeawayPrintContent.setRemark("備註：" + data.getRemark());
        }
        if (!TextUtil.isEmptyOrNullOrUndefined(data.getBillNo())) {
            takeawayPrintContent.setBillNo("訂單碼：" + data.getBillNo());
        }
        takeawayPrintContent.setOrderNo("單號：" + data.getORDER_NO());
        //支付類型
        takeawayPrintContent.setPayType(getPaytype(data));
        takeawayPrintContent.setDeliveryTime(data.getOrderType() == 2 ? data.getSEND_TIME() : data.getTakeTime());
        takeawayPrintContent.setBillingTime("開單時間：" + TimeUtils.parseTimeRepeat(data.getCREATE_TIME(), TimeUtils.DEFAULT_DATE_FORMAT));
        takeawayPrintContent.setRemark("備註：" + data.getRemark());
        takeawayPrintContent.setFoodItemList(getPrintFoodItemsByProductBeans(data.getPRODUCT_NAME()));
        takeawayPrintContent.setBillItemList(getBillItemListByInfo(data.getTOTAL_AMOUNT(), data.getLunchbox(), data.getDELIVERY_CHARGE(), data.getCouponList()));
        takeawayPrintContent.setTotalAmountText("總金額: ");
        takeawayPrintContent.setTotalAmount(AppConstant.CARSH_STR + data.getTOTAL_AMOUNT());
        takeawayPrintContent.setPayAmountText("支付金額: ");
        double disCount = 0.0;
        if (data.getCouponList() != null) {
            for (OrderDetails.DataBean.CouponBean couponBean : data.getCouponList()) {
                disCount = MoneyUtil.sum(disCount, couponBean.getDiscount_amount());
            }
        }
        takeawayPrintContent.setPayAmount(AppConstant.CARSH_STR + MoneyUtil.sub(Double.parseDouble(data.getTOTAL_AMOUNT()), disCount));
        takeawayPrintContent.setMemberName("會員名稱: " + data.getMEMBER_NAME());
//        takeawayPrintContent.setMemberNumber(data.getm);
        takeawayPrintContent.setMemberPhone("會員電話: " + data.getPHONE());
        takeawayPrintContent.setMemberOldPoints("上次積分: " + data.getOldPoints());
        takeawayPrintContent.setMemberAddPoints("本次積分: " + data.getAddPoints());
        takeawayPrintContent.setMemberNowPoints("結餘積分: " + data.getNowPoints());
        if (data.getOrderType() == 2) {
            //自取，隱藏地址和收貨人
//            takeawayPrintContent.setAdress("地址: " + data.getAddressDetail());
            takeawayPrintContent.setAdress(data.getAddressDetail());
            takeawayPrintContent.setReceiver("收貨人: " + data.getRECEIVER());
        }
        takeawayPrintContent.setPhone("手機號: " + data.getPHONE());
        takeawayPrintContent.setPoliteLanguage(GsaCloudApplication.functionConfiguration.getBottomPoliteLanguageVaule());
        return takeawayPrintContent;
    }

    private String getPaytype(OrderDetails.DataBean data) {
        String payTypeStr = null;
        if (data.getPayType() == 1) {
            //貨到付款
            payTypeStr = "貨到付款";
        } else if (data.getPayType() == 0) {
            payTypeStr = "店內支付";
        } else if (data.getPayType() == 2) {
            if (data.getPayMultiple() != null) {
                StringBuilder payName = new StringBuilder();
                for (OrderDetails.DataBean.PayMultiple payMultiple : data.getPayMultiple()) {
                    payName.append(payMultiple.getPayName());
                    payName.append(",");
                }
                if (payName.length() > 0) {
                    payName.deleteCharAt(payName.length() - 1);
                }
                payTypeStr = "在線支付：" + payName.toString();
            } else {
                payTypeStr = "在線支付";
            }
        }
        return payTypeStr;
    }

    private List<PrintFoodItem> getPrintFoodItemsByProductBeans(List<OrderDetails.DataBean.PRODUCTNAMEBean> productnameBeanList) {
        List<OrderDetail> orderDetailList = OrderDetail.productMameBeanToOrderDetail(1, productnameBeanList);
        List<PrintFoodItem> printFoodItems = new ArrayList<>();
        for (OrderDetail orderDetail : orderDetailList) {
            printFoodItems.add(new PrintFoodItem(orderDetail.getProductName(), orderDetail.getNumber(), orderDetail.getPrice(), orderDetail.getItemType()));
        }
        return printFoodItems;
    }

    private List<PrintBillItem> getBillItemListByInfo(String totalAmount, double lunchbox, double deliveryCharge, List<OrderDetails.DataBean.CouponBean> couponBeanList) {
        List<PrintBillItem> billItems = new ArrayList<>();
        billItems.add(new PrintBillItem("合計: ", AppConstant.CARSH_STR + MoneyUtil.sub(MoneyUtil.sub(Double.parseDouble(totalAmount), lunchbox), deliveryCharge)));
        if (lunchbox != 0.0) {
            billItems.add(new PrintBillItem("餐盒費: ", AppConstant.CARSH_STR + lunchbox));
        }
        if (deliveryCharge != 0.0) {
            billItems.add(new PrintBillItem("送貨費: ", AppConstant.CARSH_STR + deliveryCharge));
        }
        if (couponBeanList != null && couponBeanList.size() > 0) {
            for (OrderDetails.DataBean.CouponBean couponBean : couponBeanList) {
                billItems.add(new PrintBillItem(couponBean.getCouponName(), AppConstant.CARSH_STR + couponBean.getDiscount_amount()));
            }
        }
        return billItems;
    }

    private List<PrintPayTypeItem> getPrintPayTypeItemsByPayMultiple(List<OrderDetails.DataBean.PayMultiple> payMultiples) {
        List<PrintPayTypeItem> printPayTypeItems = new ArrayList<>();
        if (payMultiples != null) {
            for (OrderDetails.DataBean.PayMultiple payMultiple : payMultiples) {
                printPayTypeItems.add(new PrintPayTypeItem(payMultiple.getPayName(), AppConstant.CARSH_STR + payMultiple.getAmount()));
            }
        }
        return printPayTypeItems;
    }
}
