package com.quoted.system.utils;

import com.quoted.system.domain.vo.ProductPriceDto;
import com.quoted.system.domain.vo.ProductPriceRequestVo;
import org.apache.commons.math3.stat.descriptive.summary.Product;

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

public class StickerUtils {

    public static List<ProductPriceDto> getPrice(ProductPriceRequestVo product) {

        List<ProductPriceDto> list = new ArrayList<>();
        Double length = product.getLength();
        Double width = product.getWidth();
        Integer count = product.getCount();
        Integer number = product.getNumber();
        List<String> craft_list = PriceCommonUtils.getCraftList(product);
        double area = length * width / 10000;
        switch (product.getMaterial()) {
            case "铜板纸不干胶":
                // 带1厘米的尺寸，都变成2
                if (1 < length && length < 2) {
                    length = Math.ceil(length);
                }
                if (1 < width && width < 2) {
                    width = Math.ceil(width);
                }
                // 小数部分，四舍五入
                length = (double) Math.round(length);
                width = (double) Math.round(width);
                // 尺寸小数部分四舍五入后的面积
                double roundArea = length * width / 10000;
                // 一个名片位内做特价:75折，价格在前端写死
                double[] priceArr = null;
                int[] counts = {500, 1000, 2000, 3000, 5000, 10000, 20000, 30000, 40000, 50000};
                double areas[] = {0.004, 0.006, 0.008, 0.01, 0.012, 0.015, 0.017, 0.018, 0.02, 0.03, 0.04, 0.06, 0.1, 0.2, 0.3};
                //覆膜是否要加价
                boolean isMo = true;
                int MaxCount = 0;
                if (length == 1 && width == 1) {
                    priceArr = new double[]{30, 35, 50, 55, 100, 180, 270, 370, 470, 570};
                } else if ((length == 2 && width == 1) || (length == 1 && width == 2)) {
                    priceArr = new double[]{35, 40, 55, 60, 110, 190, 280, 380, 480, 580};
                } else if ((length > 2 && length <= 5 && width == 1) || (width > 2 && width <= 5 && length == 1)) {
                    priceArr = new double[]{35, 40, 55, 70, 120, 210, 308, 435, 555, 683};
                } else if ((length > 1 && length <= 3 && width == 2) || (width > 1 && width <= 3 && length == 2)) {
                    priceArr = new double[]{35, 40, 55, 70, 120, 210, 308, 435, 555, 683};
                } else if ((length > 3 && length <= 5 && width == 2) || (width > 3 && width <= 5 && length == 2)) {
                    priceArr = new double[]{35, 40, 55, 70, 120, 220, 348, 492, 642, 798};
                } else if ((length >= 3 && length <= 5 && width == 3) || (length == 3 && width <= 5 && width >= 3)) {
                    priceArr = new double[]{35, 40, 55, 70, 120, 220, 348, 492, 642, 798};
                } else if (length == 4 && width == 4) {
                    priceArr = new double[]{35, 40, 55, 70, 120, 220, 348, 492, 642, 798};
                } else if ((length == 5 && width == 4) || (length == 4 && width == 5)) {
                    priceArr = new double[]{35, 40, 55, 70, 150, 240, 420, 600, 790, 980};
                } else if (((length <= 9 && width <= 5.4) || (length <= 5.4 && width <= 9))) {
                    counts = new int[]{500, 1000, 2000, 3000, 5000, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000};
                    priceArr = new double[]{60, 70, 95, 125, 175, 270, 510, 800, 1000, 1200, 1400, 1600, 1800, 2000, 2200};
                    isMo = false;
                } else if (area <= 0.003) {
                    counts = new int[]{500, 1000, 2000, 3000, 5000, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000};
                    priceArr = new double[]{65, 75, 100, 130, 180, 275, 515, 805, 1005, 1205, 1405, 1605, 1805, 2005, 2205};
                    isMo = false;
                    MaxCount = 40000;
                } else {
                    counts = new int[]{500, 1000, 2000, 3000, 5000, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000};
                    double basePrice = 0;
                    if (area <= 0.004) {
                        basePrice = 7.5;
                    } else if (area <= 0.006) {
                        basePrice = 6.5;
                    } else if (area <= 0.2) {
                        basePrice = 5.5;
                    } else if (area <= 0.3) {
                        basePrice = 5;
                    } else if (area > 0.3) {
                        basePrice = 4.5;
                    }

                    ProductPriceDto pro = new ProductPriceDto();
                    if (count > counts[counts.length - 1]) {
                        pro.setCount(count);
                        pro.setPrice(Math.ceil(area * count * basePrice));
                        list.add(pro);
                    }
                    for (int i = 0; i < counts.length; i++) {
                        if (counts[i] < count || list.size() > 3) {
                            continue;
                        }

                        double[] arrPrice = new double[16];
                        if (counts[i] == 500) {
                            arrPrice = new double[]{37, 33, 30, 26, 25, 21, 19, 18, 17, 19, 17, 12.8, 12.5, 12, 11.5, 11};
                        } else if (counts[i] == 1000) {
                            arrPrice = new double[]{21, 19, 17, 17, 15, 14, 14, 12, 12, 12.5, 13, 9, 9, 8, 8, 7.8};
                        } else if (counts[i] == 2000) {
                            arrPrice = new double[]{14, 13, 13, 12, 11, 11, 10, 10, 9, 10, 11, 7.8, 7.5, 7.5, 7.2, 7};
                        } else if (counts[i] == 3000) {
                            arrPrice = new double[]{12, 11, 11, 10, 9, 9.5, 9, 8, 8, 9, 10, 6.8, 6.5, 6.5, 6, 6.5};
                        } else if (counts[i] == 5000) {
                            arrPrice = new double[]{10, 9.5, 8, 7.8, 8.5, 9, 8.3, 7, 8, 6.5, 8, 6, 6, 6, 5.5, 5};
                        } else if (counts[i] == 10000) {
                            arrPrice = new double[]{8, 7.5, 6.5, 6.5, 7.6, 8, 7.3, 6, 7.5, 6, 6, 6, 6, 6, 5, 5};
                        } else if (counts[i] == 20000) {
                            arrPrice = new double[]{7.6, 6.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5, 4.5};
                        } else if (counts[i] == 30000) {
                            arrPrice = new double[]{7.6, 6.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5, 4.5};
                        } else if (counts[i] == 40000) {
                            arrPrice = new double[]{7.6, 6.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5, 4.5};
                        } else if (counts[i] == 50000) {
                            arrPrice = new double[]{7.5, 6.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5, 4.5};
                        } else if (counts[i] == 60000) {
                            arrPrice = new double[]{7.5, 6.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5, 4.5};
                        } else if (counts[i] == 70000) {
                            arrPrice = new double[]{7.5, 6.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5, 4.5};
                        } else if (counts[i] == 80000) {
                            arrPrice = new double[]{7.5, 6.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5, 4.5};
                        } else if (counts[i] == 90000) {
                            arrPrice = new double[]{7.5, 6.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5, 4.5};
                        } else if (counts[i] == 100000) {
                            arrPrice = new double[]{7.5, 6.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5, 4.5};
                        }

                        double pirce = getPriceInArea(counts[i], area, arrPrice, areas);
                        pro = new ProductPriceDto();
                        pro.setCount(counts[i]);
                        pro.setPrice(Math.ceil(pirce));

                        list.add(pro);
                        break;
                    }
                }
                ProductPriceDto pro = null;

                if (MaxCount == 0) {

                    for (int i = 0; i < counts.length; i++) {
                        if (counts[i] < count || list.size() > 3) {
                            continue;
                        }
                        pro = new ProductPriceDto();
                        pro.setCount(counts[i]);
                        double price = priceArr[i];
                        if (craft_list.contains("覆哑膜") && isMo) {
                            price = Math.ceil(price * 1.2);
                        }
                        pro.setPrice(price);
                        list.add(pro);
                    }
                } else {
                    pro = new ProductPriceDto();
                    pro.setCount(count);
                    double price = Math.ceil((Math.ceil(count / 10000.0) - 4) * 150 + 720);

                    pro.setPrice(price);
                    list.add(pro);
                }
                break;
            default:
                break;
        }


        return list;
    }

    // 判断小尺寸是否大于大尺寸价格
    private static double getPriceInArea(int count, Double area, double[] priceArr, double[] areas) {
        double lastPrice = 0, price = 0;
        if (area > areas[areas.length - 1]) {
            price = area * count * priceArr[areas.length - 1];
            lastPrice = area * count * priceArr[areas.length - 2];
            if (lastPrice > price) {
                price = lastPrice;
            }
        }
        for (int i = 0; i < areas.length; i++) {
            if (areas[i] < area) {
                continue;
            }
            double lastArea = 0;
            if (i > 0) {
                lastArea = areas[i - 1];
                lastPrice = lastArea * count * priceArr[i - 1];
            }
            price = area * count * priceArr[i];
            if (lastPrice > price) {
                price = lastPrice;
            }
            break;
        }

        return price;
    }
}
