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

import com.alibaba.fastjson.JSON;
import com.engine.kq.biz.KQTimesArrayComInfo;
import com.engine.kq.log.KQLog;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import weaver.general.BaseBean;
import weaver.general.Util;

/**
 * 半天规则计算类
 */
public class HalfShiftComIndex extends ShiftComIndex {

  public HalfShiftComIndex(String name,Map<String,Object> workTimeMap){
    super(name,workTimeMap);
  }

  @Override
  public void handleDuration(ShiftInfoCominfoBean shiftInfoCominfoBean) throws Exception {
    handleHalfTime(shiftInfoCominfoBean);
  }

  public void handleHalfTime(ShiftInfoCominfoBean shiftInfoCominfoBean) throws Exception {
    String halfcalrule = Util.null2String(workTimeMap.get("halfcalrule"));
    String halfcalpoint = Util.null2String(workTimeMap.get("halfcalpoint"));
    String halfcalpoint2cross = Util.null2String(workTimeMap.get("halfcalpoint2cross"));
    shiftInfoCominfoBean.setHalfcalrule(halfcalrule);
    shiftInfoCominfoBean.setHalfcalpoint(halfcalpoint);
    shiftInfoCominfoBean.setHalfcalpoint2cross(halfcalpoint2cross);
    if("0".equalsIgnoreCase(halfcalrule)){
      //采用总工作时长的一般作为半天
      setHalfWorkTime(shiftInfoCominfoBean);
    }else{
      setCustomizeHalfWorkTime(shiftInfoCominfoBean);
    }
  }

  /**
   * 采用自定义的半天规则
   * @param shiftInfoCominfoBean
   */
  public void setCustomizeHalfWorkTime(ShiftInfoCominfoBean shiftInfoCominfoBean) {
    KQTimesArrayComInfo kqTimesArrayComInfo = new KQTimesArrayComInfo();
    String halfcalpoint = Util.null2String(workTimeMap.get("halfcalpoint"));
    String halfcalpoint2cross = Util.null2String(workTimeMap.get("halfcalpoint2cross"));

    List<String> halfWorkTime = shiftInfoCominfoBean.getHalfWorkTime();
    List<int[]> halfWorkIndex = shiftInfoCominfoBean.getHalfWorkIndex();
    List<String> allWorkTime = shiftInfoCominfoBean.getAllLongWorkTime();

    if(!allWorkTime.isEmpty() && halfcalpoint.length() > 0){
      String firstTime = allWorkTime.get(0);
      String lastTime = allWorkTime.get(allWorkTime.size()-1);
      int firstTimeIndex = kqTimesArrayComInfo.getArrayindexByTimes(firstTime);
      int lastTimeIndex = kqTimesArrayComInfo.getArrayindexByTimes(lastTime);
      int halfcalpointIndex = kqTimesArrayComInfo.getArrayindexByTimes(halfcalpoint);
      if("1".equalsIgnoreCase(halfcalpoint2cross)){
        halfcalpointIndex = kqTimesArrayComInfo.turn24to48TimeIndex(halfcalpointIndex);
      }
      if(halfcalpointIndex>=firstTimeIndex && halfcalpointIndex<=lastTimeIndex){
        int[] halfs = new int[3];
        halfs[0] = firstTimeIndex;
        halfs[1] = halfcalpointIndex;
        halfs[2] = lastTimeIndex;
        halfWorkIndex.add(halfs);

        halfWorkTime.add(kqTimesArrayComInfo.getTimesByArrayindex(firstTimeIndex));
        halfWorkTime.add(kqTimesArrayComInfo.getTimesByArrayindex(halfcalpointIndex));
        halfWorkTime.add(kqTimesArrayComInfo.getTimesByArrayindex(lastTimeIndex));
      }else{

        String serialid = Util.null2String(workTimeMap.get("serialid"));
        if(halfcalpointIndex<=lastTimeIndex){
          int[] halfs = new int[3];
          halfs[0] = halfcalpointIndex;
          halfs[1] = halfcalpointIndex;
          halfs[2] = lastTimeIndex;
          halfWorkIndex.add(halfs);

          halfWorkTime.add(kqTimesArrayComInfo.getTimesByArrayindex(halfcalpointIndex));
          halfWorkTime.add(kqTimesArrayComInfo.getTimesByArrayindex(halfcalpointIndex));
          halfWorkTime.add(kqTimesArrayComInfo.getTimesByArrayindex(lastTimeIndex));
        }else{
          new KQLog().info("serialid:"+serialid+"半天规则有误:firstTimeIndex:"+ firstTimeIndex
              +":halfcalpointIndex:"+halfcalpointIndex+":lastTimeIndex:"+lastTimeIndex);
        }
      }
    }
  }

  /**
   * 采用总工作时长的一般作为半天
   * @param shiftInfoCominfoBean
   * @throws Exception
   */
  public void setHalfWorkTime(ShiftInfoCominfoBean shiftInfoCominfoBean) throws Exception {
    KQLog kqLog = new KQLog();
    KQTimesArrayComInfo kqTimesArrayComInfo = new KQTimesArrayComInfo();

    List<String> halfWorkTime = shiftInfoCominfoBean.getHalfWorkTime();
    List<int[]> halfWorkIndex = shiftInfoCominfoBean.getHalfWorkIndex();
    List<int[]> restLongTimeIndex = shiftInfoCominfoBean.getRestLongTimeIndex();

    List<String> allWorkTime = shiftInfoCominfoBean.getAllLongWorkTime();
    String serialid = shiftInfoCominfoBean.getSerialid();

    int workmins = Util.getIntValue(Util.null2String(workTimeMap.get("workmins")));

    int halfMins = workmins/2;
    if(restLongTimeIndex != null && !restLongTimeIndex.isEmpty()){
      List<int[]> workLongTimeIndex = shiftInfoCominfoBean.getWorkLongTimeIndex();
      if(workLongTimeIndex != null && !workLongTimeIndex.isEmpty() ){
        //目前只有一天一次考勤才有半天的规则
        int[] restTimes = restLongTimeIndex.get(0);
        int[] workTimes = workLongTimeIndex.get(0);
        if(restTimes.length == 2 && workTimes.length == 2){
          int restStart = restTimes[0];
          int restEnd = restTimes[1];

          int workStart = workTimes[0];
          int workEnd = workTimes[1];
          List<int[]> workAcrossTime = new ArrayList<>();
          int[] workAcross_arr = new int[2];
          workAcross_arr[0] = workStart;
          workAcross_arr[1] = restStart;
          workAcrossTime.add(workAcross_arr);
          workAcross_arr = new int[2];
          workAcross_arr[0] = restEnd;
          workAcross_arr[1] = workEnd;
          workAcrossTime.add(workAcross_arr);
          List<Integer> eachWorkMins = new ArrayList<>();
          if(restStart-workStart < 0 || workEnd-restEnd < 0){
            kqLog.info("serialid:"+serialid+"::setHalfWorkTime 报错:restStart:"+restStart+":workStart:"+workStart
                +":workEnd:"+workEnd+":restEnd:"+restEnd);
          }
          eachWorkMins.add(restStart-workStart < 0 ? 0 : (restStart-workStart));
          eachWorkMins.add(workEnd-restEnd < 0 ? 0 : (workEnd-restEnd));

          int halfCount = 0;
          int preHalfCount = 0;
          int halfIndex = -1;
          int interval = 0;
          for(int i = 0 ;i < eachWorkMins.size() ; i++){
            preHalfCount = halfCount;
            halfCount = halfCount+eachWorkMins.get(i);
            if(halfCount > halfMins){
              //这里表示半天的时长出现在这个时段里面
              halfIndex = i;
              if(halfIndex == 0){
                interval = halfMins;
              }else{
                interval = halfMins-preHalfCount;
              }
              break;
            }
          }
          if(halfIndex > -1 ){
            int[] acrossTimes = workAcrossTime.get(halfIndex);
            int halfTimeIndex = acrossTimes[0]+interval;
            String time0 = allWorkTime.get(0);
            String time1 = kqTimesArrayComInfo.getTimesByArrayindex(halfTimeIndex);
            String time2 = allWorkTime.get(allWorkTime.size()-1);
            halfWorkTime.add(time0);
            halfWorkTime.add(time1);
            halfWorkTime.add(time2);
            int[] halfs = new int[3];
            halfs[0] = kqTimesArrayComInfo.getArrayindexByTimes(time0);
            halfs[1] = kqTimesArrayComInfo.getArrayindexByTimes(time1);
            halfs[2] = kqTimesArrayComInfo.getArrayindexByTimes(time2);
            halfWorkIndex.add(halfs);
          }
        }
      }
    }else{
      List<int[]> workLongTimeIndex = shiftInfoCominfoBean.getWorkLongTimeIndex();
      List<Integer> eachWorkMins = shiftInfoCominfoBean.getEachWorkMins();
      //说明下workIndex里可能是[09:00,12:00] [13:00,18:00]
      //workmins是7小时*60 420分钟，所以他表示的是实际的工作时长
      //如果取一半的时段的话，应该是3.5小时，那么应该是13:30

      int halfCount = 0;
      int preHalfCount = 0;
      int halfIndex = -1;
      int interval = 0;
      for(int i = 0 ;i < eachWorkMins.size() ; i++){
        preHalfCount = halfCount;
        halfCount = halfCount+eachWorkMins.get(i);
        if(halfCount > halfMins){
          //这里表示半天的时长出现在这个时段里面
          halfIndex = i;
          if(halfIndex == 0){
            interval = halfMins;
          }else{
            interval = halfMins-preHalfCount;
          }
          break;
        }
      }
      if(halfIndex > -1 ){
        int[] acrossTimes = workLongTimeIndex.get(halfIndex);
        int halfTimeIndex = acrossTimes[0]+interval;
        String time0 = allWorkTime.get(0);
        String time1 = kqTimesArrayComInfo.getTimesByArrayindex(halfTimeIndex);
        String time2 = allWorkTime.get(allWorkTime.size()-1);
        halfWorkTime.add(time0);
        halfWorkTime.add(time1);
        halfWorkTime.add(time2);
        int[] halfs = new int[3];
        halfs[0] = kqTimesArrayComInfo.getArrayindexByTimes(time0);
        halfs[1] = kqTimesArrayComInfo.getArrayindexByTimes(time1);
        halfs[2] = kqTimesArrayComInfo.getArrayindexByTimes(time2);
        halfWorkIndex.add(halfs);
      }
    }
  }
}
