package com.face.util;


import com.face.global.enums.CommonCode;
import com.face.global.language.LanguageStore;
import com.face.global.vo.ApiResult;
import com.face.global.vo.PageResponseFinanceVO;
import com.face.global.vo.PageResponseVO;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @Description:
 * @author: admin
 * @date: 2018年10月01日 12:57w
 */
public class ApiResultBuilder<T, R> {

    private String code;

    private String message;

    private R data;

    private Class<T> cls;

    public static <T> ApiResultBuilder<T, List<T>> getListBuilder(Class<T> cls) {
        ApiResultBuilder<T, List<T>> builder = new ApiResultBuilder<>();
        builder.cls = cls;
        return builder;
    }

    public static <T> ApiResultBuilder<T, T> getCommonBuilder(Class<T> cls) {
        ApiResultBuilder<T, T> builder = new ApiResultBuilder<>();
        builder.cls = cls;
        return builder;
    }

    public static ApiResultBuilder<Object, Object> getRawBuilder() {
        return new ApiResultBuilder<>();
    }


    public ApiResultBuilder<T, R> success(R data) {
        this.code = CommonCode.SUCCESS.getCode();
        this.message = getMessage();
        this.data = data;
        return this;
    }

    private String getMessage() {
        if (StringUtils.isNotBlank(this.message)) {
            return message;
        }
        return MessageUtils.getMessage(this.code, null, LanguageStore.getLocale());
    }

    public ApiResultBuilder<T, R> success() {
        this.code = CommonCode.SUCCESS.getCode();
        this.message = this.getMessage();
        return this;
    }


    public ApiResultBuilder<T, R> error() {
        this.code = CommonCode.ERROR.getCode();
        this.message = this.getMessage();
        return this;
    }

    public ApiResultBuilder<T, R> error(String message) {
        this.code = CommonCode.ERROR.toString();
        this.message = message;
        return this;
    }

    public ApiResultBuilder<T, R> baseInfo(String code, String message) {
        this.code = code;
        this.message = message;
        return this;
    }

    public ApiResult<R> build() {
        return new ApiResult<>(this.code, this.message, this.data);
    }


    public static <T> ApiResultBuilder<T, PageResponseVO<T>> getPageCommonBuilder(Class<T> cls) {
        ApiResultBuilder<T, PageResponseVO<T>> builder = new ApiResultBuilder<>();
        builder.cls = cls;
        return builder;
    }

    public static <T> ApiResultBuilder<T, PageResponseFinanceVO<T>> getPageFinanceCommonBuilder(Class<T> cls) {
        ApiResultBuilder<T, PageResponseFinanceVO<T>> builder = new ApiResultBuilder<>();
        builder.cls = cls;
        return builder;
    }
}
