package com.tofly.ghrq_route.module.download.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.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.url.NetRequestURL;
import com.tofly.ghrq_route.base.URLResponseCode;
import com.tofly.ghrq_route.module.download.domain.DownloadData;
import com.tofly.ghrq_route.module.download.domain.UpDownloadData;

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

/**
 * Created by lihualong on 2017/6/19.
 */

public class DownloadListService implements IDownloadListService {
    private Context context;

    public DownloadListService(Context context) {
        this.context = context;
    }

    //    @Override
//    public void downloadList(final String token, final DownloadListCallback downloadListCallback) {
//        final String url = NetRequestURL.getDownloadList();
//        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
//            @Override
//            public void onResponse(String response) {
//                URLResponseCode.URLResponse urlResponse = new URLResponseCode.URLResponse(response);
//                if (URLResponseCode.ErrorCode.DOWNLOAD_LIST_SUCCESS == urlResponse.getCode()){
//                    String result = urlResponse.getResult();
//                    Gson gson = new Gson();
//                    try {
//                        List<DownloadData> list = gson.fromJson(result, new
//                                TypeToken<List<DownloadData>>() {
//                        }.getType());
//                        downloadListCallback.getList(true,urlResponse.getCode(),urlResponse
//                                .getMessage(),list);
//                    } catch (JsonSyntaxException e) {
//                        e.printStackTrace();
//                        downloadListCallback.getList(false,urlResponse.getCode(),e.getMessage(),null);
//                    }
//                }else {
//                    downloadListCallback.getList(false,urlResponse.getCode(),
//                            urlResponse.getMessage(),null);
//                }
//            }
//        }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//                if (error.networkResponse != null) {
//                    try {
//                        String errString = new String(error.networkResponse.data, "UTF-8");
//                        downloadListCallback.getList(false,error.networkResponse.statusCode
//                                ,errString,null);
//                    } catch (UnsupportedEncodingException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                        downloadListCallback.getList(false,error.networkResponse.statusCode
//                                ,e.getMessage(),null);
//                    }
//                }else {
//                    downloadListCallback.getList(false,URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT,
//                            URLResponseCode.ErrorCode.getErrorMessage(URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT),null);
//                }
//            }
//        }){
////            @Override
////            protected Map<String, String> getParams() throws AuthFailureError {
////                Map<String, String> objectObjectMap = new HashMap<>();
////
////                return super.getParams();
////            }
//
//            @Override
//            public Map<String, String> getHeaders() throws AuthFailureError {
//                Map<String, String> headMap = new HashMap<>();
//                headMap.put("token",token);
//                return headMap;
//            }
//        };
//        BaseApp.addRequest(context.getClass().getName(), stringRequest);
//    }
    @Override
    public void downloadList(final DownloadListCallback downloadListCallback) {
        final String url = NetRequestURL.getDownloadList() + "?ckey=offline";
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, response -> {
            URLResponseCode.URLResponse urlResponse = new URLResponseCode.URLResponse(response);
            if (URLResponseCode.ErrorCode.SUCCESS == urlResponse.getCode()) {
                String result = urlResponse.getResult();
                Gson gson = new Gson();
                try {
                    List<DownloadData> list = gson.fromJson(result, new
                            TypeToken<List<DownloadData>>() {
                            }.getType());
                    downloadListCallback.getList(true, urlResponse.getCode(), urlResponse
                            .getMessage(), list);
                } catch (JsonSyntaxException e) {
                    e.printStackTrace();
                    downloadListCallback.getList(false, urlResponse.getCode(), e.getMessage(), null);
                }
            } else {
                downloadListCallback.getList(false, urlResponse.getCode(),
                        urlResponse.getMessage(), null);
            }
        }, error -> {
            if (error.networkResponse != null) {
                try {
                    String errString = new String(error.networkResponse.data, "UTF-8");
                    downloadListCallback.getList(false, error.networkResponse.statusCode
                            , errString, null);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    downloadListCallback.getList(false, error.networkResponse.statusCode
                            , e.getMessage(), null);
                }
            } else {
                downloadListCallback.getList(false, URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT,
                        URLResponseCode.ErrorCode.getErrorMessage(URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT), null);
            }
        }) {
//            @Override
//            protected Map<String, String> getParams() throws AuthFailureError {
//                Map<String, String> objectObjectMap = new HashMap<>();
//                objectObjectMap.put("ckey", "offline");
//                return super.getParams();
//            }

            @Override
            public Map<String, String> getHeaders() {
                Map<String, String> headMap = NetRequestURL.newApi_dyrq.getHeaderParams();
                return headMap;
            }
        };
        BaseApp.addRequest(context.getClass().getName(), stringRequest);
    }

    @Override
    public void downloadList1(DownloadListCallback downloadListCallback) {
        String url = NetRequestURL.getDownloadList1() + "?remotePath=/dyrq/mobile/";
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, response -> {
            URLResponseCode.URLResponse urlResponse = new URLResponseCode.URLResponse(response);
            if (urlResponse.getCode() == URLResponseCode.ErrorCode.SUCCESS) {
                String result = urlResponse.getResult();
                Gson gson = new Gson();
                try {
                    List<UpDownloadData> list = gson.fromJson(result, new TypeToken<List<UpDownloadData>>() {
                    }.getType());
                    downloadListCallback.getList1(true, urlResponse.getCode(), urlResponse.getMessage(), list);
                } catch (JsonSyntaxException e) {
                    e.printStackTrace();
                    downloadListCallback.getList1(false, urlResponse.getCode(), e.getMessage(), null);
                }
            } else {
                downloadListCallback.getList1(false, urlResponse.getCode(),
                        urlResponse.getMessage(), null);
            }
        }, error -> {
            if (error.networkResponse != null) {
                try {
                    String errString = new String(error.networkResponse.data, "UTF-8");
                    downloadListCallback.getList1(false, error.networkResponse.statusCode
                            , errString, null);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    downloadListCallback.getList1(false, error.networkResponse.statusCode
                            , e.getMessage(), null);
                }
            } else {
                downloadListCallback.getList1(false, URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT,
                        URLResponseCode.ErrorCode.getErrorMessage(URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT), null);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() {
                Map<String, String> headMap = NetRequestURL.newApi_dyrq.getHeaderParams();
                return headMap;
            }
        };
        BaseApp.addRequest(context.getClass().getName(), stringRequest);
    }
}
