package com.microservices.pms.product.domain.enums;

import com.microservices.common.core.exception.ServiceException;
import com.microservices.pms.product.domain.vo.ReqStatusCommonVo;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

/**
 * 产品需求状态
 *
 * @author OTTO
 */
@Getter
public enum ProductReqStatus {
    PENDING_REVIEW_USER_REQ("pendingReviewUserReq", "待评审", "待评审的用户需求", "#FF6F00"),
    REJECTED_USER_REQ("rejectedUserReq", "未通过", "已拒绝的用户需求", "#FF0000"),
    REVIEWED_USER_REQ("reviewedUserReq", "已评审", "已评审的用户需求", "#13B33E"),
    PENDING_REVIEW_REQ_SPECS("pendingReviewReqSpecs", "待评审", "待评审的需求规格", "#FF6F00"),
    CHANGING_REQ_SPECS("changingReqSpecs", "变更中", "变更中的需求规格", "#FF0000"),
    CHANGED_REQ_SPECS("changedReqSpecs", "已变更", "已变更的需求规格", null),
    REJECTED_CHANGE_REQ_SPECS("rejectedChangeReqSpecs", "已拒绝变更", "已拒绝变更的需求规格", null),
    REVIEWED_REQ_SPECS_CHANGING("reviewedReqSpecsChanging", "已评审（变更中）", "已评审的需求规格（变更中）", null),
    PLANNED_REQ_SPECS_CHANGING("plannedReqSpecsChanging", "计划中（变更中）", "计划中的需求规格（变更中）", null),
    PLANNED_REQ_SPECS("plannedReqSpecs", "计划中", "计划中的需求规格", "#0D5EF8"),
    REJECTED_REQ_SPECS("rejectedReqSpecs", "已拒绝", "已拒绝的需求规格", "#B1AAA5"),
    REVIEWED_REQ_SPECS("reviewedReqSpecs", "已评审", "已评审的需求规格", "#13B33E"),
    COMPLETED_REQ_SPECS("completedReqSpecs", "已完成", "已完成的需求规格", "#13B33E");
    private final String key;
    private final String name;
    private final String remark;
    private final String color;

    ProductReqStatus(String key, String name, String remark, String color) {
        this.key = key;
        this.name = name;
        this.remark = remark;
        this.color = color;
    }

    public static ProductReqStatus getByKey(String key) {
        for (ProductReqStatus status : ProductReqStatus.values()) {
            if (status.getKey().equals(key)) {
                return status;
            }
        }
        throw new ServiceException("需求状态错误(需求状态[%s])", key);
    }

    public static ReqStatusCommonVo getTypeByKey(String status) {
        for (ProductReqStatus productReqStatus : ProductReqStatus.values()) {
            if (productReqStatus.getKey().equals(status)) {
                return new ReqStatusCommonVo(productReqStatus);
            }
        }
        return null;
    }

    // 获取来源状态
    public static ProductReqStatus getFromStatus(String status) {
        ProductReqStatus productReqStatus = getByKey(status);
        if (REVIEWED_REQ_SPECS_CHANGING == productReqStatus) {
            return REVIEWED_REQ_SPECS;
        } else if (PLANNED_REQ_SPECS_CHANGING == productReqStatus) {
            return PLANNED_REQ_SPECS;
        } else {
            throw new ServiceException("获取需求规格的来源状态失败");
        }
    }

    // 获取将改成的状态
    public static ProductReqStatus getToStatus(String status) {
        ProductReqStatus productReqStatus = getByKey(status);
        if (REVIEWED_REQ_SPECS == productReqStatus) {
            return REVIEWED_REQ_SPECS_CHANGING;
        } else if (PLANNED_REQ_SPECS == productReqStatus) {
            return PLANNED_REQ_SPECS_CHANGING;
        } else {
            throw new ServiceException("获取需求规格的更新状态失败");
        }
    }

    public static boolean isUserReqStatus(String key) {
        return PENDING_REVIEW_USER_REQ.getKey().equals(key)
                || REJECTED_USER_REQ.getKey().equals(key)
                || REVIEWED_USER_REQ.getKey().equals(key);
    }

    public static List<ReqStatusCommonVo> getUserReqStatusList() {
        List<ReqStatusCommonVo> statusCommonVoList = new ArrayList<>();
        for (ProductReqStatus productReqStatus : ProductReqStatus.values()) {
            if (isUserReqStatus(productReqStatus.getKey()) && productReqStatus.getColor() != null) {
                statusCommonVoList.add(new ReqStatusCommonVo(productReqStatus));
            }
        }
        return statusCommonVoList;
    }

    public static List<ReqStatusCommonVo> getReqSpecsStatusList() {
        List<ReqStatusCommonVo> statusCommonVoList = new ArrayList<>();
        for (ProductReqStatus productReqStatus : ProductReqStatus.values()) {
            if (!isUserReqStatus(productReqStatus.getKey()) && productReqStatus.getColor() != null) {
                statusCommonVoList.add(new ReqStatusCommonVo(productReqStatus));
            }
        }
        return statusCommonVoList;
    }
}
