package com.seeyon.apps.src_contract.lease.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class RentCalculationUtil {
	/**
	 * 缴费方式年
	 * @return
	 * @throws ParseException 
	 */
//	开始时间，结束时间，缴费期限，租金，月份，平米单价
	public List<Map<String ,Object>> paymentMethodYear(Map<String,Object> map) throws ParseException {
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
//    	两个日期相差多少月
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = map.get("startDate").toString();
		String endDate = map.get("endDate").toString();
    	String dateString = DateGapUtil.getYearMonthDayGap(startDate,endDate);
    	String[] dateStr = dateString.split(",");
    	int year =  Integer.parseInt(dateStr[0]);
    	Date start = sdf.parse(startDate);
    	Calendar startTime = Calendar.getInstance();
    	startTime.setTime(start);
    	String deadline = "";
		if(map.get("deadline")==null){
			deadline="0";
		}else {
			deadline = map.get("deadline").toString();
		}
    	String rent = map.get("rent").toString();
    	String acreage = map.get("acreage").toString();
    	for(int i = 0 ; i < year ; i ++) {
    		Map<String,Object> submap = new HashMap<String,Object>();
//    		获取合同开始结束时间
    		submap.put("开始时间", sdf.format(startTime.getTime()));
//    		计算缴费时限（缴费开始日期-支付期限）
    		Date deadlineDate= new Date(startTime.getTime().getTime()-(Long.parseLong(deadline)*24*60*60*1000)); 
    		submap.put("当期缴费时限", sdf.format(deadlineDate));
    		startTime.add(Calendar.YEAR, 1);
    		startTime.add(Calendar.DATE, -1);
    		int months = 0;
    		if(i!=year-1) {
    			submap.put("结束时间", sdf.format(startTime.getTime()));
//    		获取租金
    			months = DateGapUtil.getTotalMonthGap(submap.get("开始时间").toString(),submap.get("结束时间").toString());
    			submap.put("当期月数",  months);
    			submap.put("租金", rent);
//    			double unitPrice = Double.parseDouble(rent)/months/Double.parseDouble(acreage);
//    			submap.put("核单价", unitPrice);
    		}else {
    			submap.put("结束时间", endDate);
//    		计算租金
    			String ymd = DateGapUtil.getYearMonthDayGap(submap.get("开始时间").toString(),submap.get("结束时间").toString());
    			String[] ymds = ymd.split(",");
    			double mon = Integer.parseInt(ymds[0])*12+Integer.parseInt(ymds[1])+(Double.parseDouble(ymds[2])/30);
    			submap.put("当期月数",  mon);
    			double rentdou = Double.parseDouble(rent);
    			submap.put("租金", rentdou);
    			//double unitPrice =rentdou/12/Double.parseDouble(acreage);
    			//submap.put("核单价", unitPrice);
    		}
    		startTime.add(Calendar.DATE, 1);
//    		计算核单价
    		list.add(submap);
    	}
		return list;
	}
	
	/**
	 * 缴费方式月
	 * @return
	 * @throws ParseException 
	 */
	public List<Map<String ,Object>> paymentMethodMonth(Map<String,Object> map) throws ParseException {
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
//    	两个日期相差多少月
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = map.get("startDate").toString();
		String endDate = map.get("endDate").toString();
    	String dateString = DateGapUtil.getYearMonthDayGap(startDate,endDate);
    	String[] dateStrings = dateString.split(",");
    	int cyclesNum = Integer.parseInt(dateStrings[0])*12+Integer.parseInt(dateStrings[1]);
    	Date start = sdf.parse(startDate);
    	Calendar startTime = Calendar.getInstance();
    	startTime.setTime(start);
    	String deadline = "";
		if(map.get("deadline")==null){
			deadline="0";
		}else {
			deadline = map.get("deadline").toString();
		}
    	String rent = map.get("rent").toString();
    	String acreage = map.get("acreage").toString();
    	for(int i = 0 ; i < cyclesNum ; i ++) {
    		Map<String,Object> submap = new HashMap<String,Object>();
//    		获取合同开始结束时间
    		submap.put("开始时间", sdf.format(startTime.getTime()));
//    		计算缴费时限（缴费开始日期-支付期限）
    		Date deadlineDate= new Date(startTime.getTime().getTime()-(Long.parseLong(deadline)*24*60*60*1000)); 
    		submap.put("当期缴费时限", sdf.format(deadlineDate));
    		startTime.add(Calendar.MONTH, 1);
    		startTime.add(Calendar.DATE, -1);
    		int months = 0;
    		if(i!= cyclesNum-1) {
    			submap.put("结束时间", sdf.format(startTime.getTime()));
//    		获取租金
    			months = DateGapUtil.getTotalMonthGap(submap.get("开始时间").toString(),submap.get("结束时间").toString());
    			submap.put("当期月数",  months);
    			submap.put("租金", rent);
//    			double unitPrice = Double.parseDouble(rent)/months/Double.parseDouble(acreage);
//    			submap.put("核单价", unitPrice);
    		}else {
    			submap.put("结束时间", endDate);
//    			计算租金
    			String ymd = DateGapUtil.getYearMonthDayGap(submap.get("开始时间").toString(),submap.get("结束时间").toString());
    			String[] ymds = ymd.split(",");
    			double mon = Integer.parseInt(ymds[1])+(Double.parseDouble(ymds[2])/30);
    			submap.put("当期月数",  mon);
    			double rentdou = Double.parseDouble(rent);
    			submap.put("租金", rentdou);
//    			double unitPrice =rentdou/Double.parseDouble(acreage);
//    			submap.put("核单价", unitPrice);
    		}
    		startTime.add(Calendar.DATE, 1);
//    		计算核单价
    		list.add(submap);
    	}
		return list;
	}
	
	/**
	 * 缴费方式季
	 * @return
	 * @throws ParseException 
	 */
	public List<Map<String ,Object>> paymentMethodQuarter(Map<String,Object> map) throws ParseException {
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
//    	两个日期相差多少月
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = map.get("startDate").toString();
		String endDate = map.get("endDate").toString();
    	int dateString = DateGapUtil.getTotalMonthGap(startDate,endDate);
    	int cyclesNum = dateString/3;
    	Date start = sdf.parse(startDate);
    	Calendar startTime = Calendar.getInstance();
    	startTime.setTime(start);
    	String deadline = "";
		if(map.get("deadline")==null){
			deadline="0";
		}else {
			deadline = map.get("deadline").toString();
		}
    	String rent = map.get("rent").toString();
    	String acreage = map.get("acreage").toString();
    	for(int i = 0 ; i < cyclesNum ; i ++) {
    		Map<String,Object> submap = new HashMap<String,Object>();
//    		获取合同开始结束时间
    		submap.put("开始时间", sdf.format(startTime.getTime()));
//    		计算缴费时限（缴费开始日期-支付期限）
    		Date deadlineDate= new Date(startTime.getTime().getTime()-(Long.parseLong(deadline)*24*60*60*1000)); 
    		submap.put("当期缴费时限", sdf.format(deadlineDate));
    		startTime.add(Calendar.MONTH, 3);
    		startTime.add(Calendar.DATE, -1);
    		int months = 0;
    		if(i!=cyclesNum-1) {
    			submap.put("结束时间", sdf.format(startTime.getTime()));
//    		获取租金
    			months = DateGapUtil.getTotalMonthGap(submap.get("开始时间").toString(),submap.get("结束时间").toString());
    			submap.put("当期月数",  months);
    			submap.put("租金", rent);
//    			double unitPrice = Double.parseDouble(rent)/months/Double.parseDouble(acreage);
//    			submap.put("核单价", unitPrice);
    		}else {
    			submap.put("结束时间", endDate);
//    			计算租金
    			String ymd = DateGapUtil.getYearMonthDayGap(submap.get("开始时间").toString(),submap.get("结束时间").toString());
    			String[] ymds = ymd.split(",");
    			double mon = Integer.parseInt(ymds[1])+(Double.parseDouble(ymds[2])/30);
    			submap.put("当期月数",  mon);
    			double rentdou = Double.parseDouble(rent);
    			submap.put("租金", rentdou);
//    			double unitPrice =rentdou/3/Double.parseDouble(acreage);
//    			submap.put("核单价", unitPrice);
    		}
    		startTime.add(Calendar.DATE, 1);
//    		计算核单价
    		list.add(submap);
    	}
		return list;
	}
	
	/**
	 * 缴费方式半年
	 * @return
	 * @throws ParseException 
	 */
	public List<Map<String ,Object>> paymentMethodSemiannua(Map<String,Object> map) throws ParseException {
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
//    	两个日期相差多少月
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = map.get("startDate").toString();
		String endDate = map.get("endDate").toString();
    	int dateString = DateGapUtil.getTotalMonthGap(startDate,endDate);
    	int cyclesNum = dateString/6;
    	Date start = sdf.parse(startDate);
    	Calendar startTime = Calendar.getInstance();
    	startTime.setTime(start);
    	String deadline = "";
		if(map.get("deadline")==null){
			deadline="0";
		}else {
			deadline = map.get("deadline").toString();
		}
    	String rent = map.get("rent").toString();
    	String acreage = map.get("acreage").toString();
    	for(int i = 0 ; i < cyclesNum ; i ++) {
    		Map<String,Object> submap = new HashMap<String,Object>();
//    		获取合同开始结束时间
    		submap.put("开始时间", sdf.format(startTime.getTime()));
//    		计算缴费时限（缴费开始日期-支付期限）
    		Date deadlineDate= new Date(startTime.getTime().getTime()-(Long.parseLong(deadline)*24*60*60*1000)); 
    		submap.put("当期缴费时限", sdf.format(deadlineDate));
    		startTime.add(Calendar.MONTH, 6);
    		startTime.add(Calendar.DATE, -1);
    		int months = 0;
    		if(i!=cyclesNum-1) {
    			submap.put("结束时间", sdf.format(startTime.getTime()));
//    		获取租金
    			months = DateGapUtil.getTotalMonthGap(submap.get("开始时间").toString(),submap.get("结束时间").toString());
    			submap.put("当期月数",  months);
    			submap.put("租金", rent);
//    			double unitPrice = Double.parseDouble(rent)/months/Double.parseDouble(acreage);
//    			submap.put("核单价", unitPrice);
    		}else {
    			submap.put("结束时间", endDate);
//    			计算租金
    			String ymd = DateGapUtil.getYearMonthDayGap(submap.get("开始时间").toString(),submap.get("结束时间").toString());
    			String[] ymds = ymd.split(",");
    			double mon = Integer.parseInt(ymds[0])*12+Integer.parseInt(ymds[1])+(Double.parseDouble(ymds[2])/30);
    			submap.put("当期月数",  mon);
    			double rentdou = Double.parseDouble(rent);
    			submap.put("租金", rentdou);
//    			double unitPrice =rentdou/6/Double.parseDouble(acreage);
//    			submap.put("核单价", unitPrice);
    		}
    		startTime.add(Calendar.DATE, 1);
//    		计算核单价
    		list.add(submap);
    	}
		return list;
	}
	
	/**
	 * 缴费方式一次性
	 * @return
	 * @throws ParseException 
	 */
	public List<Map<String ,Object>> paymentMethodAll(Map<String,Object> map) throws ParseException {
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		Map<String ,Object> submap = new HashMap<String,Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//		获取合同开始结束时间
		String startDate = map.get("startDate").toString();
		String endDate = map.get("endDate").toString();
		submap.put("开始时间", startDate);
		submap.put("结束时间", endDate);
//		计算缴费时限（缴费开始日期-支付期限）
		String deadline = "";
		if(map.get("deadline")==null){
			deadline="0";
		}else {
			deadline = map.get("deadline").toString();
		}
		Date deadlineDate= new Date(sdf.parse(startDate).getTime()-(Long.parseLong(deadline)*24*60*60*1000)); 
		submap.put("当期缴费时限", sdf.format(deadlineDate));
//		获取租金
		String rent = map.get("rent").toString();
		submap.put("租金", rent);
		int months = DateGapUtil.getTotalMonthGap(startDate, endDate);
		submap.put("当期月数", months);
//		计算核单价
		String acreage = map.get("acreage").toString();
		double unitPrice = Double.parseDouble(rent)/months/Double.parseDouble(acreage);
		submap.put("核单价", unitPrice);
		list.add(submap);
		return list;
	}
	
	
	
	public List<Map<String ,Object>> handleCollectionSub(JSONArray jsonArray) {
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		JSONObject jsonObjetc = jsonArray.getJSONObject(0);
		JSONArray plans = jsonObjetc.getJSONArray("plans");
		System.out.println("plans:"+plans);
		for(int i = 0 ; i < plans.size() ; i++) {
			System.out.println(i);
			JSONObject plan = plans.getJSONObject(i);
			Map<String,Object> submap = new HashMap<String,Object>();
			submap.put("开始时间", plan.get("planStartDate"));
			submap.put("结束时间", plan.get("planEndDate"));
			submap.put("当期缴费时限", plan.get("currentPaymentTime"));
			submap.put("租费合计", plan.get("planAmount"));
			submap.put("当期月数", plan.get("planBetweenDate"));
			submap.put("租金", plan.get("rent"));
			submap.put("管理服务费", plan.get("manageServiceFee"));
			submap.put("市场管理费", plan.get("marketFee"));
			list.add(submap);
		}
		return list;
	}
	
	
	public List<Map<String ,Object>> updateCollectionSub(List<Map<String,Object>> historyPayments,JSONArray jsonArray) {
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		for (Map<String, Object> historyPayment : historyPayments) {
			Map<String,Object> submap = new HashMap<String,Object>();
			submap.put("开始时间", historyPayment.get("STARTDATE"));
			submap.put("结束时间", historyPayment.get("ENDDATE"));
			submap.put("当期缴费时限", historyPayment.get("PLANENDDATE"));
			submap.put("租费合计", historyPayment.get("PLANTOTALCOST"));
			String startDate = historyPayment.get("STARTDATE").toString();
			String endDate = historyPayment.get("ENDDATE").toString();
			LocalDate date1 = LocalDate.of(Integer.parseInt(startDate.substring(0,4)),
					Integer.parseInt(startDate.substring(5,7)),
					Integer.parseInt(startDate.substring(8,10)));
	        LocalDate date2 = LocalDate.of(Integer.parseInt(endDate.substring(0,4)),
					Integer.parseInt(endDate.substring(5,7)),
					Integer.parseInt(endDate.substring(8,10)));
	        LocalDate nextDay = date2.plusDays(1);
	        Period period = Period.between(date1, nextDay);
	        int months = period.getMonths();
			submap.put("当期月数", months);
			submap.put("租金", historyPayment.get("DETAILRENT"));
			submap.put("管理服务费", historyPayment.get("DETAILMANAGESERVICEFEE"));
			submap.put("市场管理费", historyPayment.get("DETAILMARKETFEE"));
			
			submap.put("减免租金", historyPayment.get("DERATERENT"));
			submap.put("减免管理服务费", historyPayment.get("DERATEMANAGESERVICEFEE"));
			submap.put("减免市场管理费", historyPayment.get("DERATEMARKETFEE"));
			
			submap.put("递增租金", historyPayment.get("INCREASERENT"));
			submap.put("递增管理服务费", historyPayment.get("INCREASEMANAGESERVICEFEE"));
			submap.put("递增市场管理费", historyPayment.get("INCREASEMARKETFEE"));
			
			submap.put("已收金额", historyPayment.get("RECEIVETOTALCOST"));
			submap.put("剩余未收金额", historyPayment.get("REMAINTOTALCOST"));
			submap.put("是否历史账期", "1");
			list.add(submap);
		}
		JSONObject jsonObjetc = jsonArray.getJSONObject(0);
		JSONArray plans = jsonObjetc.getJSONArray("plans");
		System.out.println("plans:"+plans);
		for(int i = 0 ; i < plans.size() ; i++) {
			System.out.println(i);
			JSONObject plan = plans.getJSONObject(i);
			Map<String,Object> submap = new HashMap<String,Object>();
			submap.put("开始时间", plan.get("planStartDate"));
			submap.put("结束时间", plan.get("planEndDate"));
			submap.put("当期缴费时限", plan.get("currentPaymentTime"));
			submap.put("租费合计", plan.get("planAmount"));
			submap.put("当期月数", plan.get("planBetweenDate"));
			submap.put("租金", plan.get("rent"));
			submap.put("管理服务费", plan.get("manageServiceFee"));
			submap.put("市场管理费", plan.get("marketFee"));
			submap.put("已收金额", "0");
			submap.put("是否历史账期", "0");
			list.add(submap);
		}
		return list;
	}
	
}
