package com.tofly.ghrq_route.module.dasData.service;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.url.NetRequestURL.newApi_dyrq;
import com.tofly.ghrq_route.base.URLResponseCode.ErrorCode;
import com.tofly.ghrq_route.base.URLResponseCode.URLResponse;
import com.tofly.ghrq_route.module.dasData.controller.DasDataCallBack;
import com.tofly.ghrq_route.module.dasData.domain.DasData;
import com.tofly.ghrq_route.common.util.net.volley.JsonRequestConstant;

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

/**
 * Created by Amu on 2017/3/8.
 */

public class DasDataServiceImpl implements IDasDataService {

    @Override
    public void uploadDasDataBatch(Context context, List<DasData> dataList, DasDataCallBack dasDataCallBack) {
        String url = newApi_dyrq.getDasDataBatchUploadUrl();
        StringRequest postRequest = new StringRequest(Request.Method.POST,
                url, response -> {
            URLResponse responseCode = new URLResponse(response);
            if (responseCode.getCode() == ErrorCode.SUCCESS){
                dasDataCallBack.onSetData(true, responseCode.getCode(), responseCode.getMessage());
            }else {
                dasDataCallBack.onSetData(false, responseCode.getCode(), responseCode.getMessage());
            }
        }, error -> {
            //登录失败
            if (error.networkResponse != null) {
                try {
                    String errString = new String(error.networkResponse.data, "UTF-8");
                    dasDataCallBack.onSetData(false, error.networkResponse.statusCode, errString);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    dasDataCallBack.onSetData(false, error.networkResponse.statusCode, null);
                }
            }else {
                String errString = ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                dasDataCallBack.onSetData(false, ErrorCode.HTTPCONN_TIMEOUT, errString);
            }
        }){
            @Override
            public Map<String, String> getHeaders(){
                //参数拼装:表单提交方式
                Map<String, String> headerParams = newApi_dyrq.getHeaderParams();
                return headerParams;
            }

            @Override
            public byte[] getBody(){
                JsonObject root = new JsonObject();
                Gson gson = new Gson();
                if (dataList != null && dataList.size() > 0){
                    JsonArray jsonArray = gson.toJsonTree(dataList,
                            new TypeToken<List<DasData>>(){}.getType()).getAsJsonArray();
                    root.add("dataCollectionList", jsonArray);
                }

                String pipeDevice = gson.toJson(root);
                return pipeDevice.getBytes();
            }

            @Override
            public String getBodyContentType() {
                return JsonRequestConstant.PROTOCOL_CONTENT_TYPE;
            }
        };
        BaseApp.addRequest(context.getClass().getName(), postRequest);
    }
}
