package org.ta.pddserver.utils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

public class PddOverseaPriceTool {

    // 首重重量
    private static int initialWeight = 1000;
    // 基础上限重量
    private static int baseMaxWeight = 20000;
    // 最大重量
    private static int maxWeight = 300000;

    // 普货首重价格
    private static int gInitalPrice = 1850;

    // 特货首重价格
    private static int sInitalPrice = 2800;

    //超重计费标准
    private static int excessWeight = 500;

    //超重价格
    private static int excessPrice = 175;

    //标椎单边长度
    private static int baseSingleLength = 100;

    //标准三边之和
    private static int baseSumLength = 160;

    //最大单边长度
    private static int maxSingleLength = 250;

    //最大三边之和
    private static int maxSumLength = 250;

    public static int calculateWeight(int length, int width, int height, int weight) {
        int max = length + width + height;
        if (length > maxSingleLength || width > maxSingleLength || height > maxSingleLength || max > maxSumLength) {
            //三边各超100或者和超160
            return -1;
        }
        if (weight > maxWeight) {
            //超重无法计算
            return -1;
        }
        int compWeight = weight;
        if (weight > baseMaxWeight || length > baseSingleLength || width > baseSingleLength || height > baseSingleLength || max > baseSumLength) {
            //三边各超100或者和超160
            int newWeight = length * width * height / 6000 + 1;
            compWeight = Math.max(compWeight, newWeight);
        }
        return compWeight;
    }

    public static long calculatePrice(int exchange, int weight, String type) {
        int price = 0;
        if (exchange == 2) {
            // 改派
            price = 2400;
        }
        if (exchange == 3) {
            price = 1200;
        }

        int excess = 0;

        // 判断重量是否超重
        //分析重量
        if (weight > initialWeight) {
            //计算超出重量
            weight = weight - initialWeight;
            excess = excessPrice * ((int) Math.ceil(1.0 * weight / excessWeight));
        }

        //计算价格
        if (type.equals("NORMAL")) {
            //普货
            price = price + gInitalPrice + excess;

        } else {
            price = price + sInitalPrice + excess;
        }
        return price;

    }

    public static long calculateRent(String enterTime) {
        int rent = 0;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date1 = sdf.parse(enterTime);
            Date date2 = new Date();
            // 将Date转换为LocalDate
            LocalDate localDate1 = date1.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate localDate2 = date2.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

            // 计算两个日期之间的天数差异
            long daysBetween = Math.abs(java.time.temporal.ChronoUnit.DAYS.between(localDate2, localDate1));
            if (daysBetween > 180) {
                rent = (int) daysBetween - 180;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rent;
    }


    /*
     * 根据类型、重量（g） 计算运费
     * */
    /*
    public static PddJYPriceTool.FeeResult FreightCostCalculation(String type, int length, int width, int height, int weight, int exchange, String enterTime) {

        int max = length + width + height;
        if (length > maxSingleLength || width > maxSingleLength || height > maxSingleLength || max > maxSumLength) {
            //三边各超100或者和超160
            return null;
        }
        if (weight > maxWeight) {
            //超重无法计算
            return null;
        }

        PddJYPriceTool.FeeResult result = new PddJYPriceTool.FeeResult();

        int price = 0;
        if (exchange == 2) {
            // 改派
            price = 2400;
        }
        if (exchange == 3) {
            price = 1200;
        }

        int compWeight = weight;
        if (weight > baseMaxWeight || length > baseSingleLength || width > baseSingleLength || height > baseSingleLength || max > baseSumLength) {
            //三边各超100或者和超160
            int newWeight = length * width * height / 6000 + 1;
            compWeight = Math.max(compWeight, newWeight);
        }
        result.setWeight(compWeight);
        int excess = 0;

        // 判断重量是否超重
        //分析重量
        if (compWeight > initialWeight) {
            //计算超出重量
            compWeight = compWeight - initialWeight;
            excess = excessPrice * ((int) Math.ceil(1.0 * compWeight / excessWeight));
        }

        //计算价格
        if (type.equals("NORMAL")) {
            //普货
            price = price + gInitalPrice + excess;

        } else {
            price = price + sInitalPrice + excess;
        }

        int rent = 0;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date1 = sdf.parse(enterTime);
            Date date2 = new Date();
            // 将Date转换为LocalDate
            LocalDate localDate1 = date1.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate localDate2 = date2.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

            // 计算两个日期之间的天数差异
            long daysBetween = Math.abs(java.time.temporal.ChronoUnit.DAYS.between(localDate2, localDate1));
            if (daysBetween > 180) {
                rent = (int) daysBetween - 180;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        result.setRentFee(rent * 100);
        result.setFreightFee(price);
        return result;
    }

     */
}
