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

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

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
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.checkVersion.controller.ICheckVerCallback;
import com.tofly.ghrq_route.module.checkVersion.domain.CheckReslult;

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

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

public class VersionImp implements IVersion{
    private Context mContext;

    public VersionImp(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void getVersion(Context context, final String versionSub, final ICheckVerCallback checkVerCallback) {
        String url = newApi_dyrq.getCheckVersionUrl();
        if (versionSub != null && !versionSub.isEmpty()) {
            url += "?version=" + versionSub;
        }
        //Http post请求,用户登录
        StringRequest postRequest = new StringRequest(Request.Method.GET,
                url, response -> {
                    URLResponseCode.URLResponse responseCode = new URLResponseCode.URLResponse(response);
                    if (responseCode.getCode() == URLResponseCode.ErrorCode.SUCCESS){
                        String message = responseCode.getMessage();
                        String result = responseCode.getResult();
                        if (!TextUtils.isEmpty(result)) {
                            Gson gson = new Gson();
                            try {
                                CheckReslult version = gson.fromJson(result, CheckReslult.class);
                                checkVerCallback.onCheck(true, responseCode.getCode(), message, version);
                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                                checkVerCallback.onCheck(false,responseCode.getCode(),e.getMessage(),null);
                            }
                        }else {
                            checkVerCallback.onCheck(false,responseCode.getCode(),"apk历史版本列表为空",null);
                        }
                    }else {
                        checkVerCallback.onCheck(false,responseCode.getCode(),responseCode.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");
                        checkVerCallback.onCheck(false,error.networkResponse.statusCode
                                ,errString,null);
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        checkVerCallback.onCheck(false,error.networkResponse.statusCode
                                ,e.getMessage(),null);
                    }
                }else {
                    checkVerCallback.onCheck(false,URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT,
                            URLResponseCode.ErrorCode.getErrorMessage(URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT),null);
                }
            }
        }){
            @Override
            public Map<String, String> getHeaders(){
                //参数拼装:表单提交方式
                return newApi_dyrq.getHeaderParams();
            }
        };
        BaseApp.addRequest(context.getClass().getName(), postRequest);
    }

    @Override
    public void getAppInfo(Context context, String versionSub, ICheckVerCallback checkVerCallback) {
        String url = newApi_dyrq.getVersionInfoUrl();
        if (versionSub != null && !versionSub.isEmpty()) {
            url += "?version=" + versionSub;
        }
        //Http post请求,用户登录
        StringRequest postRequest = new StringRequest(Request.Method.GET,
                url, response -> {
            URLResponseCode.URLResponse responseCode = new URLResponseCode.URLResponse(response);

            if (responseCode.getCode() == URLResponseCode.ErrorCode.SUCCESS){
                String message = responseCode.getMessage();
                String result = responseCode.getResult();
                if (!TextUtils.isEmpty(result)) {
                    Gson gson = new Gson();
                    try {
                        CheckReslult version = gson.fromJson(result, CheckReslult.class);
                        checkVerCallback.onCheck(true, responseCode.getCode(), message, version);
                    } catch (JsonSyntaxException e) {
                        e.printStackTrace();
                        checkVerCallback.onCheck(false,responseCode.getCode(),e.getMessage(),null);
                    }
                }else {
                    checkVerCallback.onCheck(false,responseCode.getCode(),"apk历史版本列表为空",null);
                }
            }else {
                checkVerCallback.onCheck(false,responseCode.getCode(),responseCode.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");
                        checkVerCallback.onCheck(false,error.networkResponse.statusCode
                                ,errString,null);
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        checkVerCallback.onCheck(false,error.networkResponse.statusCode
                                ,e.getMessage(),null);
                    }
                }else {
                    checkVerCallback.onCheck(false,URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT,
                            URLResponseCode.ErrorCode.getErrorMessage(URLResponseCode.ErrorCode.HTTPCONN_TIMEOUT),null);
                }
            }
        }){
            @Override
            public Map<String, String> getHeaders(){
                //参数拼装:表单提交方式
                return newApi_dyrq.getHeaderParams();
            }
        };
        BaseApp.addRequest(context.getClass().getName(), postRequest);
    }
}
