package pwc.taxtech.atms.dto.vatdto;

import io.jsonwebtoken.lang.Collections;
import org.apache.commons.lang3.StringUtils;
import pwc.taxtech.atms.common.util.MyAsserts;
import pwc.taxtech.atms.exception.Exceptions;
import pwc.taxtech.atms.vat.entity.PeriodJob;

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

import static pwc.taxtech.atms.dto.vatdto.WrapPeriodJobDto.*;

public class PeriodJobDto {
    private String id;
    private String name;
    private String currentStep;
    private String projectId;
    private Integer period;
    private String stepsCode;
    private Date createTime;
    private String status;
    private String errorMsg;

    private String jobStatus;

    public PeriodJobDto copyFromPeriodJob(PeriodJob job) {
        this.id = job.getId();
        this.name = job.getName();
        this.currentStep = job.getCurrentStep();
        this.projectId = job.getProjectId();
        this.period = job.getPeriod();
        this.stepsCode = job.getStepsCode();
        this.createTime = job.getCreateTime();
        this.status = job.getStatus();
        this.errorMsg = job.getErrorMsg();
        MyAsserts.assertNotEmpty(stepsCode, Exceptions.SERVER_ERROR_EXCEPTION);

        if (StringUtils.isBlank(status)) {
            this.jobStatus = STATUS_BEGIN;
        } else {
            List<WrapPeriodJobDto.Task> tasks = WrapPeriodJobDto.fromJson(status);
            if (Collections.isEmpty(tasks)) {
                this.jobStatus = STATUS_BEGIN;
            } else {
                String[] codes = stepsCode.split(",");
                if (tasks.stream().anyMatch(m -> m.status.equals(STATUS_ERROR))) {
                    this.jobStatus = STATUS_ERROR;
                } else if (tasks.size() < codes.length || tasks.stream().anyMatch(m -> m.status.equals(STATUS_BEGIN))) {
                    this.jobStatus = STATUS_RUNNING;
                } else {
                    this.jobStatus = STATUS_END;
                }
            }
        }

        return this;
    }

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCurrentStep() {
        return this.currentStep;
    }

    public void setCurrentStep(String currentStep) {
        this.currentStep = currentStep;
    }

    public String getProjectId() {
        return this.projectId;
    }

    public void setProjectId(String projectId) {
        this.projectId = projectId;
    }

    public Integer getPeriod() {
        return this.period;
    }

    public void setPeriod(Integer period) {
        this.period = period;
    }

    public String getStepsCode() {
        return this.stepsCode;
    }

    public void setStepsCode(String stepsCode) {
        this.stepsCode = stepsCode;
    }

    public Date getCreateTime() {
        return this.createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getStatus() {
        return this.status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getErrorMsg() {
        return this.errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public String getJobStatus() {
        return this.jobStatus;
    }

    public void setJobStatus(String jobStatus) {
        this.jobStatus = jobStatus;
    }
}