package com.tofly.ghrq_route.base;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Amu on 2017/2/9.
 */

public class URLResponseCode{

    public static class URLResponse{
        private int code = -1;
        private String result;
        private String message;

        public URLResponse(){

        }

        public URLResponse(String response){
            Log.d(URLResponse.class.getSimpleName(), response);
            try {
                JSONObject jsonObject = new JSONObject(response);
                if (jsonObject.has("code")){
                    code = jsonObject.getInt("code");
                }
                if (jsonObject.has("message")){
                    message = jsonObject.getString("message");
                }
                if (jsonObject.has("result")){
                    result = jsonObject.getString("result");
                }
            } catch (JSONException e) {
                code = -1;
                message = response;
                result = "";
                e.printStackTrace();
            }
        }

        public static URLResponse FastResponse(String response) {
            URLResponse urlResponse = new URLResponse();
            urlResponse.result = response;
            urlResponse.code = ErrorCode.SUCCESS;
            urlResponse.message = "";
            return urlResponse;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getResult() {
            return result;
        }

        public void setResult(String result) {
            this.result = result;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public String queryResult(String key){
            String value = null;
            if (result != null && !result.isEmpty()){
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.has(key)){
                        value = jsonObject.getString(key);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            return value;
        }

        public String queryRows(String key){
            String value = null;
            if (result != null && !result.isEmpty()){
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.has("result")) {
                        JSONObject jsonReuslt = jsonObject.getJSONObject("result");
                        if (jsonReuslt.has("rows")) {
                            JSONArray rows = jsonReuslt.getJSONArray("rows");
                            if (rows != null && rows.length() > 0) {
                                JSONObject jsonObject1 = rows.getJSONObject(0);
                                if (jsonObject1.has(key)) {
                                    value = jsonObject1.getString(key);
                                }
                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            return value;
        }

        public String queryResultResult(String root_key, String key){
            String value = null;
            if (result != null && !result.isEmpty()){
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.has(root_key)){
                        String root_value = jsonObject.getString(root_key);
                        if (root_value != null && !root_value.isEmpty()){
                            jsonObject = new JSONObject(root_value);
                            if (jsonObject.has(key)) {
                                value = jsonObject.getString(key);
                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            return value;
        }
    }


    /**
     * Created by Amu on 2017/2/9.
     */

    public static class ErrorCode {
        public static final int SUCCESS = 1;
        public static final int SUCCESS_OFFLINE = 1000;
        public static final int SUCCESS_MAP_SERVER = 10000;
        public static final int UNKNOWN_ERROR = 2;
        public static final int ALREADY_EXISTS = 2;

        public static final int PLAN_TRANSFER_ERROR = 3;

        public static final int LOGIN_RELOGIN_ERROR = 4; // 登录时用户已经登录

        private static final int CAN_NOT_EMPTY = 6;
        public static final int APP_MUST_UPDATE = 6;
        public static final int PASSWORD_ERROR = 7;
        public static final int LOGIN_OFFLINE = 8;
        public static final int LOGIN_RESIST = 9;

        private static final int ACCOUNT_EXIST = 15;

        private static final int ACCOUNT_NOT_EXIST = 19;

        private static final int IDENTIFY_CODE_CANNOT_NULL = 50;
        private static final int IDENTIFY_CODE_ERROR = 51;
        public static final int NETWORK_ERROR = -1;
        public static final int GET_USER_FAILED = -2;

        private static final int NETWORK_LINK_ERROR = -98;

        public static final int HTTPCONN_TIMEOUT = -99;

        //服务器运行时出错
        public static final int SERVER_RUNTIME_ERROR = 400;

        //Token授权验证失败
        public static final int TOKEN_VERIFY_FAILED = 401;
//
        public static final int DOWNLOAD_LIST_SUCCESS = 10000;
        public static final int DOWNLOAD_LIST_FAILURE = 10000;

        //网页地址未找到
        public static final int ADDR_FOUND_FAILED = 404;

        public static String getErrorMessage(int error_code) {
            try {
                int i = error_code;
                switch (i) {
                    case SUCCESS:

                        return "成功";
                    case UNKNOWN_ERROR:

                        return "未知错误";

                    case LOGIN_RELOGIN_ERROR:

                        return "用户已登录";

                    case CAN_NOT_EMPTY:

                        return "不能为空";

                    case PASSWORD_ERROR:

                        return "密码错误";

                    case LOGIN_OFFLINE:

                        return "当前是离线登录，不可进行此操作";

                    case LOGIN_RESIST:
                        return "该用户不能登录此设备";

                    case ACCOUNT_EXIST:

                        return "账号已经存在";

                    case ACCOUNT_NOT_EXIST:

                        return "账号不存在";


                    case IDENTIFY_CODE_CANNOT_NULL:

                        return "验证码不能为空";

                    case SERVER_RUNTIME_ERROR:

                        return "服务器连接异常";

                    case TOKEN_VERIFY_FAILED:

                        return "此账户可能已经在别的设备登陆,请重新登录";

                    case IDENTIFY_CODE_ERROR:

                        return "验证码错误";

                    case NETWORK_LINK_ERROR:
                        return "抱歉,网络连接错误,请重新登录后再操作";

                    case GET_USER_FAILED:
                        return "获取用户信息失败，请稍后重试";

                    case HTTPCONN_TIMEOUT:
                        return "服务器连接超时";

                    case 99:
                        return "重复操作";
                    case ADDR_FOUND_FAILED:
                        return "服务器地址未找到";
                    default:
                        return "";
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return "未知错误";
        }
    }
}
