package com.tofly.latte_core.net.bean;

import com.tofly.latte_core.net.exception.ExceptionHandle;

import io.reactivex.ObservableTransformer;

/**
 * 基础数据类型
 */
public class MisResponse<T> {

    public static final int RESPONSE_OK = 200;

    public static final int AUTHOR_FAILED = 306;

    private int Code;
    private String Message;
    private T Result;

    public int getCode() {
        return Code;
    }

    public void setCode(int code) {
        Code = code;
    }

    public String getMessage() {
        return Message;
    }

    public void setMessage(String message) {
        Message = message;
    }

    public T getResult() {
        return Result;
    }

    public void setResult(T result) {
        Result = result;
    }

    public static class Transformer {

        public static <T> ObservableTransformer<MisResponse<T>, T> sTransformer() {

            return responseObservable -> responseObservable.map(tResponse -> {
                if (tResponse.getCode() != MisResponse.RESPONSE_OK) {
                    if (tResponse.getCode() == MisResponse.AUTHOR_FAILED) {
                        //return tResponse.getResult();
                        throw new ExceptionHandle.ServerException(tResponse.getCode(), tResponse.getMessage());
                    }else
                        throw new RuntimeException(tResponse.getMessage());
                }
                return tResponse.getResult();
            });
        }
    }
}
