package com.desfate.chart.ui.old.OTC.datas;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;


//{"endtime":"11:00","reckontime":"04:00","closeweekday":"2","reckondelay":"10","starttime":"06:00","markettypeid":"101","openweekday":"1","endweekday":"1","tradeday":"2016-07-04","runstep":"3","startweekday":"1","opentime":"06:00"}

/**
 * 结算计划
 *
 * @author yongzq
 */
public class SettlementPlan {
    private Integer marketTypeID; // 市场类型id
    private Date openTime; // 开始时间
    private Date reckonTime; // 结束时间
    private Date tradeDate;
    private List<StartEnd> list;

    public Date getTradeDate() {
        return tradeDate;
    }

    public void setTradeDate(Date tradeDate) {
        this.tradeDate = tradeDate;
    }

    public Integer getMarketTypeID() {
        return marketTypeID;
    }

    public void setMarketTypeID(Integer marketTypeID) {
        this.marketTypeID = marketTypeID;
    }

    public Date getOpenTime() {
        return openTime;
    }

    public void setOpenTime(Date openTime) {
        this.openTime = openTime;
    }

    public Date getReckonTime() {
        return reckonTime;
    }

    public void setReckonTime(Date reckonTime) {
        this.reckonTime = reckonTime;
    }

    public List<StartEnd> getList() {
        if (list == null) {
            list = new ArrayList<StartEnd>();
        }
        return list;
    }

    public void setList(List<StartEnd> list) {
        this.list = list;
    }

    /**
     * 是否在结算计划时间内
     *
     * @param timeMillions utc秒数
     * @return
     */
    public boolean inPlan(long timeMillions) {
        Date time = new Date(timeMillions);
        return this.inPlan(time);
    }

    /**
     * 是否在结算计划中
     *
     * @param time 时间参数
     * @return
     */
    public boolean inPlan(Date time) {
        if (this.getList() == null || this.getList().isEmpty()) {
            return false;
        }
        for (StartEnd startEnd : list) {
            if (!time.before(startEnd.getStartTime()) && !time.after(startEnd.getEndTime())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 将java的周几转换为mtp定义的枚举值
     *
     * @param javaWeekDay
     * @return
     */
    private static int toMTPWeekDay(int javaWeekDay) {
        switch (javaWeekDay) {
            case Calendar.MONDAY:
                return 1;
            case Calendar.TUESDAY:
                return 2;
            case Calendar.WEDNESDAY:
                return 3;
            case Calendar.THURSDAY:
                return 4;
            case Calendar.FRIDAY:
                return 5;
            case Calendar.SATURDAY:
                return 6;
            case Calendar.SUNDAY:
                return 7;
            default:
                return -1;
        }
    }

    /**
     * 将丑陋的结算计划列表转换为漂亮的结算计划对象
     *
     * @param list
     * @return
     */
    public static SettlementPlan toSettlementPlan(List<SettlementPlanData> list) {
        if (list == null || list.isEmpty()) {
            return null;
        }
        SettlementPlan settlementPlan = new SettlementPlan();
        settlementPlan.marketTypeID = Integer.parseInt(list.get(0).getMarkettypeid());
        String tradeDayString = list.get(0).getTradeday();

        //2016-07-04
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        Date tradeDay = null;
        try {
            tradeDay = dateFormat.parse(tradeDayString);
            settlementPlan.setTradeDate(tradeDay);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        Calendar tradeDayCalendar = Calendar.getInstance();
        tradeDayCalendar.setTime(tradeDay);
        int weekDay = toMTPWeekDay(tradeDayCalendar.get(Calendar.DAY_OF_WEEK));
        // ok 得到了交易日，开始干活
        for (SettlementPlanData item : list) {
            try {
                if (item.getEndweekday() == null || item.getEndweekday().equals("null") || list.size() == 1) {
                    // 说明是一段的情况
                    StartEnd se = new StartEnd();
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(tradeDay);
                    calendar.add(Calendar.DATE, Integer.parseInt(item.getOpenweekday()) - weekDay);
                    Date openDate = calendar.getTime();
                    String openDateString = dateFormat.format(openDate);
                    calendar.setTime(tradeDay);
                    calendar.add(Calendar.DATE, Integer.parseInt(item.getCloseweekday()) - weekDay);
                    Date reckonDate = calendar.getTime();
                    String reckonDateString = dateFormat.format(reckonDate);
                    if (item.getOpentime() != null && !item.getOpentime().equals("null")) {
                        String openTimeString = openDateString + " " + item.getOpentime();
                        Date openTime = dateFormat2.parse(openTimeString);
                        if (settlementPlan.getOpenTime() == null) {
                            settlementPlan.setOpenTime(openTime);
                        }
                    }
                    if (item.getReckontime() != null && !item.getReckontime().equals("null")) {
                        String reckonTimeString = reckonDateString + " " + item.getReckontime();
                        Date endTime = dateFormat2.parse(reckonTimeString);
                        if (settlementPlan.getReckonTime() == null) {
                            settlementPlan.setReckonTime(endTime);
                        }
                    }
                    if (item.getStartweekday() != null && !item.getStartweekday().equals("null")) {
                        calendar.setTime(tradeDay);
                        calendar.add(Calendar.DATE, Integer.parseInt(item.getStartweekday()) - weekDay);
                        Date startDate = calendar.getTime();
                        String startDateString = dateFormat.format(startDate);
                        String startTimeString = startDateString + " " + item.getStarttime();
                        Date startTime = dateFormat2.parse(startTimeString);
                        se.setStartTime(startTime);
                    }

                    if (item.getEndweekday() != null && !item.getEndweekday().equals("null")) {
                        calendar.setTime(tradeDay);
                        calendar.add(Calendar.DATE, Integer.parseInt(item.getEndweekday()) - weekDay);
                        Date endDate = calendar.getTime();
                        String endDateString = dateFormat.format(endDate);
                        String endTimeString = endDateString + " " + item.getEndtime();
                        Date endTime = dateFormat2.parse(endTimeString);
                        se.setEndTime(endTime);
                    }

//					if(item.getStarttime() != null && !item.getStarttime().equals("null")){
//						String startTimeString = tradeDayString + " " + item.getStarttime();
//						Date startTime = dateFormat2.parse(startTimeString);
//						se.setStartTime(startTime);
//					}
//					
//					if(item.getEndtime() != null && !item.getEndtime().equals("null")){
//						String endTimeString = tradeDayString + " " + item.getEndtime();
//						Date endTime = dateFormat2.parse(endTimeString);
//						se.setEndTime(endTime);
//					}

//					if(settlementPlan.getOpenTime() == null){
//						settlementPlan.setOpenTime(dateFormat2.parse(tradeDayString + " " + item.getStarttime()));
//					}
//					
//					if(settlementPlan.getReckonTime() == null){
//						settlementPlan.setReckonTime(dateFormat2.parse(tradeDayString + " " + item.getEndtime()));
//					}

                    if (se.getStartTime() == null) {
                        se.setStartTime(settlementPlan.getOpenTime());
                    }

                    if (se.getEndTime() == null) {
                        se.setEndTime(settlementPlan.getReckonTime());
                    }

                    settlementPlan.getList().add(se);
                } else {
                    // 不管三七二十一 得到你是礼拜几的信息
                    int dayOfWeek = Integer.parseInt(item.getStartweekday());
                    Date tradeDate = new Date(tradeDay.getTime());
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(tradeDate);
                    // 根据交易日和交易日周几的信息，加上今日周几的信息，可以求出今天是哪日
                    calendar.add(Calendar.DATE, dayOfWeek - weekDay);
                    Date date = calendar.getTime();
                    String startTimeString = dateFormat.format(date) + " " + item.getStarttime();
                    Date startTime = dateFormat2.parse(startTimeString);
                    StartEnd startEnd = new StartEnd();
                    startEnd.setStartTime(startTime);
                    dayOfWeek = Integer.parseInt(item.getEndweekday());
                    calendar.setTime(tradeDate);
                    calendar.add(Calendar.DATE, dayOfWeek - weekDay);
                    date = calendar.getTime();
                    String endTimeString = dateFormat.format(date) + " " + item.getEndtime();
                    Date endTime = dateFormat2.parse(endTimeString);
                    startEnd.setEndTime(endTime);
                    settlementPlan.getList().add(startEnd);
                    dayOfWeek = Integer.parseInt(item.getOpenweekday());
                    calendar.setTime(tradeDate);
                    calendar.add(Calendar.DATE, dayOfWeek - weekDay);
                    date = calendar.getTime();
                    String openTimeString = dateFormat.format(date) + " " + item.getOpentime();
                    Date openTime = dateFormat2.parse(openTimeString);
                    if (settlementPlan.getOpenTime() == null || settlementPlan.getOpenTime().after(openTime)) {
                        settlementPlan.setOpenTime(openTime);
                    }
                    dayOfWeek = Integer.parseInt(item.getCloseweekday());
                    calendar.setTime(tradeDate);
                    calendar.add(Calendar.DATE, dayOfWeek - weekDay);
                    date = calendar.getTime();
                    String reckonTimeString = dateFormat.format(date) + " " + item.getReckontime();
                    Date reckonTime = dateFormat2.parse(reckonTimeString);
                    if (settlementPlan.getReckonTime() == null || settlementPlan.getReckonTime().before(reckonTime)) {
                        settlementPlan.setReckonTime(reckonTime);
                    }
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return settlementPlan;
    }
}
