package com.kfzs.duanduan.utils.net;

import com.google.protobuf.InvalidProtocolBufferException;
import com.kfzs.appstore.utils.string.HexUtils;
import com.kfzs.duanduan.proto.ApiResponseOuterClass;

import go.kfzssafe.Kfzssafe;

/**
 * <pre>
 *     sinlov
 *
 *     /\__/\
 *    /`    '\
 *  ≈≈≈ 0  0 ≈≈≈ Hello world!
 *    \  --  /
 *   /        \
 *  /          \
 * |            |
 *  \  ||  ||  /
 *   \_oo__oo_/≡≡≡≡≡≡≡≡o
 *
 * </pre>
 * Created by sinlov on 17/3/30.
 */
public abstract class ApiUploadFileCallBack implements UploadFileCallBack {
    public static final int INVALID_PROTOCOL_BUFFER_ERROR = -99;

    @Override
    public void responseUploadSuccess(String msg) {
        try {
            byte[] bytes = HexUtils.hexStr2Bytes(msg);
            byte[] byteDecode = Kfzssafe.XByteDecode(bytes);
            ApiResponseOuterClass.ApiResponse apiResponse = ApiResponseOuterClass.ApiResponse.parseFrom(byteDecode);
            int codeValue = apiResponse.getCodeValue();
            switch (codeValue) {
                case ApiResponseOuterClass.Codes.Success_VALUE:
                    onApiSuccess(apiResponse);
                    break;
                case ApiResponseOuterClass.Codes.Unauthorized_VALUE:
                    onApiUnauthorized(apiResponse);
                default:
                    onApiError(codeValue, apiResponse);
                    break;
            }
        } catch (Exception e) {
            onParseError(INVALID_PROTOCOL_BUFFER_ERROR, e);
        }
    }

    @Override
    public void responseUploadError(int code, String msg) {
        onParseError(code, new Exception(msg));
    }

    /**
     * when this call back job code is {@link com.kfzs.duanduan.proto.ApiResponseOuterClass.Codes#Success_VALUE}
     *
     * @param response {@link com.kfzs.duanduan.proto.ApiResponseOuterClass.ApiResponse}
     */
    public abstract void onApiSuccess(ApiResponseOuterClass.ApiResponse response);

    /**
     * when this call back job code is {@link com.kfzs.duanduan.proto.ApiResponseOuterClass.Codes#Unauthorized_VALUE}
     *
     * @param response {@link com.kfzs.duanduan.proto.ApiResponseOuterClass.ApiResponse}
     */
    public abstract void onApiUnauthorized(ApiResponseOuterClass.ApiResponse response);

    /**
     * this method call back by right parse btn_dialog_close code not
     * <br/><li/>{@link com.kfzs.duanduan.proto.ApiResponseOuterClass.Codes#Success_VALUE}
     * <br/><li/>{@link com.kfzs.duanduan.proto.ApiResponseOuterClass.Codes#Unauthorized_VALUE}
     *
     * @param errorCode code of error
     * @param response  {@link com.kfzs.duanduan.proto.ApiResponseOuterClass.ApiResponse}
     */
    public abstract void onApiError(int errorCode, ApiResponseOuterClass.ApiResponse response);

    /**
     * this method call back by pares error code is {@link #INVALID_PROTOCOL_BUFFER_ERROR}
     * <br/> most of time Exception is {@link InvalidProtocolBufferException} or other Exception
     *
     * @param errorCode {@link #INVALID_PROTOCOL_BUFFER_ERROR}
     * @param e         {@link Exception}
     */
    public abstract void onParseError(int errorCode, Exception e);
}
