package org.springblade.modules.order.utils;

import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.order.pojo.form.ComplaintReply;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class WebPageFunUtils {

	/**
	 * 获取工单的详细过程信息
	 *
	 * @param pros
	 *            as:2017-05-04 21:40:32#@工单接入@#@2017-05-05 10:01:40#@工单进行派工
	 * @return
	 */
	public static ArrayList<Target> getOrderProcess(String pros) {
		try {
			ArrayList<Target> list = Lists.newArrayList();
			if (StringUtils.isNotBlank(pros)) {
				Gson gson = new Gson();
				list = gson.fromJson(pros, new TypeToken<ArrayList<Target>>() {
				}.getType());
			}
			// 升序 按时间升序排序
			Collections.sort(list, new Comparator<Target>() {
				public int compare(Target o1, Target o2) {
					return o1.getD().compareTo(o2.getD());
				}
			});
			return list;
		} catch (Exception ex) {
			return new ArrayList<>();
		}
	}


	/**
	 * g跟踪信息
	 */
	public static ArrayList<ComplaintReply> getFittingProcess(String pros) {
		ArrayList<ComplaintReply> list = Lists.newArrayList();
		if (StringUtils.isNotBlank(pros)) {
			Gson gson = new Gson();
			list = gson.fromJson(pros, new TypeToken<ArrayList<ComplaintReply>>() {
			}.getType());
		}
		// 降序 按时间降序排序
		Collections.sort(list, new Comparator<ComplaintReply>() {
			public int compare(ComplaintReply o1, ComplaintReply o2) {
				return -o1.getT().compareTo(o2.getT());
			}
		});
		return list;
	}

	public static ArrayList<ComplaintReply> getFittingProcessWithoutSort(String pros) {
		ArrayList<ComplaintReply> list = Lists.newArrayList();
		if (StringUtils.isNotBlank(pros)) {
			Gson gson = new Gson();
			list = gson.fromJson(pros, new TypeToken<ArrayList<ComplaintReply>>() {
			}.getType());
		}
		return list;
	}

	public static ArrayList<Target> getProcessWithoutSort(String pros) {
		ArrayList<Target> list = Lists.newArrayList();
		if (StringUtils.isNotBlank(pros)) {
			Gson gson = new Gson();
			list = gson.fromJson(pros, new TypeToken<ArrayList<Target>>() {
			}.getType());
		}
		return list;
	}

	public static ArrayList<Target> getOrderProcessDesc(String pros) {
		try {
			ArrayList<Target> list = Lists.newArrayList();
			if (StringUtils.isNotBlank(pros)) {
				Gson gson = new Gson();
				list = gson.fromJson(pros, new TypeToken<ArrayList<Target>>() {
				}.getType());
			}
			// 升序 按时间升序排序
			Collections.sort(list, new Comparator<Target>() {
				public int compare(Target o1, Target o2) {
					return o2.getD().compareTo(o1.getD());
				}
			});
			return list;
		} catch (Exception ex) {
			return new ArrayList<>();
		}
	}


	public static String appendProcessDetails(List<ComplaintReply> ta, String existsDetail) {
		for (ComplaintReply t : ta) {
			if (t == null || StringUtil.isBlank(t.getC())) {
				throw new IllegalArgumentException();
			}
		}

		try {
			ArrayList<ComplaintReply> list = getFittingProcessWithoutSort(existsDetail);// 先前已经存在的过程信息
			list.addAll(ta);
			Collections.sort(list, new Comparator<ComplaintReply>() {
				public int compare(ComplaintReply o1, ComplaintReply o2) {
					return o1.getT().compareTo(o2.getT());
				}
			});
			Gson gson = new Gson();
			return gson.toJson(list, new TypeToken<ArrayList<ComplaintReply>>() {
			}.getType());
		} catch (Exception ex) {
			return existsDetail;
		}
	}

	/**
	 * 拼接过程信息
	 */
	public static String appendOrderProcessDetail(Target ta, String existsDetail) {
		if (ta == null || StringUtil.isBlank(ta.getC())) {
			throw new IllegalArgumentException();
		}
		try {
			ArrayList<Target> list = getProcessWithoutSort(existsDetail);// 先前已经存在的过程信息
			list.add(ta);
			Collections.sort(list, new Comparator<Target>() {
				public int compare(Target o1, Target o2) {
					return o1.getD().compareTo(o2.getD());
				}
			});
			Gson gson = new Gson();
			return gson.toJson(list, new TypeToken<ArrayList<Target>>() {
			}.getType());
		} catch (Exception ex) {
			return existsDetail;
		}
	}

	/**
	 * 拼接过程信息
	 */
	public static String appendProcessDetail(ComplaintReply ta, String existsDetail) {
		if (ta == null || StringUtil.isBlank(ta.getC())) {
			throw new IllegalArgumentException();
		}
		try {
			ArrayList<ComplaintReply> list = getFittingProcessWithoutSort(existsDetail);// 先前已经存在的过程信息
			list.add(ta);
			Collections.sort(list, new Comparator<ComplaintReply>() {
				public int compare(ComplaintReply o1, ComplaintReply o2) {
					return o1.getT().compareTo(o2.getT());
				}
			});
			Gson gson = new Gson();
			return gson.toJson(list, new TypeToken<ArrayList<ComplaintReply>>() {
			}.getType());
		} catch (Exception ex) {
			return existsDetail;
		}
	}

	/**
	 * 获取总费用
	 */
	public static String getOrderTotalFee(int len, Object... fees) {
		Double totalFee = 0d;
		if (fees != null) {
			for (Object fee : fees) {
				totalFee += Double.valueOf(String.valueOf(fee));
			}
		}
		if (totalFee > 0) {
			return String.valueOf(totalFee);
		}
		return "0";
	}

	public static Double getTotalFee(Object... fees) {
		Double result = 0d;
		if (fees != null) {
			for (Object fee : fees) {
				Double item = 0d;
				try {
					item = Double.valueOf(String.valueOf(fee));
				} catch (Exception e) {
					//
				}
				result += item;
			}
		}
		return result;
	}

	public static String getOrderTotalFee(Object fee1, Object fee2, Object fee3) {
		return getOrderTotalFee(3, fee1, fee2, fee3);
	}

	public static String getOrderTotalFeeNew(Object fee1, Object fee2, Object fee3, Object fee4, Object fee5, Object fee6, Object fee7, Object fee8, Object fee9) {
		return getOrderTotalFeeNew(8, fee1, fee2, fee3, fee4, fee5, fee6, fee7, fee8, fee9);
	}

	public static String getOrderTotalFeeNew(int len, Object... fees) {
		Double totalFee = 0d;
		if (fees != null) {
			int i = 0;
			for (Object fee : fees) {
				if (fee == null) {
					fee = 0;
				}
				if (i == 0) {
					totalFee -= Double.valueOf(String.valueOf(fee));
				}else {
					totalFee += Double.valueOf(String.valueOf(fee));
				}
				i++;
			}
		}
		if (totalFee > 0) {
			DecimalFormat df = new DecimalFormat("#.00");
			return String.valueOf(df.format(totalFee));
		}
		return "0";
	}
}
