package com.docus.server.report.validate;

import com.docus.infrastructure.web.api.ResultCode;
import com.docus.server.report.config.TaskValidateConfig;
import com.docus.server.report.enums.DataTypeEnum;
import com.docus.server.report.enums.OperatorEnum;
import lombok.Data;

import javax.validation.ValidationException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public abstract class CollectTaskValidation {
    private final static DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 验证
     *
     * @param text
     * @param validateConfig
     */
    public abstract ValidationResult validate(String text, TaskValidateConfig.ValidateConfig validateConfig);

    /**
     * 验证时间
     */
    public static ValidationResult validateDate(String text, TaskValidateConfig.ValidateConfig validateConfig) {
        String operator = validateConfig.getOperator().toLowerCase();
        String message="日期验证,操作符号：" + operator + "，操作表达：" + text + ".compareTo(" + validateConfig.getValue() + ")";
        LocalDateTime localDateTime1 = LocalDateTime.parse(text, DATETIME_FORMATTER);
        LocalDateTime localDateTime2 = LocalDateTime.parse(validateConfig.getValue(), DATETIME_FORMATTER);
        int compareToResult = localDateTime1.compareTo(localDateTime2);
        return getValidationResultByCompareToResult(operator,compareToResult,message);
    }


    private static ValidationResult getValidationResultByCompareToResult(String operator,int compareToResult,String message){
        if (OperatorEnum.EQ.getMasterCode().equals(operator) || OperatorEnum.EQ.getSlaveCode().equals(operator)) {
            if (compareToResult == 0) {
                return new ValidationResult(ResultCode.SUCCESS, message + " 通过");
            }
            return new ValidationResult(ResultCode.FAILED, message + " 不通过");  }
        if (OperatorEnum.NOT_EQ_1.getMasterCode().equals(operator) || OperatorEnum.NOT_EQ_1.getSlaveCode().equals(operator)) {
            if (compareToResult != 0) {
                return new ValidationResult(ResultCode.SUCCESS, message + " 通过");
            }
            return new ValidationResult(ResultCode.FAILED, message + " 不通过"); }
        if (OperatorEnum.NOT_EQ_2.getMasterCode().equals(operator) || OperatorEnum.NOT_EQ_2.getSlaveCode().equals(operator)) {
            if (compareToResult != 0) {
                return new ValidationResult(ResultCode.SUCCESS, message + " 通过");
            }
            return new ValidationResult(ResultCode.FAILED, message + " 不通过"); }
        if (OperatorEnum.GT.getMasterCode().equals(operator) || OperatorEnum.GT.getSlaveCode().equals(operator)) {
            if (compareToResult > 0) {
                return new ValidationResult(ResultCode.SUCCESS, message + " 通过");
            }
            return new ValidationResult(ResultCode.FAILED, message + " 不通过"); }
        if (OperatorEnum.GE.getMasterCode().equals(operator) || OperatorEnum.GE.getSlaveCode().equals(operator)) {
            if (compareToResult >= 0) {
                return new ValidationResult(ResultCode.SUCCESS, message + " 通过");
            }
            return new ValidationResult(ResultCode.FAILED, message + " 不通过"); }
        if (OperatorEnum.LT.getMasterCode().equals(operator) || OperatorEnum.LT.getSlaveCode().equals(operator)) {
            if (compareToResult < 0) {
                return new ValidationResult(ResultCode.SUCCESS, message + " 通过");
            }
            return new ValidationResult(ResultCode.FAILED, message + " 不通过"); }
        if (OperatorEnum.LE.getMasterCode().equals(operator) || OperatorEnum.LE.getSlaveCode().equals(operator)) {
            if (compareToResult <= 0) {
                return new ValidationResult(ResultCode.SUCCESS, message + " 通过");
            }
            return new ValidationResult(ResultCode.FAILED, message + " 不通过");
        }
        throw new ValidationException("未知的操作符号！");
    }

    /**
     * 验证字符串
     */
    public static ValidationResult validateString(String text, TaskValidateConfig.ValidateConfig validateConfig) {
        String operator = validateConfig.getOperator().toLowerCase();
        int compareToResult = text.compareTo(validateConfig.getValue());
        String message = "字符串验证,操作符号：" + operator + "，操作表达：" + text + ".compareTo(" + validateConfig.getValue() + ")";
        return getValidationResultByCompareToResult(operator,compareToResult,message);
    }


    /**
     * 验证数字
     */
    public static ValidationResult validateNumber(String text, TaskValidateConfig.ValidateConfig validateConfig) {
        throw new RuntimeException("任务验证数字类型待开发！");
    }

    @Data
    public static class ValidationResult {
        private ResultCode code;
        private String message;

        public ValidationResult() {
        }

        public ValidationResult(ResultCode code, String message) {
            this.code = code;
            this.message = message;
        }
    }

    public static class CollectTaskAdmissionDateValidation extends CollectTaskValidation {

        @Override
        public ValidationResult validate(String text, TaskValidateConfig.ValidateConfig validateConfig) {
            if (DataTypeEnum.DATE_TIME.getCode().equals(validateConfig.getDataType())) {
                return validateDate(text, validateConfig);
            }
            return new ValidationResult(ResultCode.FAILED,"日期验证，数据类型不为"+DataTypeEnum.DATE_TIME.getCode());
        }
    }

    public static class CollectTaskDischargeDateValidation extends CollectTaskValidation {

        @Override
        public ValidationResult validate(String text, TaskValidateConfig.ValidateConfig validateConfig) {
            if (DataTypeEnum.DATE_TIME.getCode().equals(validateConfig.getDataType())) {
                return validateDate(text, validateConfig);
            }
            return new ValidationResult(ResultCode.FAILED,"日期验证，数据类型不为"+DataTypeEnum.DATE_TIME.getCode());
        }
    }

    public static class CollectTaskAdmissionDepartmentValidation extends CollectTaskValidation {

        @Override
        public ValidationResult validate(String text, TaskValidateConfig.ValidateConfig validateConfig) {
            if (DataTypeEnum.STRING.getCode().equals(validateConfig.getDataType())) {
                return validateString(text, validateConfig);
            }
            return new ValidationResult(ResultCode.FAILED,"字符串验证，数据类型不为"+DataTypeEnum.STRING.getCode());
        }
    }

    public static class CollectTaskDischargeDepartmentValidation extends CollectTaskValidation {

        @Override
        public ValidationResult validate(String text, TaskValidateConfig.ValidateConfig validateConfig) {
            if (DataTypeEnum.STRING.getCode().equals(validateConfig.getDataType())) {
                return validateString(text, validateConfig);
            }
            return new ValidationResult(ResultCode.FAILED,"字符串验证，数据类型不为"+DataTypeEnum.STRING.getCode());
        }
    }

    public static void main(String[] args) {
        CollectTaskValidation admissionDepartmentValidation = new CollectTaskValidation.CollectTaskAdmissionDateValidation();
        TaskValidateConfig.ValidateConfig validateConfig = new TaskValidateConfig.ValidateConfig();
        validateConfig.setValue("2023-04-30 00:00:00");
        validateConfig.setDataType(DataTypeEnum.DATE_TIME.getCode());
        validateConfig.setOperator("=");
        System.out.println(admissionDepartmentValidation.validate("2023-05-01 00:00:00", validateConfig));
        validateConfig.setOperator("<=");
        System.out.println(admissionDepartmentValidation.validate("2023-05-01 00:00:00", validateConfig));
        validateConfig.setOperator(">=");
        System.out.println(admissionDepartmentValidation.validate("2023-05-01 00:00:00", validateConfig));

        validateConfig.setOperator("not");
        System.out.println(admissionDepartmentValidation.validate("2023-05-01 00:00:00", validateConfig));

    }
}
