package cd.casic.module.terminal.common;

import cd.casic.module.terminal.common.holder.SpringHolder;

/**
 * 标准数据定义
 *
 * @author Jiahang Li
 * @version 1.0.0
 * @since 2023/12/21 0:07
 */
@SuppressWarnings("unchecked")
public interface GenericsStrategyDefinition {

    /**
     * 获取数据处理策略
     *
     * @return class
     */
    Class<? extends GenericsDataStrategy<? extends GenericsDataModel>> getStrategyClass();

    /**
     * 获取数据模型策略处理器
     *
     * @param <M> Model
     * @param <S> Strategy
     * @return Strategy Bean
     */
    default <M extends GenericsDataModel, S extends GenericsDataStrategy<M>> S getStrategy() {
        return (S) SpringHolder.getBean(this.getStrategyClass());
    }

    /**
     * 获取默认值
     *
     * @param <M> model
     * @return model
     */
    default <M extends GenericsDataModel> M getDefault() {
        return (M) this.getStrategy().getDefault();
    }

    /**
     * 执行完整验证链
     *
     * @param beforeModel beforeModel
     * @param afterModel  afterModel
     */
    default void doValid(GenericsDataModel beforeModel, GenericsDataModel afterModel) {
        this.getStrategy().doValid(beforeModel, afterModel);
    }

    /**
     * 反序列化对象
     *
     * @param serialModel serialModel
     * @param <M>         model
     * @return model
     */
    default <M extends GenericsDataModel> M parse(String serialModel) {
        return (M) this.getStrategy().parse(serialModel);
    }

    /**
     * 转为视图对象
     *
     * @param serialModel serialModel
     * @param <M>         model
     * @return viewModel
     */
    default <M extends GenericsDataModel> M toView(String serialModel) {
        return (M) this.getStrategy().toView(serialModel);
    }

}
