package com.xunmei.core.resumption.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.xunmei.common.core.utils.FormType;
import com.xunmei.common.core.utils.ResumptionStatus;
import com.xunmei.common.core.utils.ResumptionType;
import com.xunmei.common.core.utils.Ymd;
import com.xunmei.common.core.web.domain.BaseEntity;
import com.xunmei.system.api.domain.SysOrg;
import feign.form.FormData;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;



/**
 * 履职
 */
@Getter
@Setter
@AllArgsConstructor
@Builder

public class Resumption extends BaseEntity {

    private static Logger log = LoggerFactory.getLogger(Resumption.class);

    public static Resumption of(final Long orgId, final ResumptionType type, final FormType formType, final Ymd ymd) {
        // 判断时间是否过期, 如果过期就显示未提交
        final ResumptionStatus status = type.isExpire(ymd) ? ResumptionStatus.NOT : ResumptionStatus.WAIT;

        //
        return Resumption.builder()
                         .id(type.toId(formType, ymd.getDate(), orgId))
                         .status(status)
                         .orgId(orgId)
                         .type(type)
                         .formType(formType)
                         .exceptionCount(0)
                         .ymd(ymd)
                         .build();
    }

    public static Resumption newof(final Long orgId, final ResumptionType type, final Ymd ymd, final Long appPlanid,final String id) {
        // 判断时间是否过期, 如果过期就显示未提交
        final ResumptionStatus status = type.isExpire(ymd) ? ResumptionStatus.NOT : ResumptionStatus.WAIT;
        return Resumption.builder()
                .id(id)
                .status(status)
                .orgId(orgId)
                .type(type)
                .exceptionCount(0)
                .ymd(ymd)
                .planId(appPlanid)
                .build()
               ;
    }


    /**
     * 编号
     */
    private String id;

    /**
     * 机构
     */
    private Long orgId;

    /**
     * 年月日
     */
    private Ymd ymd;

    /**
     * 状态
     */
    private ResumptionStatus status;

    /**
     * 履职类型
     */
    private ResumptionType type;

    /**
     * 表单类型
     */
    private FormType formType;

    /**
     * 异常数目
     */
    private Integer exceptionCount;

    /**
     * 提交时间
     */
    private Date submitTime;

    /**
     * 机构
     */
    @JsonIgnore
    //@NotFound(action = NotFoundAction.IGNORE)
    private SysOrg org;

    /**
     * 计划
     */
    @JsonIgnore
    //@NotFound(action = NotFoundAction.IGNORE)
    private AppPlan plan;


    private Long planId;
    private Date startTime;
    private Date endTime;
    private Date planStartTime;
    private Date planEndTime;
    private Long roleId;


    /**
     * 表单数据
     */
    @JsonIgnore
    private List<FormData> dataList;


    public Resumption() {
    }

    public Resumption(final String id) {
        this.id = id;
    }


    /**
     * 设置为未提交
     */
//    public void notSubmit() {
//        this.status = ResumptionStatus.NOT;
//        this.registerEvent(ResumptionStatusChangeEvent.to(this));
//    }

    /**
     * 提交
     *
     * @param exceptionCount 错误数量
     */
//    public void submit(final int exceptionCount) {
//        //this.submitTime = new Date();
//        //this.status = exceptionCount > 0 ? ResumptionStatus.ERROR : ResumptionStatus.NORMAL;
//        this.exceptionCount = exceptionCount;
//        this.registerEvent(ResumptionStatusChangeEvent.to(this));
//        this.registerEvent(new ResumptionOverviewChangeEvent(
//                this.getOrgId(),
//                this.type,
//                this.formType,
//                ResumptionStatus.DELAY.equals(this.status),
//                this.type.getOverview(this.formType),
//                this.ymd
//        ));
//    }

    /**
     * @return
     */
    public String summary() {
        switch (this.type) {
            case DAY:
                return String.format("%s-%s-%s %s", this.ymd.getYear(), this.ymd.getMonth(), this.ymd.getDay(), this.formType.getText());
            case WEEK:
                return String.format("%s 第%s周 %s", this.ymd.getYear(), this.ymd.getWeek(), this.formType.getText());
            case MONTH:
                return String.format("%s-%s %s", this.ymd.getYear(), this.ymd.getMonth(), this.formType.getText());
            case QUARTER:
                return String.format("%s 第%s季 %s", this.ymd.getYear(), this.ymd.getQuarter(), this.formType.getText());
            case HALFYEAR:
                if(this.ymd.getHalfyear().intValue() == 1){
                    return String.format("%s 上半年 %s", this.ymd.getYear(), this.formType.getText());
                }else{
                    return String.format("%s 下半年 %s", this.ymd.getYear(), this.formType.getText());
                }
            default:
                return String.format("%s 年 %s", this.ymd.getYear(), this.formType.getText());
        }
    }

    /**
     * 是否提交
     */
    public boolean isSubmit() {
        return this.submitTime != null;
    }

    /***
     *
     * @param isWork
     */
    public void changeWork(final Boolean isWork) {
        final ResumptionStatus old = this.status;
        // 再次变更, 如果营业
        if (isWork) {
            // 过期,除了已提交的都是未提交
            if (this.type.isExpire(this.ymd)) {
                switch (this.status) {
                    case ERROR:
                    case NORMAL:
                        break;
                    default:
                        this.setStatus(ResumptionStatus.NOT);
                }
            }
            // 未过期, 如果是歇业或者未提交, 那么变成待履职
            else {
                switch (this.status) {
                    case NA:
                    case NOT:
                        this.setStatus(ResumptionStatus.WAIT);
                        break;
                }
            }
        }
        // 再次变更, 如果歇业
        else {
            this.setStatus(ResumptionStatus.NA);
        }
//        RESUMPTIONLOG.info("[ {} ]履职旧状态[ {} ] => [ {} ]", this.id, old, this.status);
    }
}
