package com.kfzs.duanduan.utils.net;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.android.volleyplus.AuthFailureError;
import com.android.volleyplus.NetworkResponse;
import com.android.volleyplus.ParseError;
import com.android.volleyplus.Request;
import com.android.volleyplus.Response;
import com.android.volleyplus.toolbox.HttpHeaderParser;
import com.kfzs.duanduan.KFZSApp;
import com.kfzs.duanduan.bean.BaseMsg;
import com.kfzs.duanduan.bll.UrlBll;
import com.kfzs.duanduan.datashare.DDProviderHelper;
import com.kfzs.duanduan.db.DataSave;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import go.kfzssafe.Kfzssafe;


/**
 * <pre>
 *     sinlov
 *
 *     /\__/\
 *    /`    '\
 *  ≈≈≈ 0  0 ≈≈≈ Hello world!
 *    \  --  /
 *   /        \
 *  /          \
 * |            |
 *  \  ||  ||  /
 *   \_oo__oo_/≡≡≡≡≡≡≡≡o
 *
 * </pre>
 * Created by sinlov on 17/2/17.
 */
public class APIRequestV2 extends Request<BaseMsg> {

    private Response.Listener<BaseMsg> listener;
    private byte[] bodyByte;

    public APIRequestV2(int method, String url, byte[] bodyByte, Response.Listener<BaseMsg> responseListener, Response.ErrorListener errorListener) {
        super(method, url, errorListener);
        this.listener = responseListener;
        this.bodyByte = bodyByte;
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> headers = new HashMap<>();
        headers.put(UrlBll.HeadParams.SECRET_KEY, DataSave.getInstance().getApkSign());
        String token = DDProviderHelper.getInstance().getLastUserToken(KFZSApp.context);
        if (!TextUtils.isEmpty(token)) {
            headers.put(UrlBll.HeadParams.AUTHORIZATION, token);
        }
        return headers;
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        if (null != bodyByte) {
            return Kfzssafe.xByteEncode(bodyByte);
        } else {
            return super.getBody();
        }
    }

    @Override
    protected Response<BaseMsg> parseNetworkResponse(NetworkResponse networkResponse) {
        try {
            String hexString = new String(networkResponse.data,
                    HttpHeaderParser.parseCharset(networkResponse.headers));
            BaseMsg apiResponse = JSON.parseObject(Kfzssafe.xDecodeS2B(hexString), BaseMsg.class);
            return Response.success(apiResponse, HttpHeaderParser.parseCacheHeaders(networkResponse));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException e) {
            return Response.error(new ParseError(e));
        } catch (NumberFormatException e) {
            return Response.error(new ParseError(e));
        } catch (Exception e) {
            return Response.error(new ParseError(e));
        }
    }

    @Override
    protected void deliverResponse(BaseMsg apiRequest) {
        if (listener != null) {
            listener.onResponse(apiRequest);

        }
    }


    @Override
    protected void onFinish() {
        super.onFinish();
        listener = null;
    }

}