package com.engine.kq.cmd.attendanceButton;

import com.alibaba.fastjson.JSON;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.biz.KQGroupMemberComInfo;
import com.engine.kq.entity.KQGroupEntity;
import com.engine.kq.log.KQLog;
import com.engine.kq.util.KQDurationCalculatorUtil;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import weaver.common.DateUtil;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

/**
 * 外勤签到签退 关联考勤的时候，打卡范围
 */
public class GetOutButtonRangeCmd extends AbstractCommonCommand<Map<String, Object>> {

  private KQLog kqLog = new KQLog();
  private String curDate = DateUtil.getCurrentDate();
  private DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

	public GetOutButtonRangeCmd(Map<String, Object> params, User user) {
		this.user = user;
		this.params = params;
	}

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
		Map<String, Object> retmap = new HashMap<String, Object>();
		try{
      KQGroupMemberComInfo kqGroupMemberComInfo = new KQGroupMemberComInfo();
      String outsidesign = "";
      KQGroupEntity kqGroupEntity = kqGroupMemberComInfo.getUserKQGroupInfo(user.getUID()+"");
      if (kqGroupEntity != null) {
        outsidesign = kqGroupEntity.getOutsidesign();
      }
      kqLog.info("GetOutButtonRangeCmd:outsidesign:"+outsidesign);
      List<Object> todayLineButton = new ArrayList<>();
      if("1".equalsIgnoreCase(outsidesign)){
        Map<String, Object> todayLineMap = KQDurationCalculatorUtil.getWorkButton(user, curDate,true);
        kqLog.info("GetOutButtonRangeCmd:todayLineMap:"+ JSON.toJSONString(todayLineMap));
        if(todayLineMap.get("pre_signTime") != null){
          do4YesterdayLine(todayLineMap,todayLineButton);
        }
        if(todayLineMap.get("signTime") != null){
          do4TodayLine(todayLineMap,todayLineButton);
        }
      }

      retmap.put("outsidesign", outsidesign);
      retmap.put("signSection", todayLineButton);
			retmap.put("status", "1");
		}catch (Exception e) {
			retmap.put("status", "-1");
			retmap.put("message",  SystemEnv.getHtmlLabelName(382661,user.getLanguage()));
			writeLog(e);
		}
		return retmap;
	}

  private void do4TodayLine(Map<String, Object> todayLineMap,
      List<Object> todayLineButton) {
    List<Map<String, String>> todaySignTime = (List<Map<String, String>>)todayLineMap.get("signTime");
    if(todaySignTime != null && !todaySignTime.isEmpty()){
      String yesterday = LocalDate.parse(curDate).minusDays(1).format(dateFormatter);
      String nextday = LocalDate.parse(curDate).plusDays(1).format(dateFormatter);
      for(int i = 0 ; i < todaySignTime.size() ; i++) {
        Map<String, String> todaySignMap = todaySignTime.get(i);
        String bengintime = todaySignMap.get("bengintime");
        String bengintime_across = todaySignMap.get("bengintime_across");
        String endtime = todaySignMap.get("endtime");
        String endtime_across = todaySignMap.get("endtime_across");

        String signInDateTime = curDate + " " +bengintime+":00";
        if("1".equalsIgnoreCase(bengintime_across)) {
          signInDateTime = yesterday + " " +bengintime+":00";
        }
        String signOutDateTime = curDate + " " +endtime+":59";
        if("1".equalsIgnoreCase(endtime_across)) {
          signOutDateTime = nextday + " " +endtime+":59";
        }
        String signSection = signInDateTime+"#"+signOutDateTime;
        todayLineButton.add(signSection);
      }
    }
  }

  private void do4YesterdayLine(Map<String, Object> todayLineMap,
      List<Object> todayLineButton) {
    List<Map<String, String>> yesterdaySignTime = (List<Map<String, String>>)todayLineMap.get("pre_signTime");
    if(yesterdaySignTime != null && !yesterdaySignTime.isEmpty()){
      String yesterday = LocalDate.parse(curDate).minusDays(1).format(dateFormatter);
      String yesyesterday = LocalDate.parse(yesterday).minusDays(1).format(dateFormatter);
      for(int i = 0 ; i < yesterdaySignTime.size() ; i++) {
        boolean needRecord = false;
        Map<String, String> yesterdaySignMap = yesterdaySignTime.get(i);
        String bengintime = yesterdaySignMap.get("bengintime");
        String bengintime_across = yesterdaySignMap.get("bengintime_across");
        String endtime = yesterdaySignMap.get("endtime");
        String endtime_across = yesterdaySignMap.get("endtime_across");

        String signInDateTime = yesterday + " " +bengintime+":00";
        if("1".equalsIgnoreCase(bengintime_across)) {
          signInDateTime = yesyesterday + " " +bengintime+":00";
        }
        String signOutDateTime = yesterday + " " +endtime+":59";
        if("1".equalsIgnoreCase(endtime_across)) {
          signOutDateTime = curDate + " " +endtime+":59";
          //昨天的只有允许打卡再今天的才记录
          needRecord = true;
        }
        String signSection = signInDateTime+"#"+signOutDateTime;
        if(needRecord){
          todayLineButton.add(signSection);
        }
      }
    }
  }

  @Override
	public BizLogContext getLogContext() {
		return null;
	}

}
