package lingtao.net.Factory;

import lingtao.net.bean.Product;
import lingtao.net.util.PriceUtils;
import lingtao.net.vo.PricingListVo;
import lingtao.net.vo.ProductVo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class MetalTargetStrategy implements PricingStrategy {

    @Override
    public PricingListVo calculatePrice(Product dto, String role) {
        String plantName = dto.getPlantName();
        double width = dto.getWidth();
        double length = dto.getLength();
        int count = dto.getCount();
        int number = dto.getNumber();

        List<ProductVo> resultList = new ArrayList<>();

        // 定义纸张尺寸（单位：毫米）
        double paperLength = 295; // 纸张长度
        double paperWidth = 195;  // 纸张宽度
        double basePrice = 0; // 基础价格
        double minPrice = 0; // 起步价
        double price = 0; // 最终价格
        double colorPrice = 0; // 颜色附加费
        double moreColorPrice = 0; // 多颜色附加费
        int requiredSheets = 0;
        BigDecimal weight = BigDecimal.ZERO;
        // 转换尺寸为毫米并增加边距
        length = convertToMillimetersWithMargin(length);
        width = convertToMillimetersWithMargin(width);

        if ("金大".equals(plantName) || "鑫灿".equals(plantName)) {

            // 检查是否超出纸张尺寸限制
            if (!isWithinPaperSize(length, width, paperLength, paperWidth)) {

                return new PricingListVo(100, "出纸张尺寸限制"); // 返回空列表表示不适用
            }

            // 计算一张纸能容纳的最大金属标数量
            double maxItemsPerSheet = calculateMaxItemsPerSheet(length, width, paperLength, paperWidth);

            // 计算所需纸张数量
            requiredSheets = calculateRequiredSheets(count, number, maxItemsPerSheet);

            // 计算颜色附加费
            if (!"金色".equals(dto.getTcolor()) && !"银色".equals(dto.getTcolor()) && !"玫瑰金色".equals(dto.getTcolor())) {
                colorPrice = 1;
            }

            if ("金大".equals(plantName)) {
                // 设置起步价
                minPrice = 45;
                if ("双色".equals(dto.getCraftMo())) {
                    minPrice = 70;
                    moreColorPrice = 10;
                }
                if ("彩色(三色)".equals(dto.getCraftMo())) {
                    minPrice = 105;
                    moreColorPrice = 20;

                }
                colorPrice += moreColorPrice;
                // 根据数量计算基础价格
                if (requiredSheets <= 2) {
                    price = minPrice + colorPrice;
                } else if (requiredSheets <= 9) {
                    price = minPrice + (requiredSheets - 2) * (12 + colorPrice);
                } else if (requiredSheets <= 19) {
                    price = Math.ceil((13 + colorPrice) * requiredSheets);
                } else if (requiredSheets <= 29) {
                    price = Math.ceil((12 + colorPrice) * requiredSheets);
                } else if (requiredSheets <= 39) {
                    price = Math.ceil((11 + colorPrice) * requiredSheets);
                } else if (requiredSheets <= 49) {
                    price = Math.ceil((10 + colorPrice) * requiredSheets);
                } else {
                    price = Math.ceil((9 + colorPrice) * requiredSheets);
                }
            }
            if ("鑫灿".equals(plantName)) {
                // 设置起步价
                minPrice = 45;
                if ("双色".equals(dto.getCraftMo())) {
                    minPrice = 55;
                    moreColorPrice = 10;
                }
                if ("彩色(三色)".equals(dto.getCraftMo())) {
                    minPrice = 105;
                    moreColorPrice = 20;

                }
                colorPrice += moreColorPrice;
                // 根据数量计算基础价格
                if (requiredSheets <= 2) {
                    price = minPrice + colorPrice;
                } else if (requiredSheets <= 9) {
                    price = minPrice + (requiredSheets - 2) * (10 + colorPrice);
                } else if (requiredSheets <= 19) {
                    price = Math.ceil((13 + colorPrice) * requiredSheets);
                } else if (requiredSheets <= 29) {
                    price = Math.ceil((12 + colorPrice) * requiredSheets);
                } else if (requiredSheets <= 39) {
                    price = Math.ceil((11 + colorPrice) * requiredSheets);
                } else if (requiredSheets <= 49) {
                    price = Math.ceil((10 + colorPrice) * requiredSheets);
                } else if (requiredSheets <= 100) {
                    price = Math.ceil((9 + colorPrice) * requiredSheets);
                } else {
                    price = Math.ceil((8 + colorPrice) * requiredSheets);
                }

            }

        }
        if ("紫瑶".equals(plantName)) {
            paperLength = 170;
            paperWidth = 300;
            // 检查是否超出纸张尺寸限制
            if (!isWithinPaperSize(length, width, paperLength, paperWidth)) {
                return new PricingListVo(100, "出纸张尺寸限制"); // 返回空列表表示不适用
            }

            // 计算一张纸能容纳的最大金属标数量
            double maxItemsPerSheet = calculateMaxItemsPerSheet(length, width, paperLength, paperWidth);

            // 计算所需纸张数量
            requiredSheets = calculateRequiredSheets(count, number, maxItemsPerSheet);
            // 计算颜色附加费
            if (!"银色".equals(dto.getTcolor())) {
                colorPrice = 1;
            }
            // 设置起步价
            minPrice = 40;
            if ("单色".equals(dto.getCraftMo())) {
                if (requiredSheets == 1) {
                    price = colorPrice == 1 ? 45 : 40;
                } else if (requiredSheets == 2) {
                    price = colorPrice == 1 ? 55 : 50;
                } else if (requiredSheets == 3) {
                    price = colorPrice == 1 ? 65 : 55;
                } else if (requiredSheets == 4) {
                    price = colorPrice == 1 ? 70 : 60;
                } else if (requiredSheets == 5) {
                    price = colorPrice == 1 ? 75 : 65;
                } else if (requiredSheets == 6) {
                    price = colorPrice == 1 ? 80 : 70;
                } else {
                    price = colorPrice == 1 ? 80 + (requiredSheets - 6) * 9 : 70 + (requiredSheets - 6) * 8;
                }
            }
            if ("双色".equals(dto.getCraftMo())) {
                minPrice = 60;
                moreColorPrice = 30;

            }
            if ("彩色(三色)".equals(dto.getCraftMo())) {
                minPrice = 70;
                moreColorPrice = 40;

            }
            if (price == 0) {
                price = Math.ceil(minPrice + moreColorPrice * (requiredSheets - 1));
            }
        }
        // 创建产品对象并添加到结果列表
        ProductVo pro = new ProductVo();
        pro.setPrice(price);
        pro.setCount(count);
        pro.setWeight(BigDecimal.valueOf(requiredSheets / 30).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
        resultList.add(pro);
        return new PricingListVo(resultList);
    }

    /**
     * 将给定的尺寸转换为毫米，并在每边增加3毫米的边距
     *
     * @param size 需要转换的原始尺寸
     * @return 转换后的尺寸，单位为毫米，包括边距
     */
    private double convertToMillimetersWithMargin(double size) {
        return size * 10 + 2 * 1.5; // 换成毫米，每边+3
    }

    /**
     * 检查给定的长度和宽度是否在纸张尺寸范围内
     *
     * @param length      需要检查的长度
     * @param width       需要检查的宽度
     * @param paperLength 纸张的长度
     * @param paperWidth  纸张的宽度
     * @return 如果给定尺寸在纸张尺寸范围内，则返回true；否则返回false
     */
    private boolean isWithinPaperSize(double length, double width, double paperLength, double paperWidth) {
        return (length <= paperLength && width <= paperWidth) || (length <= paperWidth && width <= paperLength);
    }

    /**
     * 计算每张纸最多可以容纳的物品数量
     *
     * @param length      物品的长度
     * @param width       物品的宽度
     * @param paperLength 纸张的长度
     * @param paperWidth  纸张的宽度
     * @return 每张纸最多可以容纳的物品数量
     */
    private double calculateMaxItemsPerSheet(double length, double width, double paperLength, double paperWidth) {
        return Math.max(
                Math.floor(paperLength / length) * Math.floor(paperWidth / width),
                Math.floor(paperLength / width) * Math.floor(paperWidth / length)
        );
    }

    /**
     * 计算完成指定数量的物品所需的纸张数量
     *
     * @param count            物品的总数量
     * @param number           每个物品需要的数量
     * @param maxItemsPerSheet 每张纸最多可以容纳的物品数量
     * @return 完成指定数量的物品所需的纸张数量
     */
    private int calculateRequiredSheets(int count, int number, double maxItemsPerSheet) {
        return (int) Math.ceil((count * number) / maxItemsPerSheet);
    }
}
