package com.yyrh.core.network;

import android.content.Context;
import android.text.TextUtils;

import com.yyrh.core.entity.ResultInfo;
import com.yyrh.core.internal.IRequestCallback;
import com.yyrh.core.SDKDrive;
import com.yyrh.utils.YYLog;

import org.json.JSONObject;

import cn.yyxx.support.JsonUtils;
import cn.yyxx.support.volley.VolleySingleton;
import cn.yyxx.support.volley.source.DefaultRetryPolicy;
import cn.yyxx.support.volley.source.Request;
import cn.yyxx.support.volley.source.Response;
import cn.yyxx.support.volley.source.VolleyError;
import cn.yyxx.support.volley.source.toolbox.StringRequest;

/**
 * @author #Suyghur.
 * Created on 2021/10/15
 */
public final class VolleyRequest {

    private static final int MAX_TIMEOUT = 10 * 1000;

    public static void post(final Context context, JSONObject jsonObject, final IRequestCallback callback) {
        try {

            // AES/CBC/PKCS7Padding
            final String enc = SDKDrive.getInstance().invokeJob(jsonObject.toString());

            YYLog.d("请求密文: " + enc);
            YYLog.logHandler("请求密文: " + enc + "\n");

            StringRequest request = new StringRequest(Request.Method.POST, Host.BASEURL, new Response.Listener<String>() {
                @Override
                public void onResponse(String resp) {
                    YYLog.d("响应密文: " + resp);
                    YYLog.logHandler("响应密文: " + resp + "\n");
                    try {
                        String raw = SDKDrive.getInstance().parseJob(resp);
                        YYLog.d("返回数据: " + raw);
                        YYLog.logHandler("返回数据: " + raw + "\n");

                        JSONObject responseObject = new JSONObject(raw);
                        ResultInfo resultInfo = new ResultInfo();

                        if (JsonUtils.hasJsonKey(responseObject, "code")) {
                            resultInfo.code = responseObject.getInt("code");
                        }
                        if (JsonUtils.hasJsonKey(responseObject, "msg")) {
                            resultInfo.msg = responseObject.getString("msg");
                        }
                        if (JsonUtils.hasJsonKey(responseObject, "data")) {
                            resultInfo.data = responseObject.getString("data");
                        }
                        callback.onResponse(resultInfo);
                    } catch (Exception e) {
                        e.printStackTrace();
                        callback.onResponse(new ResultInfo());
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    ResultInfo resultInfo = new ResultInfo();
                    if (volleyError.networkResponse != null) {
                        resultInfo.code = volleyError.networkResponse.statusCode;
                    }
                    if (!TextUtils.isEmpty(volleyError.getMessage())) {
                        resultInfo.msg = volleyError.getMessage();
                    }
                    callback.onResponse(resultInfo);
                }
            }) {
                @Override
                public byte[] getBody() {
                    return enc.getBytes();
                }

                @Override
                public String getBodyContentType() {
                    return "text/plain";
                }
            };

            request.setRetryPolicy(new DefaultRetryPolicy(MAX_TIMEOUT, 1, 1.0f));
            VolleySingleton.getInstance(context.getApplicationContext()).addToRequestQueue(context.getApplicationContext(), request);

        } catch (Exception e) {
            e.printStackTrace();
            callback.onResponse(new ResultInfo());
        }
    }


}
