package com.tofly.ghrq_route.module.incident.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.reflect.TypeToken;
import com.mgq.base.utils.errcode.ErrorCode;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.url.NetRequestURL.newApi_dyrq;
import com.tofly.ghrq_route.base.URLResponseCode;
import com.tofly.ghrq_route.module.incident.controller.MonitorCallback;
import com.tofly.ghrq_route.module.incident.domain.CarUploadBean;
import com.tofly.ghrq_route.module.incident.domain.DeviceUploadBean;
import com.tofly.ghrq_route.common.util.date.DateUtils;

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

public class MonitorServiceImpl {

    public MonitorServiceImpl() {
    }

    public void getDeviceUploadList(Context context, final String company_id, final int pageindex, final int pagesize, final MonitorCallback incidentCallback) {
        //Http post请求,获取隐患列表
        String url = newApi_dyrq.getDeviceUploadListUrl() + "?current=" + pageindex + "&size=" + pagesize
                + "&departmentId=" + company_id + "&isLastest=" + true;
        StringRequest postRequest = new StringRequest(Request.Method.GET, url, response -> {
            URLResponseCode.URLResponse responseCode = new URLResponseCode.URLResponse(response);
            if (responseCode.getCode() == URLResponseCode.ErrorCode.SUCCESS){
                String rowsArray = responseCode.queryResult("records");
                List<DeviceUploadBean> deviceList = null;
                int resCode = -1;
                boolean result = false;
                String parseErrMsg = "用户解析失败";
                int total = 0;
                String strTotal = responseCode.queryResult("total");
                if (strTotal != null && !strTotal.isEmpty()){
                    total = Integer.parseInt(strTotal);
                }
                if (rowsArray != null){
                    try {
                        Gson gson = new Gson();
                        List<DeviceUploadBean> devices = gson.fromJson(rowsArray, new TypeToken<List<DeviceUploadBean>>(){}.getType());
                        if (devices != null){
                            deviceList = devices;
                            resCode = responseCode.getCode();
                            parseErrMsg = responseCode.getMessage();
                            result = true;
                        }
                    }catch (Exception e){
                        incidentCallback.onGetDeviceUploadList(false, resCode, parseErrMsg, null, 0);
                    }
                }
                incidentCallback.onGetDeviceUploadList(result, resCode, parseErrMsg, deviceList, total);
            }else {
                incidentCallback.onGetDeviceUploadList(false, responseCode.getCode(), responseCode.getMessage(), null, 0);
            }
        }, error -> {
            //登录失败
            if (error.networkResponse != null) {
                try {
                    String errString = new String(error.networkResponse.data, "UTF-8");
                    incidentCallback.onGetDeviceUploadList(
                            false, error.networkResponse.statusCode, errString, null, 0);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    incidentCallback.onGetDeviceUploadList(
                            false, error.networkResponse.statusCode, null, null, 0);
                }
            }else {
                String errString = URLResponseCode.ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                incidentCallback.onGetDeviceUploadList(
                        false, ErrorCode.HTTPCONN_TIMEOUT, errString, null, 0);
            }
        }){
/*            @Override
            protected Map<String, String> getParams(){
                //参数拼装:表单提交方式
                Map<String, String> params = new HashMap<>();
                params.put("pageindex", pageindex + "");
                params.put("pagesize", pagesize + "");
                params.put("departmentId", company_id);
                params.put("search_date", DateUtils.getYMD());
                return params;
            }*/

            @Override
            public Map<String, String> getHeaders(){
                //参数拼装:表单提交方式
                return newApi_dyrq.getHeaderParams();
            }
        };
        BaseApp.addRequest(context.getClass().getName(), postRequest);
    }

    public void getCarUploadList(Context context, final int pageindex, final int pagesize, final MonitorCallback incidentCallback) {
        //Http post请求,获取隐患列表
        String url = newApi_dyrq.getCarUploadListUrl() + "?current=" + pageindex + "&size=" + pagesize
                + "&date=" + DateUtils.getYMD();
        StringRequest postRequest = new StringRequest(Request.Method.GET, url, response -> {
            URLResponseCode.URLResponse responseCode = new URLResponseCode.URLResponse(response);
            if (responseCode.getCode() == URLResponseCode.ErrorCode.SUCCESS){
                String rowsArray = responseCode.queryResult("records");
                List<CarUploadBean> carList = null;
                int resCode = -1;
                boolean result = false;
                String parseErrMsg = "用户解析失败";
                int total = 0;
                String strTotal = responseCode.queryResult("total");
                if (strTotal != null && !strTotal.isEmpty()){
                    total = Integer.parseInt(strTotal);
                }
                if (rowsArray != null){
                    try {
                        Gson gson = new Gson();
                        List<CarUploadBean> cars = gson.fromJson(rowsArray, new TypeToken<List<CarUploadBean>>(){}.getType());
                        if (cars != null){
                            carList = cars;
                            resCode = responseCode.getCode();
                            parseErrMsg = responseCode.getMessage();
                            result = true;
                        }
                    }catch (Exception e){
                        incidentCallback.onGetCarUploadList(false, resCode, parseErrMsg, null, 0);
                    }
                }
                incidentCallback.onGetCarUploadList(result, resCode, parseErrMsg, carList, total);
            }else {
                incidentCallback.onGetCarUploadList(false, responseCode.getCode(), responseCode.getMessage(), null, 0);
            }
        }, error -> {
            //登录失败
            if (error.networkResponse != null) {
                try {
                    String errString = new String(error.networkResponse.data, "UTF-8");
                    incidentCallback.onGetCarUploadList(
                            false, error.networkResponse.statusCode, errString, null, 0);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    incidentCallback.onGetCarUploadList(
                            false, error.networkResponse.statusCode, null, null, 0);
                }
            }else {
                String errString = URLResponseCode.ErrorCode.getErrorMessage(ErrorCode.HTTPCONN_TIMEOUT);
                incidentCallback.onGetCarUploadList(
                        false, ErrorCode.HTTPCONN_TIMEOUT, errString, null, 0);
            }
        }){
            @Override
            public Map<String, String> getHeaders(){
                //参数拼装:表单提交方式
                return newApi_dyrq.getHeaderParams();
            }
        };
        BaseApp.addRequest(context.getClass().getName(), postRequest);
    }
}
