package com.engine.kq.biz.chain.overtime;

import com.alibaba.fastjson.JSON;
import com.engine.kq.biz.KQOvertimeRulesBiz;
import com.engine.kq.biz.KQShiftRuleInfoBiz;
import com.engine.kq.biz.KQTimesArrayComInfo;
import com.engine.kq.biz.chain.shiftinfo.ShiftInfoBean;
import com.engine.kq.util.KQDurationCalculatorUtil;
import com.engine.kq.wfset.bean.SplitBean;
import com.google.common.collect.Lists;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.springframework.beans.BeanUtils;
import weaver.general.Util;

/**
 * 如果前一天是节假日，当天是工作日的情况
 */
public class OvertimeHoliday2WorkdayChain extends OvertimeChain{

  public OvertimeHoliday2WorkdayChain(Map<String, String> splitMap,List<SplitBean> splitBeans) {
    super(splitMap,splitBeans);
  }

  @Override
  public void handleOvertimeChain(SplitBean splitBean) throws Exception {
    int changeType = splitBean.getChangeType();
    int preChangeType = splitBean.getPreChangeType();
    if(preChangeType == 1 && changeType == 2){
      doOverTimeSplit1_2(splitBean);
    }else{
      this.success.handleOvertimeChain(splitBean);
    }
  }

  /**
   * 工作日上班前的时间都属于前一天的节假日跨天加班
   * @param splitBean
   */
  public void doOverTimeSplit1_2(SplitBean splitBean) throws Exception{

    KQTimesArrayComInfo arrayComInfo = new KQTimesArrayComInfo();
    String resourceid = splitBean.getResourceId();
    String splitDate = splitBean.getBelongDate();
    String preSplitDate = splitMap.get("preSplitDate");
    int preComputingMode = KQOvertimeRulesBiz.getComputingMode(resourceid, preSplitDate);

    kqLog.info("OvertimeHoliday2WorkdayChain:resourceid："+resourceid+":preComputingMode:"+preComputingMode);
    if(preComputingMode == 3){
//          前一天是无需审批，根据打卡时间计算加班时长,那么只需要按照普通的工作日加班处理就可以
      doOverTimeSplit2(splitBean);
    }else{
      ShiftInfoBean shiftInfoBean = KQDurationCalculatorUtil.getWorkTime(resourceid, splitDate,true);
      kqLog.info("OvertimeHoliday2WorkdayChain:resourceid："+resourceid+":shiftInfoBean:"+ (shiftInfoBean != null ? JSON.toJSONString(shiftInfoBean): null) );
      if(shiftInfoBean != null){
        List<int[]> workLongTimeIndex = shiftInfoBean.getWorkLongTimeIndex();
        List<int[]> real_workLongTimeIndex = Lists.newArrayList();

        //list带数组，这里要深拷贝
        for(int[] tmp : workLongTimeIndex){
          int[] real_tmp = new int[tmp.length];
          System.arraycopy(tmp, 0, real_tmp, 0, tmp.length);
          real_workLongTimeIndex.add(real_tmp);
        }
        if(real_workLongTimeIndex.size() == 1){
          //个性化设置只支持一次打卡的
          KQShiftRuleInfoBiz kqShiftRuleInfoBiz = new KQShiftRuleInfoBiz();
          kqShiftRuleInfoBiz.rest_workLongTimeIndex(shiftInfoBean,splitBean,real_workLongTimeIndex,arrayComInfo,null);
        }
        if(real_workLongTimeIndex != null && !real_workLongTimeIndex.isEmpty()){
          int firstWorkIndex = doOverTimePreHoliday(splitBean, arrayComInfo, real_workLongTimeIndex);
          kqLog.info("OvertimeHoliday2WorkdayChain:resourceid："+resourceid+":firstWorkIndex:"+firstWorkIndex);
          handleCurWork(splitBean, arrayComInfo, shiftInfoBean, real_workLongTimeIndex,firstWorkIndex);
        }else{
          doOverTimeSplit2(splitBean);
        }
      }else{
        doOverTimeSplit2(splitBean);
      }
    }
  }

  /**
   * 前一天是节假日，处理前一天节假日的跨天数据
   * @param splitBean
   * @param arrayComInfo
   * @param real_workLongTimeIndex
   */
  public int doOverTimePreHoliday(SplitBean splitBean, KQTimesArrayComInfo arrayComInfo,List<int[]> real_workLongTimeIndex){
    String resourceid = splitBean.getResourceId();
    String splitDate = splitBean.getBelongDate();
    String splitFromTime = splitBean.getFromTime();
    String splitToTime = splitBean.getToTime();
    int[] initArrays = arrayComInfo.getInitArr();
    int startIndex = arrayComInfo.getArrayindexByTimes(splitFromTime);
    boolean shouldAcross = "1".equalsIgnoreCase(Util.null2String(splitMap.get("shouldAcross")));
    String preSplitDate = splitMap.get("preSplitDate");

    int overtimeEnable = KQOvertimeRulesBiz.getOvertimeEnable(resourceid, preSplitDate);

    int endIndex = arrayComInfo.getArrayindexByTimes(splitToTime);
    if(shouldAcross){
      endIndex = arrayComInfo.getArrayindexByTimes("24:00");
    }
    int[] workIndexs = real_workLongTimeIndex.get(0);
    //得到当天最早的上班时间，则从0-firstWorkIndex都属于前一天的加班
    int firstWorkIndex = workIndexs[0];

    //先把要计算的时长填充上0
    Arrays.fill(initArrays, startIndex, endIndex, 0);

    List<String[]> restTimeList = KQOvertimeRulesBiz.getRestTimeList(resourceid, preSplitDate);

    //再把休息时间填充上去
    if(!restTimeList.isEmpty()){
      for(int i =0 ; i < restTimeList.size() ; i++){
        String[] restTimes = restTimeList.get(i);
        if(restTimes.length == 2){
          int restStart = arrayComInfo.getArrayindexByTimes(restTimes[0]);
          int restEnd = arrayComInfo.getArrayindexByTimes(restTimes[1]);
          if(shouldAcross && restEnd == 1439){
            //针对跨天的休息时段单独处理排除掉23:59-00:00的时间
            restEnd = 1440;
          }
          Arrays.fill(initArrays, restStart, restEnd, 1);
        }
      }
    }

    int overMins = arrayComInfo.getCnt(initArrays, startIndex, firstWorkIndex, 0);

    overMins = overMins > 0 ? overMins : 0;
    //如果人再当前指定日期下未开启加班，直接跳过
    if(overtimeEnable != 1){
      overMins= 0;
      kqLog.info("doOverTimePreHoliday:resourceid："+resourceid+":preSplitDate:"+preSplitDate+"不允许加班");
    }
    if(overMins > 0){
      SplitBean preSplitBean = new SplitBean();
      //然后把bean重新赋值下，根据拆分后的时间
      BeanUtils.copyProperties(splitBean, preSplitBean);
      preSplitBean.setD_Mins(overMins);
      preSplitBean.setBelongDate(preSplitDate);
      preSplitBean.setChangeType(1);
      int computingMode = KQOvertimeRulesBiz.getComputingMode(preSplitBean.getResourceId(), preSplitDate);
      preSplitBean.setComputingMode(""+computingMode);
      fillNewBean(preSplitBean);
    }

    return firstWorkIndex;
  }

}
